I'm writing a program were I have to give the user the option of starting again.........so I ask 'Would you like to add more votes (y/n)?' then I scan for the answer using the line :
'another = scan.nextLine();'
The program compiles ok but when i run it. the program goes from start to end then straight to the start again. it ask the question but it doesn't wait for the answer or is unable to scan it.

Can any of the gurus in here help me please???

Not if you don't post the code. We really can't speculate what you have written.

Here is a small section of the program i'm trying to write.

// Booth 5
            System.out.println ("BOOTH 5");
            System.out.print ("Enter the number of votes for CANDIDATE A= ");
            a5 = scan.nextDouble();	
            System.out.print ("Enter the number of votes for CANDIDATE B= ");
            b5 = scan.nextDouble();
         	         
            t5 = a5+b5;
            A5 = a5/t5*100;
            B5 = b5/t5*100;
         	
            System.out.println ("CANDIDATE A = " + fmt.format(A5) + "%");
            System.out.println ("CANDIDATE B = " + fmt.format(B5) + "%");
            System.out.println ("Total votes BOOTH 5 = " + t5);
            System.out.println();

            System.out.println ("Would you like to add more votes (y/n)?");
            add = scan.nextLine();

How can I ake it to allow the user to do it again?
Should I use a 'while' statement or something else.
I hope this may give you an idea.....

Help

Typically, yes, you place a while loop around that section that checks a boolean variable like "continue" or "moreEntries" (or whatever). That variable is set based upon the response to your question, in your case the value of "add".

That is what I have being doing but still can't get it to work.
If i use it before the question, the program would run once then stop. If I use it after the question and the scanning, it will straight from the last booth (no. 5) to the fist one and start again. all this without waiting for an answer (y or n)

Well, are you using the equals method to compare the Strings or == ? Your code might help us to guide you better.

Here is the whole program;

