Troubleshooting Java Loop: My Loop Won't Run - Am I Missing Something?"

  • Thread starter Thread starter Darkstar3000
  • Start date Start date
  • Tags Tags
    Loop
AI Thread Summary
The user encountered an issue where their Java loop was not executing, initially believing it was due to a missing condition. The main problem was identified as the main method being declared as private, which prevented the program from running correctly. Changing the main method to public resolved the issue, allowing the loop to function as intended. The discussion emphasized the importance of adhering to Java's method signature requirements for execution. The user successfully ran their code after making the necessary adjustments.
Darkstar3000
Messages
28
Reaction score
0
I tried to write this program but the loop just won't run, am I missing something ?

Code:
import java.io.*;
import java.util.*;

public class basicswitch
{
private static void main(String [] args)throws IOException
{
int a ,b,y, x = 0;
double c = 0;

BufferedReader in;
in = new BufferedReader( new InputStreamReader( System.in ) );

while(true)
{
System.out.println("Enter the value of a: ");
a = Integer.parseInt( in.readLine() );

System.out.println("Enter the value of b: ");
b= Integer.parseInt( in.readLine() );

System.out.println("Enter 1 for addition\n2 for subtraction\n3 for multiplication\nor 4 for division");
x = Integer.parseInt( in.readLine() );

switch(x)
{
case 1:
c = a + b;
System.out.println("The sum is :"+c);
break;

case 2:
c = a - b;
System.out.println("The difference is :"+c);
break;

case 3:
c = a * b;
System.out.println("The product is :"+c);
break;

case 4:
c = (a / b);
System.out.println("The quotient is :"+c);
break;
}

System.out.println( "Press Y if you wish to enter new numbers : ");
y = in.readLine().charAt( 0 );

if((y == 'y') || (y == 'Y'))
{
System.out.println("Enter the value of a: ");
a = Integer.parseInt( in.readLine() );

System.out.println("Enter the value of b: ");
b= Integer.parseInt( in.readLine() );

continue;
}
}

}
/////////////////////////////////////////////////////////////////////////
} // End of class
 
Physics news on Phys.org
Your code would be easier to read if it were indented properly. I have done this below.
Darkstar3000 said:
I tried to write this program but the loop just won't run, am I missing something ?
When you say that the loop won't run, can you be more precise? What output are you getting? Since you have an infinite loop, the loop should run forever.
Darkstar3000 said:
Code:
import java.io.*;
import java.util.*;

public class basicswitch
{
  private static void main(String [] args)throws IOException
  {
    int a ,b,y, x = 0;
    double c = 0;

    BufferedReader in;
    in = new BufferedReader( new InputStreamReader( System.in ) );

    while(true)
    {
      System.out.println("Enter the value of a: ");
      a = Integer.parseInt( in.readLine() );

      System.out.println("Enter the value of b: ");
      b= Integer.parseInt( in.readLine() );

      System.out.println("Enter 1 for addition\n2 for subtraction\n3 for multiplication\nor 4 for division");
      x = Integer.parseInt( in.readLine() );

      switch(x)
      {
        case 1:
          c = a + b;
          System.out.println("The sum is :"+c);
          break;

        case 2:
          c = a - b;
          System.out.println("The difference is :"+c);
          break;

        case 3:
          c = a * b;
          System.out.println("The product is :"+c);
          break;

        case 4:
          c = (a / b);
          System.out.println("The quotient is :"+c);
          break;
      }

      System.out.println( "Press Y if you wish to enter new numbers : ");
      y = in.readLine().charAt( 0 );

      if((y == 'y') || (y == 'Y'))
      {
        System.out.println("Enter the value of a: ");
        a = Integer.parseInt( in.readLine() );

        System.out.println("Enter the value of b: ");
        b= Integer.parseInt( in.readLine() );

        continue;
      }
    }

  }
/////////////////////////////////////////////////////////////////////////
} // End of class
 
I'm trying to say that the condition will remain true no matter what, I did this because I thought a loop cannot be run without a condition. I'm not getting output at all, I can't even get to the stage where I get something out of it
 
You should be getting something like this:

Enter the value of a:
3
Enter the value of b:
5
Enter 1 for addition
2 for subtraction
3 for multiplication
or 4 for division
1
The sum is 8
.
.
.

If that's not what you're getting, please include a screen shot of a sample run of your code.
 
Have you been able to get any other java code to run? It's been many years since I wrote any java code, so I can't offer much help on getting things set up correctly.

The only thing I notice is that your main method is private. I believe you should change private to public.
 
Yes I've been able to run codes, I changed the main method and rewrote the code, it's working now, thank you.

Can you tell me why I had to change it to public ?
 
Darkstar3000 said:
Can you tell me why I had to change it to public ?

Because that's what the Java language specification says. The main method must have the signature

Code:
public static void main(String[] args)

You can write as many other functions called "main" as you want in as many classes as you want (though doing that is more likely to be confusing than really necessary), but Java knows which main() method is meant to start executing the application from that unique signature.
 
AlephZero said:
Because that's what the Java language specification says. The main method must have the signature

Code:
public static void main(String[] args)

You can write as many other functions called "main" as you want in as many classes as you want (though doing that is more likely to be confusing than really necessary), but Java knows which main() method is meant to start executing the application from that unique signature.

Thank you :)
 

Similar threads

Replies
7
Views
2K
Replies
12
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top