This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
So I wrote up a code that will make the user enter the name "Jack" but if he enters any other name it will say "That is not your name!" but i'm having trouble to make it do that. For example when I enter that name "Jack" It will say "That is not your name!" and when I enter a name like Joey for example it will say the same thing.
import java.util.Scanner;
public class WhoAreYou
{
public static void main(String[] args){
Scanner user_input = new Scanner(System.in);
String vYourName, vNotYourName;
System.out.print("Enter your name here: ");
vYourName = user_input.next();
/*
String vNotYourName;
System.out.print("Enter your name here: ");
vNotYourName = user_input.next();
*/
if(vYourName == "Jack")
System.out.println("Your name is: "+vYourName);
else{
vNotYourName = " ";
System.out.println("Thats not your name!");
}
}
}
Instead if using two Strings I tried to use one and that didn't work. Any ideas?
use .equals() when comparing strings:
if(vYourName.equals("Jack"))
System.out.println("Your name is: "+vYourName);
else{
"".equals(vYourName);
System.out.println("Thats not your name!");
}
instead of:
if(vYourName == "Jack")
use this:
if(vYourName.equals("Jack"))
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
import java.util.*;
public class Test {
public static void main(String[] args) {
ArrayList<String> name = new ArrayList<String>();
String cond="y";
do {
Scanner n = new Scanner(System.in);
System.out.println("enter the name: ");
String value = n.nextLine();
name.add(value);
Iterator itr = name.iterator();
while(itr.hasNext()) {
System.out.println("Names on the list: "+itr.next());
}
System.out.println("Want to continue? (Y/N): ");
cond=n.nextLine();
} while(cond=="Y");
}
System.out.println("End of Program");
}
This is the code to get the Names and store them in the list. Then it must print all those values stored in the list. Then it must ask the user whether they have some more input, if "yes" then it must get the value, store it, display all names.
But it is not working in the while loop. Can anyone explain me this?
Expected output:
Enter the name: xyz
Names on the list: xyz
Want to continue? (Y/N): Y
Enter the name: abc
Names on the list: xyz,abc
Want to continue? (Y/N): N
End of Program
The "==" doesn't compare the values of the string in Java user equalsIgnoreCase / equals
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I have made a JAVA program where I have initialized a 1-D String array. I have used for loop to search any inputted String if it exists in the array(Scanner Class).
Here is the source code :-
import java.util.*;
class search
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name to search :-");
String s=sc.nextLine();
String array[]={"Roger","John","Ford","Randy","Bacon","Francis"};
int flag=0,i;
for(i=0;i<6;i++)
{
if(s==array[i])
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("The name "+s+" Exists");
else
System.out.println("The name "+s+" does not Exists");
}
}
The class even compiles successfully, but when I enter a valid string(say- Roger), the output is The name Roger does not Exists.
Please help me out with this issue, and for this I shall be grateful to you.
Thanking You,
J.K. Jha,
01.09.2018.
You are confusing == and equals
Since String is an object == just checks for if the references are same instead of actual contents
You should use String.equals() instead
Changes your if condition
for(i=0;i<6;i++)
{
if(s.equals(array[i]))
{
flag=1;
break;
}
}
This question already has answers here:
Validating input using java.util.Scanner [duplicate]
(6 answers)
Closed 7 years ago.
I want to make a while loop so that whenever a user inputs a blank input, it will re-ask the question until it is not empty. So far, I have this:
Scanner scn = new Scanner(System.in);
while (//user input is not blank) {
System.out.print("Enter id: ");
int id = scn.nextInt();
System.out.print("Enter name: ");
String last_name = scn.next();
System.out.print("Enter phone: ");
String first_name = scn.next();
scn.close();
break;
}
i'm pretty sure i'm over thinking this but i'm not sure of the syntax or the functions.
You should expect user to type say Quit/quit to quit rather than empty string.
You should close scanner out of loop without break.
You should use do...while loop instead if while loop. Something like:
do {
...
exit = scn.next();
} while (!exit.equalsIgnoreCase("quit"));
scn.close();
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(25 answers)
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 4 years ago.
So this is the code I am using:
System.out.println("Create a name.");
name = input.nextLine();
System.out.println("Create a password.");
password = input.nextLine();
But when it reaches this point it just says "Create a name." and "Create a password." both at the same time and then I have to type something. So it's basically skipping the Scanner parts where I need to type a String. After "Create a name." and "Create a password." is outprinted and I type then, both name and password are changing to what I typed in. How do I fix this?
This is the full class. I am just testing so it isn't actually going to be a program:
package just.testing;
import java.util.Scanner;
public class TestingJava
{
static int age;
static String name;
static String password;
static boolean makeid = true;
static boolean id = true;
public static void main(String[] args){
makeid(null);
if(makeid == true){
System.out.println("Yay.");
}else{
}
}
public static void makeid(String[] args){
System.out.println("Create your account.");
Scanner input = new Scanner(System.in);
System.out.println("What is your age?");
int age = input.nextInt();
if(age<12){
System.out.println("You are too young to make an account.");
makeid = false;
return;
}
System.out.println("Create a name.");
name = input.nextLine();
System.out.println("Create a password.");
password = input.nextLine();
return;
}
}
And sorry for my bad grammar. I am not English so it is kinda hard for me to explain this.
The nextInt() ate the input number but not the EOLN:
Create your account.
What is your age?
123 <- Here's actually another '\n'
so the first call to nextLine() after create name accept that as an input.
System.out.println("Create a name.");
name = input.nextLine(); <- This just gives you "\n"
User Integer.parseInt(input.nextLine()) or add another input.nextLine() after reading the number will solve this:
int age = Integer.parseInt(input.nextLine());
or
int age = input.nextInt();
input.nextLine()
Also see here for a duplicated question.
This doesnt actually tell you why the lines are being skipped, but as you're capturing names and passwords you could use Console:
Console console = System.console();
String name = console.readLine("Create a name.");
char[] password = console.readPassword("Create a password.");
System.out.println(name + ":" + new String(password));
You can also use next() instead of nextLine(). I have tested it in eclipse. its working fine.
next() will work but it will not read the string after space.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm working on a small program that asks for your name using a Scanner. If you enter blankstring, then I would like the console to display a message.
Here's what I tried doing:
import java.util.Scanner;
public class Adventure
{
public static void main(String[] args)
{
Scanner myScan = new Scanner (System.in);
System.out.println("What's your name?");
String name = myScan.nextLine();
while (!(name == "")) //Always returns false.
{
System.out.println("That's not your name. Please try again.");
name = myScan.nextLine();
}
System.out.println("It's a pleasure to meet you, " + name + ".");
}
}
The code never enters the while loop. Why?
Change your condition to:
while(!name.equals("")) {
or as suggested below by m0skit0:
while(!name.isEmpty()) {
See also
why equals() method when we have == operator?