Results 1 to 15 of 15
  1. #1
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Need help with my code

    Hey guys,

    Im completely new to coding. I literally started about 3 weeks ago. I haven't done computer science yet so bear with me :)


    Java Code:
    import java.util.Scanner;
    
    public class MainClass {
    	public static void main(String[] args){
    	
    		Scanner sc = new Scanner (System.in);
    		boolean input = sc.nextLine() != null;
    		System.out.println(input);		
    			if (input = "Hello my friend" != null){
    					System.out.println("Ahh, hello there!");
    			
    			
    			
    			}
    		
    	}
    }
    This code works but i dont know why.. A bunch of errors showed up before and told me to add the two "!= null". Also I had the variable "input" as a String instead of a Boolean. When i tried to switch it to a boolean before the two "!= null", it said i wasn't aloud to do that.. Can anyone explain this to me? Any help would be appreciated.
    Last edited by sunde887; 10-02-2011 at 07:07 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Need help with my code

    sc.nextLine() returns a string, not a boolean. What happens if someone types in "blah", how can that be a boolean value(true/false). If you intend to get a string variable, store it in a String variable. In your code, input is a boolean, "Hello my friend" != null returns true as long as the string is not null. If this makes sense--good. If not ask me to clarify, and I will.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need help with my code

    boolean input = sc.nextLine() != null;
    Instead of trying to do two things in one statement, break this statement into two simple statements.
    1) get the input
    2) test it

  4. #4
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    Thanks for the help guys :) i changed the variable to a string and then it underlines "input = "Hello my friend" and says i cannot covert it
    from a string to a boolean. Can i just simply not have an if statement for a String variable? Is there anyway to go around that? For example, i want the console to respond to the user if he says a certain thing. Sorry if im not making sense.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Need help with my code

    Check out the .equals(Object o) method. String (Java Platform SE 6)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Need help with my code

    Can you post the code with the problem?

    The following statement has a boolean on the left and a String on the right
    Java Code:
    boolean input = "Hello my friend";
    That doesn't work. You need
    either a String variable on the left
    or a boolean expression on the right.

  7. #7
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    Java Code:
    import java.util.Scanner;
    
    public class MainClass {
    	public static void main(String[] args){
    	
    		Scanner sc = new Scanner (System.in);
    		String input = sc.nextLine();
    		System.out.println(input);		
    			if ([U]input = "Hello my friend"[/U]){
    					System.out.println("Ahh, hello there!");
    			
    			
    			
    			}
    		
    	}
    }
    Last edited by sunde887; 10-02-2011 at 08:14 PM. Reason: Added code tags, [code]...[/code]

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Need help with my code

    When posting code, please use code tags: [code] YOUR CODE HERE [/code]

    Also, '=' is an assignment operator, comparisons is done with '==' and strings need to be compared with .equals(Object o)

  9. #9
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    Is there something different besides a string or boolean that i can use? I looked at the .equals (Object o) method. I dont really understand that. It said it was a "superclass" or something...

  10. #10
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    oh sorry about that :/ i was wondering how to do that haha

  11. #11
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    Ohhhh hold on i think i know how to fix it..

  12. #12
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    Alright i fixed it all i had to do was change it from '=' to '=='. Thanks guys!

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Need help with my code

    That is still actually incorrect. For primitives it is fine to use '==' but for strings this compares references--which is not what you want. Instead use equals.
    Java Code:
    string1.equals(otherString)
    Will return true if the two strings are equal.

  14. #14
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    Hold on ill try that right now

  15. #15
    JakeEdwards27 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help with my code

    Ok i got it.. I'll look into that area more. Thanks for the help!

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Tags for this Thread

Posting Permissions

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