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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 53

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

  1. #26
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

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

    This is basically a repetition of what Norm had been saying. - For your string.equals method, you're comparing "Sammy" to "Sammy" so it would alway print out "Hello Sammy" regardless of what you input. Next, you assign "guest" to the user input but that it. I believe you are trying to compare the user input to "Sammy". In that case, make a new string that take the value of the scanner "guest" and then use it with the .equals method.

  2. #27
    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)

    How do i make a new string that takes the value of the scanner "guest" and then use it with the .equals method?

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    make a new string that takes the value of the scanner
    You did it in post #20
    	String uinputvar = uinput.nextLine();
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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)

    My code at the moment:

    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();
    		}
    	}
    }

    Please fix my code (i dont understand what you guys mean :p)

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    What happens when you compile and execute the code?

    Look at the statements in the program and think about what the computer will do as each statement is executed.
    Make a list of the steps you want the program to do. Make each step as simple as possible, like you were explaining to a child what the program is to do.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    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 compile the code and my cursor says its loading... then nothing happens. (I compile + run with eclipse.)

    So this is what i want my code to do:
    Attached Images Attached Images

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    Please write the steps as descriptions of what is to be done, not as comments in the program.

    For example: Instructions on How to walk across the floor
    Begin loop
    Shift weight to left foot
    Raise right foot
    move right foot forward
    put right foot on the floor
    shift weight to right foot
    lift left foot
    move left foot forward
    put left foot on floor
    Are we close to the wall?
    No, go to start of loop
    Yes exit loop

    my cursor says its loading... then nothing happens
    Is the program waiting for you to enter some data?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    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)

    No, It does not give me a box to enter the data..

    --- Update ---

    This is what i want the program to do:

    1. Wait for the user input from the scanner
    2. If the user input is the word "Sammy" then say "Hello Sammy"
    3. If the user input is something else than sammy, say "Sorry, but you are not Sammy."

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    1. Wait for the user input from the scanner
    2. If the user input is the word "Sammy" then say "Hello Sammy"
    3. If the user input is something else than sammy, say "Sorry, but you are not Sammy."
    Where do you ask the user to enter his name?
    Where do you save what the user entered?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    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. Well ideally, i would like to import the JOptionPane from javax.swing.JOptionPane and let the user import it from there
    2. Im not sure, u can tell me what/how to do this...

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    Please work on one problem at a time. Make the list of what the program is supposed to do and then write the code to do that. Later change the code to use a different class and method.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    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 what i want the program to do

    1. Wait for the user input from the scanner/JOptionPane Dialogue Box
    2. If the user input is the word "Sammy" then say "Hello Sammy"
    3. If the user input is something else than sammy, say "Sorry, but you are not Sammy."

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    Where is the user's input saved in step 1?

    What is the java statement for comparing two Strings: the user's input and "Sammy"?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    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 dont know how to save the user input in step 1
    2. Im not sure, can u tell me>?

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    Use an assignment statement to save the value returned by a method:
    aVar = someMethod(); // save value returned by someMethod() in aVar
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    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)

    And someMethod(); is? (what shall i enter for it?)

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    What class and method are you using to read the input from the user?
    Change the code I posted to use:
    the variable in your code that is to hold the user's input
    and the method that you are using to read the user's input.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    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)

    How?

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    Define a String variable
    Assign that variable the value returned by the method that has read the user's input.

    What method are you using to read the user's input?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    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)

    Assign that variable the value returned by the method that has read the user's input.
    how?

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    Use an assignment statement:
    theVar = aMethodThatReturnsValue();

    What variable is to receive the value?
    What method is called to get the user's input?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #47
    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 really dont understand this...
    Can someone just edit my code and leave comments on each line? (Im a **** noob at java)

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,108
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

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

    Perhaps you should consider dropping the course. Are you expecting someone to write every program that you are assigned? You need to learn how to do it. Otherwise why take the course.

    You are having problems with some very basic concepts:

    What is a variable
    How to assign a value to a variable
    How to call a method
    How to assign the value that the method returns to a variable.

    You will be using these concepts over and over and over when writing programs.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #49
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    1- dont give semicolon( after if condition
    2- leave space } else in 7th line line

  25. #50
    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 know what a variable is
    i don't fully know how to assign a value to a variable
    i dont know how to call a method
    i dont know how to assign the value that the method returns to a variable...

Page 2 of 3 FirstFirst 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, 05:05 AM

Tags for this Thread