Results 1 to 9 of 9
  1. #1
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Exclamation While loop not working with OR operator

    this line is not working as I am wanting to "while(!unit.equalsIgnoreCase("f") || (!unit.equalsIgnoreCase("c")))". when I take out the ! not equals it works fine. but when i put it back in even when I enter f or c it still repeats the loop. I'm assuming that I am setting it up wrong? The rest of my program works fine with the !equals statements. Thanks (I know its not very clean at the moment, work in progress)



    Java Code:
    import java.util.Scanner;
    
    public class CS123Ass31ID1832
    {
      public static void main(String[] args)
      {
        double temperature;
        double degrees_C =0;
        double degrees_F = 0;
        String unit = "";
        
        
        while(!unit.equalsIgnoreCase ("q"))
        {
        
        Scanner keyboard = new Scanner(System.in);
        System.out.println("If you want to convert Fahrenheit into Celsius please enter a F");
        System.out.println("If you want to convert Celsius into Fahrenheit please enter a C");
        
        unit = keyboard.nextLine();
        
        while(!unit.equalsIgnoreCase("f") || (!unit.equalsIgnoreCase("c"))) 
        {
             System.out.println("Error: you have entered a invalid Letter, please enter a valid letter");
             System.out.println("If you want to convert Fahrenheit into Celsius please enter a F");
        System.out.println("If you want to convert Celsius into Fahrenheit please enter a C");
        
        unit = keyboard.nextLine();
           
           }
        
        
        System.out.println("now enter the temperature you want converted");
        temperature = keyboard.nextDouble();
        
       
       
        
        
        if(unit.equalsIgnoreCase("f"))
             {
          degrees_F = temperature;
          degrees_C = 5*(degrees_F-32)/9;
          System.out.println(temperature +" degrees Fahrenheit is " + degrees_C + " in Celsius");
        }
           else if(unit.equalsIgnoreCase("c"))
                     {
             degrees_C = temperature;
             degrees_F = ((9*(degrees_C)/5)+32);
             System.out.println(temperature +" degrees in Celsius is " + degrees_F + " in Fahrenheit");
        }
          
           
           System.out.println("If you want to enter a new temperature press any key, or press q to quit");
           keyboard.nextLine();
           unit = keyboard.nextLine();
           
           
        }
           
        
       
        
        
        
      }
    }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: While loop not working with OR operator

    If it works fine when you take out the "!", then why do you want to put them in?

    If you are insistent on putting them in, take a look at the following: De Morgan's laws

  3. #3
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Default Re: While loop not working with OR operator

    because for the program I need that while loop to repeat if the user input isn't f or c

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: While loop not working with OR operator

    You basically have (If unit is not f OR unit is not c). This will always be true.

  5. #5
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Default Re: While loop not working with OR operator

    but since they are putting a new value in for "unit" at the end of the loop if they put in f or c when the loop checks again it will see that it is f or c and then continue on?

  6. #6
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Default Re: While loop not working with OR operator

    ahhh I think i know what you mean now. then how would I go about changing it so that if the user doesnt even either f or c that while statement is going to continue to loop until their input is one of those letters?
    Last edited by snobbysteven; 02-05-2013 at 11:36 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: While loop not working with OR operator

    You need an AND, not an OR.
    While (NOT 'f') AND (NOT 'c')
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: While loop not working with OR operator

    Think about the logic of it all: !A || !B is true even if A or B (but not both) is true.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: While loop not working with OR operator

    And now that I'm awake...
    My above becomes:
    WHILE NOT ('f' || 'c')
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. My while loop is not working, why??
    By esined93 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 01:05 AM
  2. for loop not working?
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 07-24-2012, 02:55 PM
  3. Replies: 5
    Last Post: 01-21-2012, 11:02 PM
  4. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 10:36 PM
  5. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •