Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 439 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
why Scanner class not working ?

my keyboard inputs // first an integer and second a string
2
This is a string
my code :
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in) ;
int a=sc.nextInt();
//float b=sc.nextFloat();
String str=sc.nextLine();
System.out.println("###");
System.out.println(a);
// System.out.println(b);
System.out.println(str);
}
}
I am not getting two inputs . I am getting 2 only. what happened to second input ?
Answers
-
Try your input from one line.
10 my string input
Check the documentation and has number of examples.
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
-
import java.util.Scanner;
public class B {
public static void main(String args[]) {
String strInput = "10\n01--some text for testing...\n02--second text string";
Scanner sc = new Scanner(strInput);
sc.useDelimiter("\n");
int a = sc.nextInt();
String str1 = sc.next();
String str2 = sc.next();
System.out.println("### Input:");
System.out.println(strInput);
System.out.println("### Output:");
System.out.println(a);
System.out.println(str1);
System.out.println(str2);
}
}
### Input:
10
01--some text for testing...
02--second text string
### Output:
10
01--some text for testing...
02--second text string
-
my keyboard inputs // first an integer and second a string2This is a string. . .int a=sc.nextInt();. . .String str=sc.nextLine();I am not getting two inputs . I am getting 2 only. what happened to second input ?
It is still there waiting for you to 'get' it.
The 'nextint' gets a TOKEN - it does NOT get a line - the line terminator is a token and is still there.
The 'nextLine' gets the rest of the line - which is the line terminator of the first line.
So ask yourself - why did you use 'nextint' for the first input but 'nextLine' for the next one?
I suggest you read The Java Tutorials trail named 'Scanning'.
https://docs.oracle.com/javase/tutorial/essential/io/scanning.html
Try the example in that trail to understand how it works.
-
the line terminator is a token and is still there.
Could not get this part. Could you please explain bit more at this ?
-
I'd send inputs as I have shown ( but not through a single line.)
2
This is a string.
how do I take into values ?
-
import java.util.Scanner;
public class B {
public static void main(String args[]) {
System.out.println(">> example - 1");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
//https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--
sc.nextLine(); //clue from rp0428
String str1 = sc.nextLine();
System.out.println("### Output:");
System.out.println(a);
System.out.println(str1);
System.out.println("\n>> example - 2");
sc = new Scanner(System.in);
a = Integer.parseInt(sc.nextLine());
str1 = sc.nextLine();
System.out.println("### Output:");
System.out.println(a);
System.out.println(str1);
System.out.println("\n>> example - 3");
sc = new Scanner(System.in);
sc.useDelimiter("\n");
a = Integer.parseInt(sc.next());
str1 = sc.next();
System.out.println("### Output:");
System.out.println(a);
System.out.println(str1);
}
}
-
import java.util.Scanner;
public class B {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
int a = sc.nextInt();
String str1 = sc.next();
System.out.println("### Output:");
System.out.println(a);
System.out.println(str1);
}
}
-
if I input this way :
2 this is a test
output:
2
<space>this is a test
I was not expecting <space> there. What could be the reason ?
-
That is because, everything after the int value (2) is returned as nextLine() as described in the API documentation.
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--
nextLine
public String nextLine()
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
- Returns:
- the line that was skipped
Try this for explanation:
import java.util.Scanner;
public class B {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
sc.useDelimiter("mydelimiter");
int a = sc.nextInt();
String str1 = sc.nextLine();
System.out.println("### Output:");
System.out.println(a);
System.out.println(str1);
}
}
100mydelimiter text string
### Output:
100
mydelimiter text string
-
Could not get this part. Could you please explain bit more at this ?Did you go through the Java Tutorial trail?
Did you try that example?
Did you understand what a 'token' is? And that some methods read 'tokens'?
Your first line has two tokens. The nextInt method reads ONE TOKEN so gets the 2.
The nextLine method reads the rest of the line - which is the line terminator token.
Call 'nextLine' again and it will read the second line.
Go thru the tutorial and read the API docs for the methods you are using.