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

  • Thread starter Thread starter Darkstar3000
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Java program that contains a loop intended for user input and arithmetic operations. Participants explore issues related to the loop's execution and the proper structure of the main method in Java.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports that their loop does not run and seeks assistance in identifying the issue.
  • Another participant suggests that the loop should run indefinitely due to its structure but asks for clarification on the specific output the original poster is receiving.
  • A participant expresses confusion about the loop's condition, indicating they believed a loop required a condition to function.
  • One participant provides an example of expected output to illustrate how the program should behave.
  • A participant questions whether the original poster has successfully run any other Java code and notes that the main method is declared as private, suggesting it should be public instead.
  • The original poster confirms they have successfully run other codes, mentions changing the main method to public, and asks for clarification on why this change was necessary.
  • A participant explains that the Java language specification requires the main method to have a specific signature to be recognized as the entry point of the application.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of the main method's signature for proper execution, but there is some uncertainty regarding the original poster's initial issue with the loop not running.

Contextual Notes

There are unresolved aspects regarding the original poster's environment and setup, which may have contributed to the initial problem with the loop not executing as expected.

Who May Find This Useful

Individuals learning Java programming, particularly those encountering issues with loops and method declarations.

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 18 ·
Replies
18
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K