0

(Java) The first Print Statement loops twice after going around once. I am a new coder, and I have tried looking to see what is wrong but I have no clue what is wrong. Everything else works fine, I think the loop or the switch is causing the problem but again, I have no idea.

Scanner sc = new Scanner(System.in);
        
        
    for (int i = 1; i>0; i++) {
            
        System.out.print("Type in the name of the type of Equation you want to solve on the line below: (Addition, Subtraction, Multiplication, Division, Exponents)    If you want to Stop using the calculator, type 'Close'");

        System.out.println("");
        
        String Input = sc.nextLine();

        if (Input.equals("Close")) {
            break;
        }
        
        switch (Input) {
        
        case "Addition" : 
            System.out.print("Type in your first number: ");
                Double Addition1 = sc.nextDouble();
            System.out.print("Type in what you want to add your first number by: ");
                Double Addition2 = sc.nextDouble();
    
                Double Sum = Addition1 + Addition2;

        System.out.println(Addition1 + " + " + Addition2 + " = " + Sum);
        System.out.println("");
        break;
        
        case "Subtraction" :
            System.out.print("Type in your first number: ");
                Double Subtraction1 = sc.nextDouble();
            System.out.print("Type in what you want to subtract your first number by: ");
                Double Subtraction2 = sc.nextDouble();
    
                Double Difference = Subtraction1 - Subtraction2;

        System.out.println(Subtraction1 + " - " + Subtraction2 + " = " + Difference);
        System.out.println("");
        break;
        
        case "Multiplication" :
            System.out.print("Type in your first number: ");
                Double Multiplication1 = sc.nextDouble();
            System.out.print("Type in what you want to multiply your first number by: ");
                Double Multiplication2 = sc.nextDouble();
            
                Double Product = Multiplication1 * Multiplication2;

        System.out.println(Multiplication1 + " * " + Multiplication2 + " = " + Product);
        System.out.println("");
        break;
        
        
        case "Division" :
            System.out.print("Type in your first number: ");
                Double Division1 = sc.nextDouble();
            System.out.print("Type in what you want to divide your first number by: ");
                Double Division2 = sc.nextDouble();
    
                Double Quotient = Division1 / Division2;

        System.out.println(Division1 + " / " + Division2 + " = " + Quotient);
        System.out.println("");
        break;
            }
        }
João Dias
  • 16,277
  • 6
  • 33
  • 45
Josh
  • 1
  • What is your intention with the loop header of `for (int i = 1; i>0; i++)`? Does it look the same as the one your teacher showed you? – Dawood ibn Kareem Oct 30 '21 at 22:58
  • My intention is for it to keep looping unless it is broken by the ` if (Input.equals("Close")) { break; } ` At first i tried ` for (int i = 1; i==1;)` but after looping once, my print statement in the beginning still printed twice, I just changed it up a bit to see if it would fix it but it didn't. – Josh Oct 30 '21 at 23:08
  • OK. It looks a little confusing, and suggests that you made a mistake - that you intended to have some kind of limit (like `for (int i = 1; i < 10; i++ )` for example) . If you mean "loop forever", there are a couple of more conventional ways of writing it. I would recommend writing `while (true)` which is very clear. There's also `for(;;)`, which is pronounced "forever", but this is considered a bit old-fashioned and not popular among younger programmers. – Dawood ibn Kareem Oct 30 '21 at 23:11
  • Ok thank you! I am still not sure why my print statement loops twice but I will try to figure it out. Its not a major issue since I still got a long way to go with learning java, and the program works, and I just did this little project for fun. – Josh Oct 30 '21 at 23:17
  • Have a read of the duplicate question that khelwood linked you to, up the top. It describes how using `nextLine` and `nextDouble` on the same `Scanner` can sometimes have unexpected results, and what you can do about it. – Dawood ibn Kareem Oct 30 '21 at 23:19
  • Ok i will look into that, thank you! – Josh Oct 30 '21 at 23:22

0 Answers0