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 5 of 5

Thread: String or Boolean help

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String or Boolean help

    Ok, so i hope i am not going against any rules by posting here... i read and re-read the rules, so if i accidently break one, please let me konw.

    I did search google and stuff for this answer, but none of them post or tutorials i watched showed me what i needed.

    Ok, so i am trying to make a class (program?) that takes what the user inputs and replies to it.

    so i have

    import java.util.Scanner; //so i can use the scanner
     
    public class ScannerMain {
     
    	public static void main (String args[]){
     
    		Scanner wordscanner = new Scanner(System.in); //setting the scanner
     
    		boolean con = true; //i am just using this to keep the string repeating after they input, so they can reply to what the computer outputs
     
     
    	    while (con = true) {
    	      String answer;
    	      System.out.print("Reply:");
    	      answer = wordscanner.next(); //mostly self explanitory
     
     
    	      if (answer = "hi"){  /*here is where i get the error.... it says unable to convert String to boolean i tried changed the answer to a boolean, or copying the value of the boolean to a new variable.. but that didn't work. also, i think i may have to word the "hi" differently, what im trying to do with that: is seeing if the user had wrote hi, and if so reply with hello*/
     
     
                  System.out.println("Hello");  
    	      }else{
     
    	    	  System.out.println("I do not reconize your statement");
    	      }
    	    }
    	  }
    	}


    Again, i did try converting the string to a boolean, but i couldn't seem to get that to work.



    Code Without any comments (so you might be able to read the actual code better)

    import java.util.Scanner;
     
    public class ScannerMain {
     
    	public static void main (String args[]){
     
    		Scanner wordscanner = new Scanner(System.in);
     
    		boolean con = true;
     
     
    	    while (con = true) {
    	      String answer;
    	      System.out.print("Reply:");
    	      answer = wordscanner.next();
     
     
    	      if (answer = "hi"){
     
    	    	System.out.println("Hello");  
    	      }else{
     
    	    	  System.out.println("I do not reconize your statement");
    	      }
    	    }
    	  }
    	}

    The only error I'm getting is at the "unable to convert String to Boolean"
    Last edited by __NightWhisper__; May 17th, 2012 at 02:10 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: String or Boolean help

    one = is 'assignment', it means copy the value on the right to the variable on the left. The type of an 'x = y' expression is the type of x, so String x = "a letter y" is an expression with a String type.
    two =, like == is 'test for equality', compare the left side to the right side and return boolean true if they're the same, boolean false otherwise. It doesn't matter what type x and y are in x == y, the type of the expression is boolean.
    If statements are if ([boolean_expression]) - no other type of expression is allowed.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String or Boolean help

    Quote Originally Posted by Sean4u View Post
    one = is 'assignment', it means copy the value on the right to the variable on the left. The type of an 'x = y' expression is the type of x, so String x = "a letter y" is an expression with a String type.
    two =, like == is 'test for equality', compare the left side to the right side and return boolean true if they're the same, boolean false otherwise. It doesn't matter what type x and y are in x == y, the type of the expression is boolean.
    If statements are if ([boolean_expression]) - no other type of expression is allowed.
    i am ashamed, i have watched so many tutorials, and actually used that before (the ==) my brain was out of it, thank you

    ok, now i don't know if this is the way i worded the code or not, but no-matter what i type, it still goes with the second argument (I do not reconize that statement)

    Here is my updated code:

    import java.util.Scanner;
     
    public class ScannerMain {
     
    	public static void main (String args[]){
     
    		Scanner wordscanner = new Scanner(System.in);
     
    		boolean con = true;
     
     
    	    while (con = true) {
    	      String answer;
    	      System.out.print("Reply:");
    	      answer = wordscanner.next();
     
     
    	      if (answer == "hi"){
     
    	    	System.out.println("Hello");  
    	      }else{
     
    	    	  System.out.println("I do not reconize your statement");
    	      }
    	    }
    	  }
    	}


    Thanks for your help!!

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: String or Boolean help

    == compares the left hand side with the right hand side. answer's 'type' is a String reference - it's a reference that you assigned to a (reference to a) String object returned from your Scanner's next() method. "hi" is another, completely separate String object which has its own reference. The two references are not the same. '==' is doing the very shallowest of comparisons between the two reference types. It is asking "is the left hand side referring to the same object that the right hand side is referring to?". If you want to do something deeper (like compare the content of the two objects to see if they hold the same sequence of characters), you'll need to use the equals() method. Read the API documentation for Object.equals and String.equals

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: String or Boolean help

    Keep in mind that the == operator will only work for primitive data types (char, int, double, float...ect.). When comparing Objects, in this case Strings, you should use the equals() method provided by the Object class.

Similar Threads

  1. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  2. Operator || cannot be applied to java.lang.String, boolean
    By djl1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 25th, 2011, 06:00 PM
  3. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  4. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM
  5. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM