-
Help me with my code (im learning Java)
Code :
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)
Code :
Syntax error on token "else", delete this token
and how can i fix my code?
-
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 ;
-
Re: Help me with my code (im learning Java)
Thanks for this, however i tried entering my new code:
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 =)
-
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()
Code :
if (entry.equals(name)) ...
Class names usually start with a capital letter.
Don't forget to 'close' the scanner after use.
-
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.
-
Re: Help me with my code (im learning Java)
Quote:
Originally Posted by
Starstreak
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()
Code :
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
--- Update ---
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 (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.
-
Re: Help me with my code (im learning Java)
Quote:
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?
-
Re: Help me with my code (im learning Java)
I read your post but how will i create a new variable for the input?
-
Re: Help me with my code (im learning Java)
Quote:
how will i create a new variable
Just like you create any variable. Here you create two new variables: name and scanner1
Code :
String name = "Sammy";
Scanner scanner1 = new Scanner (System.in);
-
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.
-
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 =]
-
Re: Help me with my code (im learning Java)
Code :
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.
-
Re: Help me with my code (im learning Java)
Ok, so i edited the code a bit and it now looks like this:
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?");
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:
Code :
Hello, What is your name?
Test
Hello Sammy
It should turn out as
Code :
"Sorry but you are not "+ name;
Please help me and tell me how to fix it =]
-
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"; so this test should always be true:
Code :
if (name.equals("Sammy"))
The code should compare what is read in from the user to what is in name.
-
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"
-
Re: Help me with my code (im learning Java)
Code :
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?
-
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:
Code :
Scanner guest = new Scanner (System.in);
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.
-
Re: Help me with my code (im learning Java)
Quote:
Originally Posted by
Norm
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:
Code :
Scanner guest = new Scanner (System.in);
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.
-
Re: Help me with my code (im learning Java)
When a method returns a value the code should save that value in a variable:
Code :
String aVar = methodThatReturnsAString();
aVar now has the String that the method returned.
-
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 :P, please edit my code and 'fix it' and add comments on each line so i can learn :P e.g //creates a variable to store (name)
Thanks =)
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();
}
}
}
-
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: 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?
-
Re: Help me with my code (im learning Java)
Fix this code instead:
Code :
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:
Code :
Hello, What is your name?
Test
Hello Sammy //This should be ''Sorry but you are not "" + name;
-
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: 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?
-
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;
-
Re: Help me with my code (im learning Java)
Quote:
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.
Quote:
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:
Code :
x = 23; // give the value 23 to the variable x
The variable name gets a value here: 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:
That code compares the contents of the name variable against the contents of the name variable.
When would that ever be not the same?