Using String name = input.next(); then making an If statement [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I was wondering how I can fix this:
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name == Donald)
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}
I want to make it so that if the user inputs a specific name, then it will say something specific, anything else and it says go away.
I am a new student of Java and was messing around to see if this is possible

If I understand correctly strings are immutable and are frequently reused, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. That means you can't just ask if string1 == string2 because they might be separate instances. So you want to check with string.equals(string2) to 'see if the content is the same'.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.next();
if (name.equals("Donald"))
System.out.println("Welcome back Admin");
else
System.out.println("Go Away");
}
}

Related

Check If User Inputs Specific Data [duplicate]

This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 1 year ago.
I would like to check if the user has input a specific word/alphabet so that I can run some code on the basis of what s/he inputs.
This is my code:
import java.util.concurrent.ThreadLocalRandom;
import java.util.Scanner;
public class EligibilityTest {
public static void main(String[] args) {
Scanner temperature = new Scanner(System.in);
System.out.println("Press the 'T' key to check your temperature. ");
double tempMeasure = ThreadLocalRandom.current().nextDouble(96, 98.9);
// Here I would like to add an if statement to check if the user has input the 'T', and if yes, print the tempMeasure
}
}
How do I construct this if statement, and how do I keep the decimal value for variable 'tempMeasure' in two digits? I find Java inputs pretty complex, since I started learning after Python, which is relatively much easier.
Yoy need to add
scan.nextLine()
to retrieve input from user. And then you can write if statement:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Press the 'T' key to check your temperature. ");
double tempMeasure = ThreadLocalRandom.current().nextDouble(96, 98.9);
String temperature = scan.nextLine();
if(temperature.equals("T")){
System.out.println(tempMeasure);
}
}

Using Strings in a If, else statements JAVA [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I'm trying to make a program that can read whatever the user inputs and checks their input using if..else statement.
import java.util.Scanner;
class Answers{
public void FalseAnim() {
System.out.println("Game Over your answer is wrong. try again!");
}
public void CorrectAnim() {
System.out.println("your answer is correct");
}
}
public class quizgame {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
Answers ans = new Answers();
String strans1;
System.out.println("welcome to the quiz game!");
System.out.println("what is 1+1");
strans1 = input.nextLine();
if (strans1=="two"||strans1=="2") {
ans.CorrectAnim();
}
else {
ans.FalseAnim();
}
}
}
every time I run the program and input anything it goes straight into the else statement, even when I input either a "2" or a "two"
if ("two".equals(strans1)||"2".equals(strans1))
will work.
In your code, you are comparing references and not the value. Hence it is returning false either way.

Use Scanner for many different methods [duplicate]

This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 5 years ago.
I'm new to Java so I'm practicing with some simple code. I want to create some methods and use Scanner for all of them, but I don't want to declare Scanner every time I create other new methods.
So here is my code. Is this correct? Are there better ways to do this?
import java.util.*;
public class TripPlanner {
public static final Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Greeting();
}
public static void Greeting() {
System.out.println("Welcome to Vacation Planner");
System.out.print("What is your name? ");
String name = input.nextLine();
System.out.print("Nice to meet you " + name +", where are you travelling to? ");
String place = input.nextLine();
System.out.println("Great! " + place + " sounds like a great trip" );
System.out.println("************");
System.out.println("\n");
}
}
Yes, that will work for different methods in the same thread.
It is important to note, however, that unless you synchronize access to the scanner, you cannot use it in different threads
If you need it in different threads then make one for each thread

How do i save next string in scanner? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Iam making a basic calculator with 3 functions. Iam using a Scanner-utility to help me interact with the calculator. You enter the first number, then it saves it to a variable, You enter a second number and that is also saved to a variable. Then after that it asks you what you want to do with the numbers. You can choose between Multiply, Add, Subtract. But none of them seem to work, and i dont know why, but iam guessing that the String that is inserted after the question isnt saved or maybe iam using wrong method.
import java.util.Scanner;
public class MainClass {
private static final String Multiply = null;
private static final String Add = null;
private static final String Subtract = null;
public static void main(String[] args)
{
MainClass mainObject=new MainClass();
mainObject.Calculator();
}
public void Calculator()
{
double firstnumber;
double secondnumber;
double result;
String word1="Multiply";
String word2="Add";
String word3="Subtract";
Scanner scan=new Scanner(System.in);
System.out.println("Enter first number");
firstnumber=scan.nextInt();
System.out.println("Enter second number");
secondnumber=scan.nextInt();
System.out.println("What do you want to do?");
String operator=scan.next();
if(operator==word1)
{
result=firstnumber*secondnumber;
System.out.println(result);
}
else if(operator==word2)
{
result=firstnumber+secondnumber;
System.out.println(result);
}
else if(operator==word3)
{
result=firstnumber-secondnumber;
System.out.println(result);
}
}
}
The comparison operation between the strings is not == but the equals() method:
if(operator.equals("add"))
...
else if (operator.equals("subtract"))
...
else if (operator.equals("multiply"))
...
else
wrong input

Need to fix program in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want user to input text while it is not equal to "start".When it is equal to "start" I want to show "Bravo".In my code when I enter "start" it just continue to ask to input a text.What is missing in my code to process the operation i described.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(komanda != "start");
System.out.println("Bravo");
}
}
You have to use the equals method to compare strings in java:
while (!komanda.equals("start"));
or even better
while (!"start".equals(komanda));
this does not crash if komanda is null
See How do I compare strings in Java? for more information.
do it this way
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(!"start".equals(komanda));
System.out.println("Bravo");

Categories

Resources