Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Whats wrong with my looping?

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my looping?

    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
    Last edited by copeg; August 21st, 2010 at 01:58 PM. Reason: Please use the code tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Whats wrong with my looping?

    What are the expected outputs, and what outputs are you actually getting?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my looping?

    Quote Originally Posted by helloworld922 View Post
    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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);

  7. The Following User Says Thank You to Norm For This Useful Post:

    argie123sky (August 21st, 2010)

  8. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my looping?

    Quote Originally Posted by Norm View Post
    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

Similar Threads

  1. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM
  2. whats wrong with my program??
    By jrodriguo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2010, 06:16 AM
  3. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  4. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM