How to take multiple string value as input? [duplicate] - java

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 5 years ago.
I have a string variable that I use to get input values. for ex.
Scanner in=new Scanner(System.in);
varName=in.next();
when I give value as (John jony) it only displays John. Any way to get whole string?

Use the following instead:
Scanner in=new Scanner(System.in);
String text = in.nextLine();
System.out.println( text );
"in.nextLine()" reads in a whole line

Related

Throw Exception for unsupported characters [duplicate]

This question already has answers here:
How to check if a String contains only ASCII?
(14 answers)
Closed 3 years ago.
I have user input
private Scanner inn = new Scanner(System.in);
String input = inn.nextLine().toLowerCase();
and i need to throw IllegalCharacterException(own exception, created this class already) for not desired input (all numerals and symbols and maybe even other languages)I need only english letters. How can i do that? Thank you.
You can use String.matches(String regex) with regular expressions. E.g.
private Scanner inn = new Scanner(System.in);
String input = inn.nextLine().toLowerCase();
if(input.matches("[a-z]*")) {
// Do some stuff
}
The Regular expression [a-z]* matches any character from a to z (lowercase), without any numbers, symbols or spaces.

How to make scanner input to be on the same line as question? [duplicate]

This question already has answers here:
How to keep my user input on the same line after an output?
(4 answers)
Java How to get scanner.nextLine() on the same line? [duplicate]
(3 answers)
Closed 3 years ago.
I want the input of the scanner to be on the same line as the question.
By code is as below
String choice;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your choice: ");
choice = scanner.next().toUpperCase();
Current console output:
Enter your choice:
A
Desired console output:
Enter your choice: A
Thanks in advance for the help
You can print without a new line using
System.out.print("Enter your choice: ");

Java Equivalent to fscanf in C [duplicate]

This question already has answers here:
Is there an equivalent method to C's scanf in Java?
(7 answers)
what is the Java equivalent of sscanf for parsing values from a string using a known pattern?
(8 answers)
Closed 4 years ago.
Hi I i'm trying to do a line by line reading in of a file in Java. For example I'd like to be able to do something like
//c code
while( fscanf(ptr, "%s %s %s",string1,string2,string3) == 3)
printf("%s %s %s",string1,string2,string3);
Where I scan in every line in the file individually and store it in a variable.
I have started out by making a file object and finding the file. However I haven't found a function to be able to read input.
Use line oriented input, split the lines and print the contents. That is, read each line, check if you have three tokens (end if you don't) and print them. Like,
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] tokens = line.split(" ");
if (tokens.length != 3) {
break;
}
System.out.printf("%s %s %s%n", tokens[0], tokens[1], tokens[2]);
}
Change System.in to a File to read from a file.

Converting String to Integer or object [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 6 years ago.
I was wondering how I could convert user input using scanner to an integer that I have already declared. I have already created the object person1 and declared the variables firstName, lastName, age and gender in the 'Person' class. I know I should be using parse but I'm unsure how to implement it within my code.
Scanner user_input = new Scanner( System.in );
System.out.println("Please input your details.");
System.out.println("First Name:");
person1.setFirstName(user_input.nextLine());
System.out.println("Last Name:");
person1.lastName = user_input.nextLine();
System.out.println("Age:");
person1.age = user_input.nextLine();
System.out.println("Gender:");
person1.gender = user_input.nextLine();
Lines 9 and 11 need modifying.
For the age, use :
person1.age = user_input.nextInt();

Java : Scanner odd behaviour [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 7 years ago.
I tried to get inputs via scanner and in the past, I use enter to get to the next set of inputs.
For ex.
Input 1 <enter>
Input 2 <enter>
However this time, it only accepts in the same line , taking spaces as delimiter.
Scanner in = new Scanner(System.in);
int a,b,n,t;
String input_line;
String inputs[]= new String[3];
t = in.nextInt();
in.reset(); //Tried resetting Scanner to see if this works
input_line = in.nextLine();
inputs = input_line.split(" ");
for(String s:inputs)
System.out.println(s);
For instance, I expect to take the variable t in first line and then move on to the second line for input_line scanning. But if I hit enter after entering t, the program ends.
What am I missing here?
(Merging with another question was suggested but , let me explain, the Scanner does not skip any inputs).
Without any testing I would think you would need something like this
Scanner in = new Scanner(System.in);
int a,b,n,t;
String input_line;
String[] input_numbers = new String[3];
t = in.nextInt();
in.nextLine();
input_line = in.nextLine();
while(!input_line.equals("")){
input_numbers = input_line.split(" ");
// do what you want with numbers here for instance parse to make each string variable into int or create new scanner to do so
input_line = in.nextLine();
}
}

Categories