Results 1 to 11 of 11
  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Input String to boolean equation

    I was trying to figure out how to work with CASE_INSENSITIVE, but that failed. So I decided to break it down and simply write an if for "n" and "N".

    PROBLEM:
    The code compiles fine as is. But all I ever get is the "Liar".
    I don't seem to understand why if(ready == "N" ... or ... "n") doesn't call their respective println()
    Am I not allowed to throw a String to a boolean equation?


    Java Code:
    import java.util.Scanner;
    
    public class Choice{
      public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        System.out.println("Type the letter (N).");
         String ready = input.nextLine();
    
         if(ready == "n"){
          System.out.println("CAPS are OFF");
        }
         else if(ready == "N"){
          System.out.println("CAPS are ON");
        }
         else{
          System.out.println("Liar");
      }
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Input String to boolean equation

    Java Code:
    ready == "n"
    Don't compare instances of String (or any other class) with ==, instead use the equals() method.

    All classes implement the equals() method precisely so that they can determine what it is for two instances to be the "same". Count the number of words in the previous sentence. Do you get 22 or 21? The problem is that "the" occurs twice: is it to count as one word or two? In the String class, equals() is implemented so that if two strings are made up of the same characters in the same order equals() will return true.

  3. #3
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Re: Input String to boolean equation

    A. Is this the proper implementation of equals() ...
    B. I know this is a very basic question, but I for the life of me am clueless right now. How would I make the program run again after it prints the proper println();

    Java Code:
    import java.util.Scanner;
    
    public class Choice{
      public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        System.out.println("Type the letter (N).");
         String ready = input.next();
    
         if(ready.equals("n")){
          System.out.println("CAPS are OFF");
        }
         else if(ready.equals("N")){
          System.out.println("CAPS are ON");
        }
         else{
          System.out.println("Liar");
      }
    }
    }
    Thank you so much.
    Last edited by rru96; 09-22-2012 at 08:11 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Input String to boolean equation

    Quote Originally Posted by rru96 View Post
    A. Is this the proper implementation of equals()
    That's not an implementation. It's a method call, or method invocation. As to whether it's proper: does it do what you intended it to do?

    There's already a class named Choice in the standard JDK. As it's considered bad practice to duplicate names of JDK classes, you should change that class name.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Input String to boolean equation

    A. Compile it and run it. Does it do what you expect?

    Once you have that sorted out you can think about how to make it "run again".

  6. #6
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Re: Input String to boolean equation

    Java Code:
    import java.util.Scanner;
     
    public class ConGame{
      
      public static void main(String[] args){
        int x = 0;
        while(x==0){
        Scanner input = new Scanner(System.in);
        System.out.println("Hello there.");
        System.out.println("What is your name?");
        String name = input.next();
        System.out.println("Do you want to continue " + name + "? (y) or (n)");
        String ready = input.next();
     
         if(ready.equals("n") || ready.equals("N")){
           x = 1;
          System.out.println("Sorry you don't want to play again.");
          System.out.println("Have a great day!");
          break;
    
        }
         else if(ready.equals("y") || ready.equals("Y")){
          x = 0;
          System.out.println("Back to the beginning...\n\n");
    
        }
    }
    }
    }
    Thank you guys so much. Once again, not sure if I did it the best way, but it works!
    Last edited by rru96; 09-22-2012 at 01:33 PM. Reason: code edit

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Input String to boolean equation

    String has a method that tests equality while ignoring the case. Read the API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Re: Input String to boolean equation

    If you are talking about CASE_INSENSITIVE, I looked through the API, but how to 'implement' what I am reading into real code is still a little awkward to me.

  9. #9
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Re: Input String to boolean equation

    I even tried to figure out how to use (?i) but that didn't go over so well either

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Input String to boolean equation

    Did you see this in the API?

  11. #11
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Re: Input String to boolean equation

    Thank you guys. PBRockway - I did not, though I see now. I was stuck on the CASE_INSENSITIVE I found and the (?i). The API seems like such an overwhelming document, and without having found a great way to search through it, I am trying to use the index. That hasn't always been working out so well. I did read the link though. Thanks again.

Similar Threads

  1. Replies: 2
    Last Post: 11-20-2011, 09:33 PM
  2. Replies: 3
    Last Post: 09-25-2011, 09:56 AM
  3. convert boolean to string
    By newbiejava in forum New To Java
    Replies: 3
    Last Post: 07-22-2010, 03:32 PM
  4. Limiting loop in boolean string
    By jimmy-lin in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 06:31 AM
  5. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 05:31 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
  •