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

  • Thread starter Darkstar3000
  • Start date
  • Tags
    Loop
In summary, the main method in Java must have the signature "public static void main(String[] args)" in order to start executing the application. Other methods can be named "main" but they will not be recognized as the starting point for the program.
  • #1
Darkstar3000
29
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
  • #2
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
 
  • #3
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
 
  • #4
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.
 
  • #6
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.
 
  • #7
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 ?
 
  • #8
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.
 
  • #9
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 :)
 

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

1. Why is my loop not running at all?

There could be several reasons for your loop not running at all. First, check if your loop is properly defined and if there are any syntax errors. Next, ensure that your loop condition is being met and that the code inside the loop is being executed. Finally, make sure that the loop is not being terminated prematurely due to a break or return statement.

2. Why is my loop only running once?

If your loop is only running once, it is likely that the loop condition is not being met after the first iteration. Check your loop condition and make sure that it is properly set up to continue running until the desired condition is met.

3. Why is my loop running indefinitely?

An infinite loop occurs when the loop condition is never met, causing the loop to continuously run. This can happen if the loop condition is incorrect or if there is no code inside the loop that changes the condition. To fix this, double check your loop condition and make sure there is code inside the loop that modifies the condition.

4. Why is my loop not executing the correct number of times?

If your loop is not executing the expected number of times, there may be an issue with your loop condition or the code inside the loop. Check that your loop condition is correctly set up to run for the desired number of iterations and that the code inside the loop is actually being executed.

5. Why is my loop not working with arrays or other data structures?

When working with arrays or other data structures, it is important to make sure that the loop is properly accessing and manipulating the elements within the structure. Check that your loop is using the correct index or iterator variable and that the code inside the loop is correctly interacting with the data structure.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
18K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top