import java.util.Scanner;
   import java.text.DecimalFormat;
	
    public class ElectionsA
   {
   			
   //********************************************************************
   // Receives and computes votes from the two electorates.
   //********************************************************************
   
       public static void main (String[]args)
      {
         double a1, b1, a2, b2, a3, b3, a4, b4, a5, b5;
         double t1, t2, t3, t4, t5, A1, A2, A3, A4, A5, B1, B2, B3, B4, B5;    
         double Tt, At, Bt;
         String add ="y";
               		
         Scanner scan = new Scanner (System.in);
         DecimalFormat fmt = new DecimalFormat ("0.##");
      	
         // Votes from the INNER CITY ELECTORATE.
         	System.out.println ("ELECTIONS 2008 ");
            System.out.println ();
            System.out.println ("INNER CITY ELECTORATE");
				
            while (add.equals("y"))
            {
			// Booth 1.
            System.out.println ("BOOTH 1");
            System.out.print ("Enter the number of votes for CANDIDATE A= ");
            a1 = scan.nextDouble();	
            System.out.print ("Enter the number of votes for CANDIDATE B= ");
            b1 = scan.nextDouble();
         
            t1 = a1+b1;
            A1 = a1/t1*100;
            B1 = b1/t1*100;
         	
            System.out.println ("CANDIDATE A = " + fmt.format(A1) + "%");
            System.out.println ("CANDIDATE B = " + fmt.format(B1) + "%");
            System.out.println ("Total votes BOOTH 1 = " + t1);
            System.out.println();
         
			// Booth 2
            System.out.println ("BOOTH 2");
            System.out.print ("Enter the number of votes for CANDIDATE A= ");
            a2 = scan.nextDouble();	
            System.out.print ("Enter the number of votes for CANDIDATE B= ");
            b2 = scan.nextDouble();
         	         
            t2 = a2+b2;
            A2 = a2/t2*100;
            B2 = b2/t2*100;
         	
            System.out.println ("CANDIDATE A = " + fmt.format(A2) + "%");
            System.out.println ("CANDIDATE B = " + fmt.format(B2) + "%");
            System.out.println ("Total votes BOOTH 2 = " + t2);
            System.out.println();
         	
         // Booth 3
            System.out.println ("BOOTH 3");
            System.out.print ("Enter the number of votes for CANDIDATE A= ");
            a3 = scan.nextDouble();	
            System.out.print ("Enter the number of votes for CANDIDATE B= ");
            b3 = scan.nextDouble();
         	         
            t3 = a3+b3;
            A3 = a3/t3*100;
            B3 = b3/t3*100;
         	
            System.out.println ("CANDIDATE A = " + fmt.format(A3) + "%");
            System.out.println ("CANDIDATE B = " + fmt.format(B3) + "%");
            System.out.println ("Total votes BOOTH 3 = " + t3);
            System.out.println();
         
         // Booth 4
            System.out.println ("BOOTH 4");
            System.out.print ("Enter the number of votes for CANDIDATE A= ");
            a4 = scan.nextDouble();	
            System.out.print ("Enter the number of votes for CANDIDATE B= ");
            b4 = scan.nextDouble();
         	         
            t4 = a4+b4;
            A4 = a4/t4*100;
            B4 = b4/t4*100;
         	
            System.out.println ("CANDIDATE A = " + fmt.format(A4) + "%");
            System.out.println ("CANDIDATE B = " + fmt.format(B4) + "%");
            System.out.println ("Total votes BOOTH 4 = " + t4);
            System.out.println();
         	
         // Booth 5
            System.out.println ("BOOTH 5");
            System.out.print ("Enter the number of votes for CANDIDATE A= ");
            a5 = scan.nextDouble();	
            System.out.print ("Enter the number of votes for CANDIDATE B= ");
            b5 = scan.nextDouble();
         	         
            t5 = a5+b5;
            A5 = a5/t5*100;
            B5 = b5/t5*100;
         	
            System.out.println ("CANDIDATE A = " + fmt.format(A5) + "%");
            System.out.println ("CANDIDATE B = " + fmt.format(B5) + "%");
            System.out.println ("Total votes BOOTH 5 = " + t5);
            System.out.println();
								
           // Grand Total
            Tt = (t1+t2+t3+t4+t5);
            At = (a1+a2+a3+a4+a5)/Tt*100;
            Bt = (b1+b2+b3+b4+b5)/Tt*100;
         	
            System.out.println ("The final tally is:");
            System.out.println ("Total votes = " + Tt);
            System.out.println();
            System.out.println ("CANDIDATE A total = " + fmt.format(At)+ "%");
            System.out.println ("CANDIDATE B total = " + fmt.format(Bt)+ "%");
            System.out.println();
            }
            System.out.println ("Would you like to add more votes (y/n)?");
            add = scan.nextLine();
           }
       }
   }

The red 'braket' makes the program run then stop at the end of the question
The green 'braket' makes the program run again and again, still displaying the question but no waiting for the answer.

The "green bracket" does not include the question to continue in the while loop at all, and since you set add='y' to begin with it will always be true. You need to use the "red bracket" and determine why "add" is not equal to "y" when you want to continue.

I would recommend you use a boolean value for "add" instead of the string comparison. Set that boolean based on the value of the input from your question.

Also, always indent blocks such as your while() loop block. Proper code formatting isn't just for "prettiness" - it helps you visually keep track of the logical structure of your code more easily.

First, can you explain what you mean by determine why 'add is not equal to 'y'?
I have being using the red bracket and the program 'cycles' but it doesn't scan or even wait for an answer, why?

Secondly, if i change 'add' from a string to a boolean. What do I have to change in the program, apaart from declaring it as a boolean?

Also, if a change 'add' to a boolean value, will a have to declare 'y' as a boolean as well? if not they may be incompatable.

This loop works just fine when I run it

while(add.equals("y")) {
    // Booth 1.
    System.out.println("BOOTH 1");
    // ... stuff
    System.out.println("Would you like to add more votes (y/n)?");
    add = scan.nextLine();
}

Converting this to use a boolean instead

boolean add = true;
while(add) {
    // Booth 1.
    System.out.println("BOOTH 1");

    System.out.println("Would you like to add more votes (y/n)?");
    String response = scan.nextLine();
    add = ("y".equalsIgnoreCase(response));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.