Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Printing loop twice?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Printing loop twice?

    Hi, I'm very new to Java and I've run into a strange problem while I was writing a calculator class.

    For some reason, anytime I run the program it prints this part twice:

    System.out.println("Calculator started.");
    System.out.println("Would you like to:");
    System.out.println("Multiply");
    System.out.println("Divide");
    System.out.println("Add");
    System.out.println("Subtract");
    System.out.println("Random");
    opr = inp.nextLine();


    It's a pretty simple problem, but I'd also like some feedback on the rest of my code. Thanks!

    import java.util.*;
     
    public class CalculatorAdv {
     
    	public static void main(String[] args) {
    		int fNum;
    		int sNum;
    		int aNum;
    		String opr;
    		int cont;
    		Scanner inp = new Scanner(System.in);
    		Random rand = new Random();
     
    		System.out.println("Hello, welcome to the calculator.");
    		System.out.println("Would you like to proceed? (Press 1 to continue, anything else to terminate.)");
    		cont = inp.nextInt();
     
    		do
    		{
    			int cont2 = 0;
    			System.out.println("Calculator started.");
    			System.out.println("Would you like to:");
    			System.out.println("Multiply");
    			System.out.println("Divide");
    			System.out.println("Add");
    			System.out.println("Subtract");
    			System.out.println("Random");
    			opr = inp.nextLine();
     
     
    			if(opr.equalsIgnoreCase("Multiply"))
    			{
    				System.out.println("You've chosen multiply.");
    				System.out.print("First number: " );
    				fNum = inp.nextInt();
    				System.out.print("Second number: " );
    				sNum = inp.nextInt();
    				aNum = mult(fNum, sNum);
    				System.out.println(fNum + " times " + sNum + " equals: " + aNum);
    				cont2++;
    			}
    			else if(opr.equalsIgnoreCase("Divide"))
    			{
    				System.out.println("You've chosen divide.");
    				System.out.print("First number: " );
    				fNum = inp.nextInt();
    				System.out.print("Second number: ");
    				sNum = inp.nextInt();
    				aNum = div(fNum, sNum);
    				System.out.println(fNum + " divided by " + sNum + " equals: " + aNum);
    				cont2++;
    			}
    			else if(opr.equalsIgnoreCase("Add"))
    			{
    				System.out.println("You've chosen add.");
    				System.out.print("First number: ");
    				fNum = inp.nextInt();
    				System.out.print("Second number: ");
    				sNum = inp.nextInt();
    				aNum = add(fNum, sNum);
    				System.out.println(fNum + " plus " + sNum + " equals: " + aNum);
    				cont2++;
    			}
    			else if(opr.equalsIgnoreCase("Subtract"))
    			{
    				System.out.println("You've chosen subtract.");
    				System.out.print("First number: ");
    				fNum = inp.nextInt();
    				System.out.print("Second number: ");
    				sNum = inp.nextInt();
    				aNum = sub(fNum, sNum);
    				System.out.println(fNum + " minus " + sNum + " equals: " + aNum);
    				cont2++;
    			}
    			else if(opr.equalsIgnoreCase("Random"))
    			{
    				System.out.println("You've chosen random.");
    				int rndchoice = rand.nextInt();
    				System.out.println("Your random number is: " + rndchoice);
    				cont2++;
    			}
     
    			while(cont2 == 1)
    			{
    				System.out.println("Would you like to perform another calculation?");
    				System.out.println("1 for yes, anything else to terminate.");
    				cont = inp.nextInt();
    				cont2--;
    			}
     
    		}while(cont == 1);
     
    		System.out.println("System terminated.");
    	}
     
     
     
    	public static int mult(int x, int y)
    	{
    		return x * y;
    	}
     
    	public static int add(int x, int y)
    	{
    		return x + y;
    	}
     
    	public static int div(int x, int y)
    	{
    		return x / y;
    	}
     
    	public static int sub(int x, int y)
    	{
    		return x - y;
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Printing loop twice?

    Try while loop instead of do while loop. Go through the details of while and do while which will make you clear regarding the issue you are facing. When you use do while your code actually gets executed atleast once inspite of your condition and then checks for your condition and based on the result (whether true or false) it either continues or exits from the loop.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    151
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: Printing loop twice?

    Basically what is happening is when you are using the nextInt() method, there is still a newline character left in the buffer, which is picked up when calling nextLine(). Basically, you just need to clear out the buffer before going into your do-while loop.

    I hope this makes sense

Similar Threads

  1. How tp printing array elements vertically through loop.
    By bikes_28 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 6th, 2012, 12:51 PM
  2. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  3. HELP: UNABLE TO USE NON-PRINTING CHARACTERS IN A LOOP
    By baraka.programmer in forum Loops & Control Statements
    Replies: 5
    Last Post: June 24th, 2011, 02:21 AM
  4. Triangle printing (for loop)
    By thirdwave in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2010, 03:07 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread