This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
This is my code
import java.io.*;
import java.util.*;
class student
{
String name;
int age;
float cgpa;
}
public class getdata
{
public static void main(String args[]) throws IOException
{
Scanner in=new Scanner(System.in);
int n;
n=in.nextInt();
student[] s=new student[n];
for(int i=0;i<n;i++)
{
try
{
s[i]=new student();
s[i].name=in.nextLine();
in.nextLine();
s[i].age=in.nextInt();
s[i].cgpa=in.nextFloat();
}
catch(InputMismatchException e)
{
System.out.println(e.getMessage());
}
}
System.out.println();
System.out.println("Name\tAge\tCGPA\n");
for(int i=0;i<n;i++)
{
System.out.println(s[i].name+"\t"+s[i].age+"\t"+s[i].cgpa+"\n");
}
}
}
compiling the program gave no problem. but when executing and i try to input a string space , it takes the string as two separate strings and assigns all other values of one to be null. for eg if i enter
mike hannigan
5
6.5
The output is
mike 0 0.0
hannigan 5 6.5
i tried getting the string with only a single in.nextLine(); but that causes the string to be taken as null(Throws InputMismatchException). with try and catch block
and without the try block, this is the output i get
My suggestion is to always scan the entire line as String and convert it to required data types using parse methods. Please see below:
public static void main(String args[]) throws IOException
{
Scanner in=new Scanner(System.in);
int n;
n=Integer.parseInt(in.nextLine());
student[] s=new student[n];
for(int i=0;i<n;i++)
{
s[i]=new student();
s[i].name=in.nextLine();
s[i].age=Integer.parseInt(in.nextLine());
s[i].cgpa=Float.parseFloat(in.nextLine());
}
System.out.println();
System.out.println("Name\tAge\tCGPA\n");
for(int i=0;i<n;i++)
{
System.out.println(s[i].name+"\t"+s[i].age+"\t"+s[i].cgpa+"\n");
}
}
Your problem is the very common mistake in understanding Scanner.
Calling nextInt(), nextFloat(), or most other nextXxx() methods will leave the newline following the number unprocessed.
A subsequent call to any of the nextXxx() methods, other than nextLine(), will automatically skip that newline, as being whitespace between tokens.
However, nextLine() does not skip leading whitespace, so calling nextLine() after any other nextXxx() method, will return an empty string (or rather whatever is on the rest of the line following the last token).
So, when mixing nextXxx() calls with nextLine() calls, you have to flush (discard) the end of the previous line by calling nextLine() first.
This means your code should be:
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine(); // Ignore rest of line after int
student[] s = new student[n];
for (int i = 0; i < n; i++) {
s[i] = new student();
s[i].name = in.nextLine();
s[i].age = in.nextInt();
s[i].cgpa = in.nextFloat();
in.nextLine(); // Ignore rest of line after float
}
System.out.println();
System.out.println("Name\tAge\tCGPA");
for (int i = 0; i < n; i++) {
System.out.println(s[i].name + "\t" + s[i].age + "\t" + s[i].cgpa);
}
Better yet, do what the answer by #JaganathanNanthakumar says: Always use nextLine() and parse the number yourself.
Related
I realized the issue was due to me creating a new scanner/reader object(the new scanner has a blank input), the issue still persists - how do I use the same input stream for both methods (or) how do I use the same scanner reader for both methods
Original question: how to pass on the main method input stream to called method
So I'm taking in formatted input which is like this from geek for geeks (this is important to my error)
1
4
1 2 3 4
And I am using scanner class to read the numbers into variables. This is my code
// code to print reverse of input string after reading the length of the string and no. of testcases
class Main {
public static void main (String[] args) {
int i,t,n;
String x,y;
Scanner scan = new Scanner(System.in);
try {
t=scan.nextInt();
scan.nextLine();
REV rv=new REV();
for (i=0;i<t;i++){
rv.reverse();
}
}catch (Exception e){
return;
}
}
}
class REV{
public void reverse(){
int i,a[],n;
Scanner scan = new Scanner(System.in);
try {
n=scan.nextInt();
scan.nextLine()
a= new int[n];
for (i=n-1;i>=0;i--){
a[i]=scan.nextInt();
}
System.out.println(a[i]);
}catch(Exception E){
return;
}
}
}
I get no output for this (java.util.NoSuchElementException if I don't use try and catch)
I am able to read the variables in the main method but my input stream becomes empty for the new method
I verified this by using nextLine() both in the main() method and reverse() method as shown
public static void main (String[] args) {
int t,n;
String x,y,z;
t=scan.nextInt();
x=scan.nextLine();
n=scan.nextInt(); //to eat up the n input
y=scan.nextLine();
z=scan.nextLine();
System.out.println(x+y);
....
}
output-
141 2 3 4
and
public void reverse(int n){
....
String k,j;
k = scan.nextLine(); //replacing n- to check what n=scanInt() reads
j= scan.nextLine();
System.out.println(x +y);
....
}
Output is blank again (java.util.NoSuchElementException)
I think this means the input stream is empty for the reverse() method.
So how do I pass on the main() input to reverse()
Note: 1. if I don't use try{} and catch{} it gives me java.util.NoSuchElementException
I'm aware I have made the code needlessly a little complicated, this is due to me trying to solve this problem
I got the try{} and catch{} solution from here, but it doesn't solve my empty input problem
4.This made me understand the empty input exception
Create a method instead of making an Object of a class and then calling the method. (Unless you want it that way).
Create a single static Scanner and use it everywhere.
private static Scanner scanner = new Scanner(System.in)
To print an array use System.out.println(Arrays.toString(a));.
Reductant use of scanner.nextLine() after Scanner.nextInt.
import java.util.Arrays;
import java.util.Scanner;
class Main {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int i, t, n;
String x, y;
t = scanner.nextInt();
for (i = 0; i < t; i++) {
reverse();
}
}
private static void reverse() {
int i;
int[] a;
int n;
n = scanner.nextInt();
a = new int[n];
for (i = n - 1; i >= 0; i--) {
a[i] = scanner.nextInt();
}
System.out.println(Arrays.toString(a));
}
}
Is this your desired output?
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
Below is a program for palindrome. If it is palindrome it will print YES else NO.
I am unable to understand what's the difference in calling:
> int n=Integer.parseInt(in.nextLine());
> or int n=in.nextInt();
because both is doing the same work.
1st one is taking Stringas input then converting it into int
2nd one is taking directly int.
when the 1st one is taken there is no error.
But when 2nd one is taken it gets stored in n then it prints YES.(when debugged i found out that it gets stored in n but it skips the input string str and then it compares with str and s and prints YES).
So can anyone explain the logic behind this.
public class Test1 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//int n=Integer.parseInt(in.nextLine());
int n=in.nextInt();
while(n!=0){
String s="";
String str=in.nextLine();
for(int i=str.length()-1;i>=0;i--){
s=s+str.charAt(i);
}
if(str.equals(s) ){
System.out.println("YES");
}
else{
System.out.println("NO");
}
n--;
}
}
}
I guess that with Scanner you expect the String to be verified... in that case this line int n=in.nextInt(); will throw a java.util.InputMismatchException exception if the input is not a number.
The verification would be easier to achieve by using StringBuilder, like this:
Scanner in=new Scanner(System.in);
String original = in.nextLine();
StringBuilder reversa = new StringBuilder(original).reverse();
if (reversa.toString().equals(original)) {
System.out.println("YES");;
} else {
System.out.println("NO");;
}
I'm trying to write a class that will take in numbers from a file, but I keep running into this error when I run:
Java.util.NoSuchElementException: Null(in java.util.Scanner)
This is my code:
import java.util.*;
import java.io.*;
public class finalMain
{
public static void main (String args[]) throws IOException
{
int lineNumber = 0;
Scanner sc = new Scanner (new File ("Prog349f.txt"));
System.out.println("Student Quiz 1 Quiz2 MidTerm Final Final % Grade");
while(sc.hasNextLine())
{
lineNumber++;
sc.nextLine();
}
for(int i = 1; i <= 1; i++)
{
int quizOne = sc.nextInt();
int quizTwo = sc.nextInt();
int midterm = sc.nextInt();
int finalTest = sc.nextInt();
finalGrade studentNext = new finalGrade(sc.nextInt(),sc.nextInt(), sc.nextInt(),sc.nextInt(), i);
System.out.println(studentNext);
}
sc.close();
}
}
I'm thinking maybe I need to create two scanner objects, one for each line or something but I don't know how I would go about doing that.
You have misunderstood the usage of Scanner methods : sc.nextLine(); consumes and returns the line.
That means that you're currently reading the whole file with sc.nextLine();, discarding the result, and only then you try to read 4 ints, that can't be read since the Scanner is at the end of the file.
You should instead use one of these two methods :
If you're positive that each line of your file contains 4 ints separated by space (or any other specific character), and nothing more, you can then scan 4 ints while the scanner has a next line.
If there might be variations, or useless data, you should keep your hasNextLine() and nextLine() calls as they are, then use regex, split+indexing or another Scanner to retrieve the 4 ints from the line.
I have some problem when I ask the user to input some numbers and then I want to process them. Look at the code below please.
To make this program works properly I need to input two commas at the end and then it's ok. If I dont put 2 commas at the and then program doesnt want to finish or I get an error.
Can anyone help me with this? What should I do not to input those commas at the end
package com.kurs;
import java.util.Scanner;
public class NumberFromUser {
public static void main(String[] args) {
String gd = "4,5, 6, 85";
Scanner s = new Scanner(System.in).useDelimiter(", *");
System.out.println("Input some numbers");
System.out.println("delimiter to; " + s.delimiter());
int sum = 0;
while (s.hasNextInt()) {
int d = s.nextInt();
sum = sum + d;
}
System.out.println(sum);
s.close();
System.exit(0);
}
}
Your program hangs in s.hasNextInt().
From the documentation of Scanner class:
The next() and hasNext() methods and their primitive-type companion
methods (such as nextInt() and hasNextInt()) first skip any input that
matches the delimiter pattern, and then attempt to return the next
token. Both hasNext and next methods may block waiting for further
input.
In a few words, scanner is simply waiting for more input after the last integer, cause it needs to find your delimiter in the form of the regular expression ", *" to decide that the last integer is fully typed.
You can read more about your problem in this discussion:
Link to the discussion on stackoverflow
To solve such problem, you may change your program to read the whole input string and then split it with String.split() method. Try to use something like this:
import java.util.Scanner;
public class NumberFromUser {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] tokens = sc.nextLine().split(", *");
int sum = 0;
for (String token : tokens) {
sum += Integer.valueOf(token);
}
System.out.println(sum);
}
}
Try allowing end of line to be a delimiter too:
Scanner s = new Scanner(System.in).useDelimiter(", *|[\r\n]+");
I changed your solution a bit and probably mine isn't the best one, but it seems to work:
Scanner s = new Scanner(System.in);
System.out.println("Input some numbers");
int sum = 0;
if (s.hasNextLine()) {
// Remove all blank spaces
final String line = s.nextLine().replaceAll("\\s","");
// split into a list
final List<String> listNumbers = Arrays.asList(line.split(","));
for (String str : listNumbers) {
if (str != null && !str.equals("")) {
final Integer number = Integer.parseInt(str);
sum = sum + number;
}
}
}
System.out.println(sum);
look you can do some thing like this mmm.
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Input some numbers");
System.out.println("When did you to finish and get the total sum enter ,, and go");
boolean flag = true;
int sum = 0;
while (s.hasNextInt() && flag) {
int d = s.nextInt();
sum = sum + d;
}
System.out.println(sum);
}
I am trying to take string input in java using Scanner, but before that I am taking an integer input. Here is my code.
import java.util.*;
class prc
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int n=input.nextInt();
for(int i=1;i<=n;i++)
{
String str=input.nextLine();
System.out.println(str);
}
}
}
The problem is that if I give a number n first, then the number of string it is taking as inputs is n-1.
e.g if the number 1 is entered first, then it is taking no string inputs and nothing is printed.
Why is this happening ?
Thanks in Advance!
nextLine() reads everything up to and including the next newline character.
However, nextInt() only reads the characters that make up the integer, and if the integer is the last (or only) text in the line, you'll be left with only the newline character.
Therefore, you'll get a blank line in the subsequent nextLine(). The solution is to call nextLine() once before the loop (and discard its result).
Information regarding the code is mentioned in the comments written next to each line.
public static void main(String[] args) {
int num1 = sc.nextInt(); //take int input
double num2 = sc.nextDouble(); //take double input
long num3 = sc.nextLong(); //take long input
float num4 = sc.nextFloat(); //take float input
sc.nextLine(); //next line will throw error if you don't use this line of code
String str = sc.nextLine(); //take String input
}
import java.util.*;
class prc
{
public static void main(String[] args)
{
String strs[];
Scanner input = new Scanner(System.in);
int n = input.nextInt();
input.nextLine();
strs = new String[n];
for(int i = 1; i < n; i++)
{
strs[i] = input.nextLine();
System.out.println(strs[i]);
}
}
}