Converting String to Integer or object [duplicate] - java

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

Related

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

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

How to keep asking for user input until the input is empty? [duplicate]

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

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

Return the user input backwards e.g. Laura would be returned as aruaL [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
In this program I am trying to return the user input backwards
e.g. Laura would be returned as aruaL, but I am getting [C#11d72ca as my output..
String input = JOptionPane.showInputDialog("Enter name");
Scanner scanner = new Scanner(input);
String name = scanner.next();
scanner.close();
char[] backwards = new char[name.length()];
for(int i = 0; i<name.length(); i++)
{
backwards[i] = name.charAt(name.length()-1-i);
}
JOptionPane.showMessageDialog(null, backwards.toString());
}
There is an easier way to reverse a string:
String reverse = new StringBuilder(name).reverse().toString()
If you don't want to use this method, in order to covert a char[] to a string you would need to do something like:
String reverse = new String(backwards);
Calling toString on an array will call the toString method of the Object. This will return you the hashCode which is what you are getting

Java String not working, printing different names [duplicate]

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

Categories