Whats wrong with my looping?
Code java:
import java.io.*;
public class dowhile
{
public static void main(String[] args)
{
int START = 0;
int END = 0;
int STEP = 0;
String input = "";
char a = 'y';
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
do{
try{
System.out.print("Input Starting number: ");
input = in.readLine();
START = Integer.parseInt(input);
System.out.print("Input Ending number: ");
input = in.readLine();
END = Integer.parseInt(input);
System.out.print("Input Step (interval): ");
input = in.readLine();
STEP = Integer.parseInt(input);
}catch(IOException e)
{System.out.println("Error");}
if( START >= END )
System.out.println("Starting Should be lesser than the ending number");
if( STEP < 1 )
System.out.println("Step should be greater than 0");
do{
System.out.println( START );
START = START + STEP;
}while( START < END && STEP > 0);
try{
System.out.print("Try again? Y/N: ");
input = in.readLine();
a = input.charAt(0); }
catch(IOException e)
{System.out.println("Error");}
if(a != 'y' || a != 'Y' || a != 'n' || a != 'N'){
do{ System.out.println("Enter Y or N only");
try{
System.out.print("Try again? Y/N: ");
input = in.readLine();
a = input.charAt(0); }
catch(IOException e)
{System.out.println("Error");}
}while(a != 'y' || a != 'Y' || a != 'n' || a != 'N');}
}while(a == 'y' || a == 'Y');
}
}
Its been driving me carzy help pls :mad:
Re: Whats wrong with my looping?
What are the expected outputs, and what outputs are you actually getting?
Re: Whats wrong with my looping?
Check your logic in evaluating the yes/no entries: they will always evaluate to true regardless of the entry and result in an infinite loop.
Re: Whats wrong with my looping?
An easy way to see what the results of a large condition like you are using is to print it:
System.out.println("cond=" + (a != 'y' || a != 'Y' || a != 'n' || a != 'N'));
Put the above just before the while() statement.
Then you can work with your input and the way the above condition is coded to get the desired result.
Re: Whats wrong with my looping?
Quote:
Originally Posted by
helloworld922
What are the expected outputs, and what outputs are you actually getting?
The code is supposed to input a start an end and an interval which the program does fine... the problem is in the if(a != 'y' || a != 'Y' || a != 'n' || a != 'N') it enters the body even IF character a is equal to one of the four variables
Re: Whats wrong with my looping?
What do you see when you add this print statement to the code just before the while() test?
System.out.println("cond=" + (a != 'y' || a != 'Y' || a != 'n' || a != 'N') + " for a=" + a);
Re: Whats wrong with my looping?
Quote:
Originally Posted by
Norm
What do you see when you add this print statement to the code just before the while() test?
System.out.println("cond=" + (a != 'y' || a != 'Y' || a != 'n' || a != 'N') + " for a=" + a);
that helped ty \m/