I have written a basic code to read different data types. But I can not enter the string as input. What am I missing ?
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int integer = read.nextInt();
double Double = read.nextDouble();
String string = read.nextLine();
System.out.printf("String: %s\nDouble: %f\nInt: %d",string,Double,integer);
}
}
You must "eat up" the newline character left over from the double.
Scanner read = new Scanner(System.in);
int integer = read.nextInt();
double Double = read.nextDouble();
read.nextLine();
String string = read.nextLine();
System.out.printf("String: %s\nDouble: %f\nInt: %d",string,Double,integer);
The problem is that after nextDouble() there is still a newline character out there so the scanner reads the next line which has nothing in it...
Related
I need to use scannner to take input from user for data type double separated by white space.
Scanner input = new Scanner(System.in);
System.out.print("exampleDouble: ");
double db = input.nextDouble();
db+=input.nextLine();
I thought this would work, looking for a simple statement for capturing 2 values (both double, separated by space). no arrays. only need to capture 2 values , not more.
Example : 58.0 57.3
WITHOUT USING ARRAYS
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double double1 = input.nextDouble();
double double2 = input.nextDouble();
System.out.println("Double1 is "+double1);
System.out.println("Double2 is "+double2);
}
}
WITH ARRAYS
You can split it into a String array, and then parse them using Double.parseDouble like so:
Scanner input = new Scanner();
String[] userinput = input.nextLine().split(" ");
double double1 = Double.parseDouble(userinput[0]);
double double2 = Double.parseDouble(userinput[1]);
Therefore, the full code would be:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String[] userinput = input.nextLine().split(" ");
double double1 = Double.parseDouble(userinput[0]);
double double2 = Double.parseDouble(userinput[1]);
System.out.println("Double1 is "+double1);
System.out.println("Double2 is "+double2);
}
}
Sample I/O
Input
2.5 3.5
Output
Double1 is 2.5
Double2 is 3.5
HOWEVER: As #Pschemo so aptly pointed out - you should just be using input.nextDouble() twice.
I was trying to code this program for one of my classes and I ran into a problem with my output. It is supposed to read whatever I have typed in for the Scanner input. However, the output skips the first word and I'm not really sure why. You can ignore most of the declarations of variables in the main method. Those are useful just for the rest of the program.
public static void main(String[] args) {
String fullName;
int anniversaryM;
int anniversaryY;
int periodHours;
String jobTitle;
double payRate;
int monthsWorked;
double vacationHours;
double grossPay;
double retirement;
double taxWithholding;
double netPay;
Scanner in = new Scanner(System.in);
fullName = inputLine(in, "Enter your full name:");
System.out.print(fullName);
}
public static double inputNumber(Scanner input, String prompt) {
Scanner in = new Scanner(System.in);
in.nextDouble();
return in.nextDouble();
}
public static String inputLine(Scanner input, String prompt) {
Scanner in = new Scanner(System.in);
System.out.println(prompt);
in.next();
return in.next();
}
public static double calcPercentage(double grossPay, double retirement) {
Scanner in = new Scanner(System.in);
in.nextDouble();
return in.nextDouble();
}
Output:
Enter your full name:
John Doe
Doe
You have a double call to in.next(). Just remove it and you should be OK. Additionally, note that you're passing the Scanner to the method, so you shouldn't create a new inside the method:
public static String inputLine(Scanner input, String prompt) {
System.out.println(prompt);
return input.nextLine();
}
I think your problem is the in.next(); return in.next();. in which case it will call the reader two times, if you want to return the value of the in.next(); you should put it in a container and return that container or just go straight return in.next();
I'm trying to read the input which is in the following format.
2
asdf
asdf
3
asd
df
2
Following is the code:
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
System.out.println(t);
while(t>0){
String a = scanner.next();
String b = scanner.next();
int K = scanner.nextInt();
}
But when I'm scanning, I'm getting empty t=2 , a="" , b=asdf, K=asdf
Can't figure out the issue. There is no space/new line between 2 and asdf.
I have tried using scanner.nextLine() instead of scanner.next() but no change
nextInt() doesn't cosume the newline token, so the following read will get it. You could introduce a nextLine after the nextInt to skip it:
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
scanner.nextLine(); // Skip the newline character
System.out.println(t);
while(t > 0) {
String a = scanner.next();
String b = scanner.next();
int K = scanner.nextInt();
scanner.nextLine(); // Skip the newline character
}
Another approach I prefer:
Scanner scanner = new Scanner(System.in);
int t = Integer.parseInt(scanner.nextLine());
System.out.println(t);
while(t>0){
String a = scanner.nextLine();
String b = scanner.nextLine();
int K = Integer.parseInt(scanner.nextLine());
}
But note it will throw NumberFormatException when input is incorrect.
Read an integer and a string provided in a single line in JAVA.
INPUT
3 ABCDEF
how to store 3 in a integer variable and ABCDEF in a string variable
Use Scanner:
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
String text = scanner.next();
If you already have the data in a String, pass this String variable as the constructor of your Scanner:
String foo = "123 ABCDE";
Scanner scanner = new Scanner(foo);
int number = scanner.nextInt();
String text = scanner.next();
If you want/need more info about it, read the proper javadoc.
you can also do like this :
public static void main(String[] args) {
String mixData="123 string";
String []data = mixData.split("\\s+", 2);
int intValue=Integer.parseInt(data[0]);
String str=data[1];
System.out.println(intValue);
System.out.println(str);
}
package calculator;
import java.util.*;
public class calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Calculator");
System.out.println("Enter a number");
double firstNumber = in.nextDouble();
System.out.println("Enter another number");
double secondNumber = in.nextDouble();
System.out.println("Enter the letter for 'm'ultiply 's'ubtract 'a'dd or 'd'ivide");
String wantedProcess = in.nextLine();
String multiply = "m";
String subtract = "s";
String add = "a";
String divide = "d";
if(wantedProcess.equals(multiply))
{
double product = firstNumber * secondNumber;
System.out.println("=" + product);
}
}
}
so this is the calculator i was making but after i enter two numbers i cant type m s a or d. what am i doing wrong?
if(wantedProcess.equals(multiply))
You missed the second brace at the end. Now look: if you enter "m" for multiply and then compare String "m" with String "Multiply" how do you expect them to be equal? Compare only first letters, for example, using String.charAt() method.
What's more, I guess you'd be better off using Scanner's next() method, rather than nextLine(). Check their docummentation here: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()
change this if(wantedProcess.equals(multiply) to if(wantedProcess.equals(multiply))
you missed out )
Edit: Then
You need to create a new Scanner object again
Scanner inF = new Scanner(System.in);
String wantedProcess = inF.nextLine();