Use Scanner for many different methods [duplicate] - java

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

Related

Is there a visual-studio/C# class equivalent to the Scanner(System.in) class in java? [duplicate]

This question already has answers here:
Is there an equivalent to the Scanner class in C# for strings?
(7 answers)
Closed 1 year ago.
I have been trying to input values into my program to be able to store and use the inputs of the user. This is easily done in java because one can just create a Scanner class and use it like so.
import java.util.Scanner;
public static void main(String[] args){
static Scanner sc= new Scanner(System.in);
System.out.print("\nEnter id number:");
int id = sc.nextInt();
System.out.print("\nEnter name:");
int name = sc.nextString();
}
However, when it comes to implementing the same idea in C# there seems to be no equivalent class that can be used. Is there such a class in the system files that could do this?
If there isn't then are there any methods that would be compatible with C# / visual studio that accomplish this same feature (like the keyboard class used in java)? Preferably methods that can be implemented in a separate class?
You don't need to implement anything man. Just use
Console.WriteLine(), Console.ReadLine() & Console.ReadKey()
So to answer your question, equivalent of Scanner in C# would be Console
You can simply use Console.ReadLine() to read and store Input to any variable in C#.
for example :
using System;
public class GFG{
static public void Main (){
//Code
string message = "What is your name?";
string myName = Console.ReadLine();
Console.WriteLine("Hello! " + myName);
}
}

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);
}
}

I'm trying to get a toString to print out in a group together after a loop is cancelled [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
This is what I have so far and I tried to use the while (yorn=="yes") to cancel but it keeps going regardless and I am also trying to find a way to make it so the outputs go in one group together rather than being seperated.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String no = "no";
String yes = "yes";
String yorn="yes";
CollegeStudent Student1 = new CollegeStudent();
while (yorn=="yes") {
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
}
}
You need to use the equals method for string comparing in the java:
while (yorn.equals("yes"))
{
System.out.println("continue? yes/no:");
yorn=scan.next();
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
}
Since after you answer exists the logic in the cicle, after yes typing code has been executing anyway (one time). Simply solution for you will be:
System.out.println("Do you need to add a student? yes/no:");
yorn=scan.next();
while (yorn.equals("yes"))
{
Student1.setname();
Student1.setcourseTitle();
Student1.setcredits();
Student1.setcourseCode();
System.out.println(Student1.toString());
System.out.println("continue? yes/no:");
yorn=scan.next();
}

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

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");
}
}

How do I create multiple console inputs for different variables?

My goal is to have the user be prompted for multiple separate inputs that would store the data in a manner that I could then manipulate.
For example:
Question:
What is your username?
What is your name?
Input:
Sparkeyy
Nelson
I then want to be able to take these and add them / multiply if they're numbers. This is what I have so far. (Also first question so sorry for poor formatting)
import java.util.*;
public class Program{
public static void main(String args[]){
System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
System.out.println("Your username is " + username);
}
public static void (name){
System.out.println("Enter your name: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("Your name is " + name);
}
}
Take a look here, they answered you're question thoroughly:
https://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java
You basically need a Scanner, like you're doing, and then for numbers:
scanner.nextInt();
There are also ways of converting properly formatted Strings to Ints/Doubles.

Categories

Resources