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.

Page 1 of 3 123 LastLast
Results 1 to 25 of 53

Thread: Help me with my code (im learning Java)

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Help me with my code (im learning Java)

    class ifelse {
    	public static void main (String args []) {
    		String name = "Sammy";
     
    		if (name == "Sammy"); {
    			System.out.println("Hello "+ name);
    		}else{
    			System.out.println("Sorry but you are not "+ name);
    		}
    	}
    }

    What does this mean? (below)
    Syntax error on token "else", delete this token

    and how can i fix my code?


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

    Default Re: Help me with my code (im learning Java)

    Where did the message you are asking about come from?

    The compiler found the end of the if statement at the ; after the if() and thinks there is no if statement for the else it found.

    Remove the ;
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    Thanks for this, however i tried entering my new code:

    import java.util.Scanner;
     
    class ifelse {
    	public static void main (String args []) {
    		String name = "Sammy";
     
    		Scanner scanner1 = new Scanner (System.in);
    		System.out.println("Hello, What is your name?");
    		System.out.println(scanner1.nextLine());
    		if (name == "Sammy") {
    			System.out.println("Hello "+ name);
    		}else{
    			System.out.println("Sorry but you are not "+ name);
    		}
    	}
    }

    and i want it to be: if the input for the name is not "Sammy" it will message "Sorry but you are not " + name.
    But, if the user DOES input the word "Sammy" it will send message "Hello "+ name.

    Thanks =)

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Help me with my code (im learning Java)

    Hi R0bsterz,

    You set a string variable for Sammy, but then you're using the same one for the input and comparison.

    also

    Don't compare strings with ==. Use .equals()

    if (entry.equals(name)) ...


    Class names usually start with a capital letter.

    Don't forget to 'close' the scanner after use.

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

    Default Re: Help me with my code (im learning Java)

    You need to have a separate variable that is used to receive the input from the user that is returned by the nextLine() method. The contents of that variable can be compared to the name variable.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    Quote Originally Posted by Starstreak View Post
    Hi R0bsterz,

    You set a string variable for Sammy, but then you're using the same one for the input and comparison.

    also

    Don't compare strings with ==. Use .equals()

    if (entry.equals(name)) ...


    Class names usually start with a capital letter.

    Don't forget to 'close' the scanner after use.
    Thanks for this, so to close the scanner i do

    scanner1.close();


    --- Update ---

    import java.util.Scanner;
     
    class ifelse {
    	public static void main (String args []) {
    		String name = "Sammy";
     
    		Scanner scanner1 = new Scanner (System.in);
    		System.out.println("Hello, What is your name?");
    		System.out.println(scanner1.nextLine());
    		if (entry.equals("Sammy")) {
    			System.out.println("Hello "+ name);
    		}else{
    			System.out.println("Sorry but you are not "+ name);
    		}
    	}
    }

    Will this code work?

    and i want it to be: if the input for the name is not "Sammy" it will message "Sorry but you are not " + name.
    But, if the user DOES input the word "Sammy" it will send message "Hello "+ name.

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

    Default Re: Help me with my code (im learning Java)

    Will this code work?
    compile and execute it and see what happens. Then you can tell us if the code worked.

    Did you read my post#5?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    I read your post but how will i create a new variable for the input?

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

    Default Re: Help me with my code (im learning Java)

    how will i create a new variable
    Just like you create any variable. Here you create two new variables: name and scanner1
              String name = "Sammy";
     
    	Scanner scanner1 = new Scanner (System.in);
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Help me with my code (im learning Java)

    You need one variable for the name you have in the beginning "Sammy" and a separate one that gets input by the user.

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    Sorry, but i dont understand what you mean by a seperate one input by the user, please could you add the code to my code and show me,

    Thanks =]

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

    Default Re: Help me with my code (im learning Java)

    System.out.println(scanner1.nextLine());
    Where did you see this way of coding a call to a Scanner class method inside of a println() method?
    Make two statements out of it.
    One that reads the user's input into a variable.
    Second to print that variable.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    Ok, so i edited the code a bit and it now looks like this:
    import java.util.Scanner;
     
    class ifelse {
    	public static void main (String args []) {
    		String name = "Sammy";
     
    		Scanner scanner1 = new Scanner (System.in);
    		System.out.println("Hello, What is your name?");
    		scanner1.nextLine();
    		if (name.equals("Sammy")) {
    			System.out.println("Hello "+ name);
    		}else{
    			System.out.println("Sorry but you are not "+ name);
     
    			scanner1.close();
    		}
    	}
    }

    But, when i compile and execute the code - it turns out like this:

    Hello, What is your name?
    Test
    Hello Sammy

    It should turn out as
    "Sorry but you are not "+ name;

    Please help me and tell me how to fix it =]

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

    Default Re: Help me with my code (im learning Java)

    What happens to the String returned by the nextLine() method? It should be assigned to a variable.

    The variable name is assigned the value "Sammy";
    String name = "Sammy";
    so this test should always be true:
    if (name.equals("Sammy"))

    The code should compare what is read in from the user to what is in name.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Help me with my code (im learning Java)

    Declare two variables:

    use name as the String variable for Sammy

    use guest as a variable for the name that gets read in by scanner.

    Now compare name to guest in the if/else statement.

    Can't be clearer than that ..

    At the moment, name will always be "Sammy"

  16. #16
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    import java.util.Scanner;
     
    class ifelse {
    	public static void main (String args []) {
    		String name = "Sammy";
     
    		Scanner guest = new Scanner (System.in);
    		System.out.println("Hello, What is your name?");
    		guest.nextLine();
    		if (name.equals(name)) {
    			System.out.println("Hello "+ name);
    		}else{
    			System.out.println("Sorry but you are not "+ name);
     
    			guest.close();
    		}
    	}
    }

    How do i do an if/else statement for the Scanner bit?

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

    Default Re: Help me with my code (im learning Java)

    Where does the code save the String that is returned by the nextLine() method?
    Here the code saves in guest what is returned by the Scanner class's constructor:
    Scanner guest = new Scanner (System.in);

    if (name.equals(name))
    When would this compare NOT be true? Something should always be equal to itself.
    You want to compare what is read in from the user to name.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    Quote Originally Posted by Norm View Post
    Where does the code save the String that is returned by the nextLine() method?
    Here the code saves in guest what is returned by the Scanner class's constructor:
    Scanner guest = new Scanner (System.in);

    if (name.equals(name))
    When would this compare NOT be true? Something should always be equal to itself.
    You want to compare what is read in from the user to name.
    Sorry but i really dont understand you..
    Please add what your saying to my code and i will be eternally grateful.

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

    Default Re: Help me with my code (im learning Java)

    When a method returns a value the code should save that value in a variable:
    String aVar = methodThatReturnsAString();
    aVar now has the String that the method returned.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    This is my code at the moment. Sorry Norm, but I do not understand what you are saying when you are trying to help me , please edit my code and 'fix it' and add comments on each line so i can learn e.g //creates a variable to store (name)

    Thanks =)

    import java.util.Scanner;
     
    class ifelse {
    	public static void main (String args[]) {
     
    		Scanner uinput = new Scanner (System.in);
    		String uinputvar = uinput.nextLine();
    		String name = "Sammy";
    		System.out.println("Hello, What is your name?");
    		if (name.equals(name)) {
    		System.out.println("Hello Sammy");
    		}else{
    			System.out.println("Sorry but you are not "+name);
     
    			uinput.close();
    		}
    	}
    }

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

    Default Re: Help me with my code (im learning Java)

    What happens when you compile and execute the code?
    Copy and paste the console's contents and add some comments describing what is wrong.

    What is the following statement supposed to do:
    		if (name.equals(name)) {
    That code compares the contents of the name variable against the contents of the name variable.
    When would that ever be not the same?

    In what variable is the data that has been read from the user's input?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    Fix this code instead:
    import java.util.Scanner;
     
    class ifelse {
    	public static void main (String args []) {
    		String name = "Sammy";
     
    		Scanner guest = new Scanner (System.in);
    		System.out.println("Hello, What is your name?");
    		guest.nextLine();
    		if (name.equals(name)) {
    			System.out.println("Hello "+ name);
    		}else{
    			System.out.println("Sorry but you are not "+ name);
     
    			guest.close();
    		}
    	}
    }

    I want it to be: if the input for the name is not "Sammy" it will message "Sorry but you are not " + name.
    But, if the user DOES input the word "Sammy" it will send message "Hello "+ name.

    The console outputs as:
    Hello, What is your name?
    Test
    Hello Sammy //This should be ''Sorry but you are not "" + name;

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

    Default Re: Help me with my code (im learning Java)

    The code in post#22 has the same problem as the code in post#16.

    What happened when you compiled and executed the code in post#20?

    What about what I asked in post#21. Here it is again:

    What is the following statement supposed to do:
    		if (name.equals(name)) {
    That code compares the contents of the name variable against the contents of the name variable.
    When would that ever be not the same?

    In what variable is the data that has been read from the user's input?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help me with my code (im learning Java)

    1. When i ran the code in post #20 it didnt run properly -- looks like i didnt close the scanner or somthing.
    2. i didnt understand post #21
    3. if (name.equals(name)) {
    was meant to mean: if the variable name (which equaled "Sammy") was entered, then it would say "Hello Sammy"
    Otherwise, it would say "Sorry but you are not "+name;

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

    Default Re: Help me with my code (im learning Java)

    When i ran the code in post #20 it didnt run properly
    Sorry, you'll have to explain. I don't know what "didn't run properly" means.

    meant to mean: if the variable name (which equaled "Sammy") was entered,
    Where is the data that was entered saved?
    There was code in post #20 that saved the user's input.

    A variable gets a value in an assignment statement:
    x = 23;  //  give the value 23 to the variable x

    The variable name gets a value here:
    String name = "Sammy";
    It will have that value until it is assigned a new value. It can not have two values at the same time.

    I'm lost understanding what you think this statement does:
    		if (name.equals(name)) {
    That code compares the contents of the name variable against the contents of the name variable.
    When would that ever be not the same?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. Learning Java
    By jblankinship in forum Member Introductions
    Replies: 1
    Last Post: October 14th, 2012, 04:14 PM
  2. Need some help, learning java!
    By thisguyrighthere in forum Object Oriented Programming
    Replies: 5
    Last Post: June 10th, 2012, 09:41 PM
  3. Learning Java
    By destruxion in forum Member Introductions
    Replies: 1
    Last Post: September 12th, 2011, 03:13 AM
  4. Learning Java
    By jgc1 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 6th, 2011, 06:17 PM
  5. Tips or suggestion to learn java
    By tj23 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2009, 06:05 AM

Tags for this Thread