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);
}
}
Related
i am running a code, but i dont seem to be able to get the scanner working, anyone got an idea how?
public class verk34 {
public static void main(String[] args) {
System.out.println(Math.pow(x,2));
}
}
the thing is inserting a number on your own without altering the code.
As per my understanding, you need to add Scanner for taking input from end users and importing the java.util.Scanner statement.
Scanner in = new Scanner(System.in);
int x = in.nextInt();
First, you need to import scanner and Math
import java.util.Scanner
import java.lang.Math
Then If I am understanding correctly the problem, you need to do this in two steps :
-Instantiate a scanner and collect user input as an integer.
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the square area :");
int userValue = sc.nextInt();
The nextInt method will lock process and wait for a keyboard event before continuing. So enter a nulber with your keyboard.
-Use System.out.println with given value.
System.out.println(Math.pow(userValue,2));
Hello i'm currently a beginner in Java. The code below is a while loop that will keep executing until the user inputs something other than "yes". Is there a way to make the scanner accept more than one answer? E.g. yes,y,sure,test1,test2 etc.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String ans = "yes";
while (ans.equals("yes"))
{
System.out.print("Test ");
ans = in.nextLine();
}
}
}
Use the or operator in your expression
while (ans.equals("yes") || ans.equals("sure") || ans.equals("test1"))
{
System.out.print("Test ");
ans = in.nextLine();
}
But if you are going to include many more options, it's better to provide a method that takes the input as argument, evaluates and returns True if the input is accepted.
Don't compare the user input against a value as loop condition?!
Respectively: change that loop condition to something like
while(! ans.trim().isEmpty()) {
In other words: keep looping while the user enters anything (so the loop stops when the user just hits enter).
You are looking for a method to check whether a given string is included in a List of string values. There are different ways to achieve this, one would be the use of the ArrayList contains() method to check whether your userinput in appears in a List of i.e. 'positive' answers you've defined.
Using ArrayList, your code could look like this:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList<String> positiveAnswers = new ArrayList<String>();
positiveAnswers.add("yes");
positiveAnswers.add("sure");
positiveAnswers.add("y");
Scanner in = new Scanner(System.in);
String ans = "yes";
while (positiveAnswers.contains(ans))
{
System.out.print("Test ");
ans = in.nextLine();
}
}
}
This question already has answers here:
Scanner input validation in while loop
(3 answers)
Closed 5 years ago.
I am trying to set up a simple text tree that reads in either single-characters (y/n) or integers that correspond to a printed list (1-4). I want to know the easiest way to have the program ignore user inputs that don't correspond to the given options like so:
import java.util.Scanner;
public class simpleMenu
{
Scanner sc = new Scanner(System.in);
String choicePick;
public static void main(String[] args)
{
System.out.println("Would you like to continue? (y/n)");
choicePick = sc.next();
if(choicePick.equals("y"))
{
// The program continues.
}
else if(choicePick.equals("n"))
{
// The program closes.
}
else
{
/*
The scanner ignores the input, ideally without having to restate the question.
The program does not quit or move on until "y" or "n" is entered.
*/
}
}
}
Bonus points if you can help me implement a 'back' option that takes me to the previous choice.
While the next string is not Y or N sc.next() until it is. Then use that string.
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");
}
}
"if" statement only allows to put numbers in it.
Is there a way to make it read letters?
I'm only in my fifth lesson of Java (I study in a uni and the teacher is very slow but I want to learn things fast)
for example.
import java.util.Scanner;
public class Java {
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
int answer1;
System.out.println("Do you like Java?");
answer1 = scan.nextInt();
if (answer1 == yes)
System.out.println("Cool ~");
else
System.out.println("Ehh...");
}
}
I want to put "yes" instead of the number 5.
So if the user types "yes" it will print "correct".
P.S. I didn't find a clear answer to that in the search engine.
It's not a duplicated thread as I'm trying to find a clear answer to that.
I need a detailed explanation about it.
I'm still a beginner, using those "high tech java words" won't help me.
You need to modify your program so that your scanner to reads a String instead of an int. You can do that as:
import java.util.Scanner;
public class Java {
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
String answer1;
System.out.println("Do you like Java?");
answer1 = scan.next();
if (answer1.equals("yes"))
System.out.println("Cool ~");
else
System.out.println("Ehh...");
}
}
I used next() for this since we only want one word (token), but be aware that there are other options for reading Strings.
Notice also that I've changed the test in the condition because it's now a String. See this answer for more on comparing Strings.
You need to modify your program so that your scanner to reads a String instead of an int. You can do that as:
import java.util.Scanner; public class Java {
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
String answer1;
System.out.println("Do you like Java?");
answer1 = scan.next();
if (answer1.equals("yes"))
System.out.println("Cool ~");
else
System.out.println("Ehh...");
} }
I used next() for this since we only want one word (token), but be aware that there are other options for reading Strings.
Notice also that I've changed the test in the condition because it's
now a String. See this answer for more on comparing Strings.
Ok, what if you want the program to read both words and numbers:
Here's my program (more in depth, when you see the full thing), but this is one of 5 parts (that look a like) where I'm having the program...
public static void Gdr1() {
try {
System.out.print("[Code: Gdr1] Grade 1: %");
Scanner gdr1 = new Scanner(System.in);
Z = gdr1.next();
Z = Double.toString(Grd1);
Grd1 = Double.parseDouble(Z);
if ((Grd1<100)&&(Grd1>=5)) {
Gdr2();
} else if ((Grd1>=100)&&(Grd1<125)) {
System.out.println(" System> Great Job "+Stu+"!");
Gdr2();
} else if (Grd1<5) {
System.out.println("I'm sorry, the lowest grade I am allowed to compute is 5...");
Gdr1();
} else if (Z.equalsIgnoreCase("restart")) {
restart01();
} else {
System.out.println("("+Z+") cannot be resolved in my system...");
Gdr1();
}
} catch (Exception e) {}
}
Now everything works in the program, besides for when the End-User's input = "restart", I know some of the code in the program seems complicated, but it does work (most of it), can anyone help me try to figure this out, its for my portfolio at my school due latest by 1/25/2017 # 11:59 pm.
The things like Z (constant String), ""+Stu+"" (variable input), and [Code: Gdr1] are there for a purpose...