# Homework Help: Why won't my loop run ?

1. Jan 2, 2012

### Darkstar3000

I tried to write this program but the loop just won't run, am I missing something ?

Code (Text):
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;

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

2. Jan 2, 2012

### Staff: Mentor

Your code would be easier to read if it were indented properly. I have done this below.
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.

3. Jan 3, 2012

### Darkstar3000

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. Jan 3, 2012

### Staff: Mentor

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.

5. Jan 3, 2012

6. Jan 3, 2012

### Staff: Mentor

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. Jan 8, 2012

### Darkstar3000

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. Jan 8, 2012

### AlephZero

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

Code (Text):
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. Jan 8, 2012

Thank you :)