-
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.
-
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?
-
Re: Help me with my code (im learning Java)
Quote:
make a new string that takes the value of the scanner
You did it in post #20
Code :
String uinputvar = uinput.nextLine();
-
Re: Help me with my code (im learning Java)
My code at the moment:
Code :
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)
-
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.
-
1 Attachment(s)
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:
http://i45.tinypic.com/674ec4.png
-
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
Quote:
my cursor says its loading... then nothing happens
Is the program waiting for you to enter some data?
-
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."
-
Re: Help me with my code (im learning Java)
Quote:
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?
-
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...
-
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.
-
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."
-
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"?
-
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>?
-
Re: Help me with my code (im learning Java)
Use an assignment statement to save the value returned by a method:
Code :
aVar = someMethod(); // save value returned by someMethod() in aVar
-
Re: Help me with my code (im learning Java)
And someMethod(); is? (what shall i enter for it?)
-
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.
-
Re: Help me with my code (im learning Java)
-
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?
-
Re: Help me with my code (im learning Java)
Quote:
Assign that variable the value returned by the method that has read the user's input.
how?
-
Re: Help me with my code (im learning Java)
Use an assignment statement:
Code :
theVar = aMethodThatReturnsValue();
What variable is to receive the value?
What method is called to get the user's input?
-
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)
-
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.
-
Re: Help me with my code (im learning Java)
1- dont give semicolon(;) after if condition
2- leave space } else in 7th line line
-
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...