My Java program refuses to take a string input [duplicate]
This question already has an answer here:
Scanner is skipping nextLine() after using next() or nextFoo()?
14 answers
I have an array of strings - name
. When I try to take an input from the user using the Scanner class the program seems to ignore the statement and go on to the next.
import java.util.Scanner;
class Student //start of class
public static void main(String args) //start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i]; //calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
java string input
marked as duplicate by Ole V.V.
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 6:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Scanner is skipping nextLine() after using next() or nextFoo()?
14 answers
I have an array of strings - name
. When I try to take an input from the user using the Scanner class the program seems to ignore the statement and go on to the next.
import java.util.Scanner;
class Student //start of class
public static void main(String args) //start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i]; //calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
java string input
marked as duplicate by Ole V.V.
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 6:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please indent your code properly (your IDE can do that for you). I downvoted because if we cannot read your code, we cannot help you.
– Ole V.V.
Nov 11 at 7:17
add a comment |
This question already has an answer here:
Scanner is skipping nextLine() after using next() or nextFoo()?
14 answers
I have an array of strings - name
. When I try to take an input from the user using the Scanner class the program seems to ignore the statement and go on to the next.
import java.util.Scanner;
class Student //start of class
public static void main(String args) //start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i]; //calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
java string input
This question already has an answer here:
Scanner is skipping nextLine() after using next() or nextFoo()?
14 answers
I have an array of strings - name
. When I try to take an input from the user using the Scanner class the program seems to ignore the statement and go on to the next.
import java.util.Scanner;
class Student //start of class
public static void main(String args) //start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i]; //calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
This question already has an answer here:
Scanner is skipping nextLine() after using next() or nextFoo()?
14 answers
java string input
java string input
edited Nov 14 at 16:18
Bilesh Ganguly
1,73811533
1,73811533
asked Nov 11 at 6:11
Rohan Khatua
35
35
marked as duplicate by Ole V.V.
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 6:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ole V.V.
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 11 at 6:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please indent your code properly (your IDE can do that for you). I downvoted because if we cannot read your code, we cannot help you.
– Ole V.V.
Nov 11 at 7:17
add a comment |
Please indent your code properly (your IDE can do that for you). I downvoted because if we cannot read your code, we cannot help you.
– Ole V.V.
Nov 11 at 7:17
Please indent your code properly (your IDE can do that for you). I downvoted because if we cannot read your code, we cannot help you.
– Ole V.V.
Nov 11 at 7:17
Please indent your code properly (your IDE can do that for you). I downvoted because if we cannot read your code, we cannot help you.
– Ole V.V.
Nov 11 at 7:17
add a comment |
2 Answers
2
active
oldest
votes
That's because the sc.nextInt()
method does not read the newline character in your input and so you need to call sc.nextLine()
From the docs
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.
You code will now look like :
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
sc.nextLine(); // <----- observe this
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
sc.nextLine(); // <----- observe this
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
add a comment |
Try this.. Your sc.nextLine() is reading empty String after you input integer value
import java.util.Scanner;
class Student
//start of class
public static void main(String args)
//start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String emp = sc.nextLine();
String name = new String[n];
int totalmarks = new int[n];
for (int i=0;i<n;i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
emp = sc.nextLine();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];//calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
There is no need to store the reference ofsc.nextLine()
at all.
– Nicholas K
Nov 11 at 6:28
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's because the sc.nextInt()
method does not read the newline character in your input and so you need to call sc.nextLine()
From the docs
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.
You code will now look like :
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
sc.nextLine(); // <----- observe this
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
sc.nextLine(); // <----- observe this
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
add a comment |
That's because the sc.nextInt()
method does not read the newline character in your input and so you need to call sc.nextLine()
From the docs
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.
You code will now look like :
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
sc.nextLine(); // <----- observe this
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
sc.nextLine(); // <----- observe this
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
add a comment |
That's because the sc.nextInt()
method does not read the newline character in your input and so you need to call sc.nextLine()
From the docs
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.
You code will now look like :
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
sc.nextLine(); // <----- observe this
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
sc.nextLine(); // <----- observe this
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
That's because the sc.nextInt()
method does not read the newline character in your input and so you need to call sc.nextLine()
From the docs
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.
You code will now look like :
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
sc.nextLine(); // <----- observe this
String name = new String[n];
int totalmarks = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
sc.nextLine(); // <----- observe this
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
answered Nov 11 at 6:18
Nicholas K
5,17251031
5,17251031
add a comment |
add a comment |
Try this.. Your sc.nextLine() is reading empty String after you input integer value
import java.util.Scanner;
class Student
//start of class
public static void main(String args)
//start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String emp = sc.nextLine();
String name = new String[n];
int totalmarks = new int[n];
for (int i=0;i<n;i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
emp = sc.nextLine();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];//calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
There is no need to store the reference ofsc.nextLine()
at all.
– Nicholas K
Nov 11 at 6:28
add a comment |
Try this.. Your sc.nextLine() is reading empty String after you input integer value
import java.util.Scanner;
class Student
//start of class
public static void main(String args)
//start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String emp = sc.nextLine();
String name = new String[n];
int totalmarks = new int[n];
for (int i=0;i<n;i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
emp = sc.nextLine();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];//calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
There is no need to store the reference ofsc.nextLine()
at all.
– Nicholas K
Nov 11 at 6:28
add a comment |
Try this.. Your sc.nextLine() is reading empty String after you input integer value
import java.util.Scanner;
class Student
//start of class
public static void main(String args)
//start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String emp = sc.nextLine();
String name = new String[n];
int totalmarks = new int[n];
for (int i=0;i<n;i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
emp = sc.nextLine();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];//calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
Try this.. Your sc.nextLine() is reading empty String after you input integer value
import java.util.Scanner;
class Student
//start of class
public static void main(String args)
//start of method main
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = sc.nextInt();
String emp = sc.nextLine();
String name = new String[n];
int totalmarks = new int[n];
for (int i=0;i<n;i++)
System.out.println("Student " + (i + 1));
System.out.print("Enter name: ");
name[i] = sc.nextLine();
System.out.print("Enter marks: ");
totalmarks[i] = sc.nextInt();
emp = sc.nextLine();
int sum = 0;
for (int i = 0; i < n; i++)
sum = sum + totalmarks[i];//calculating total marks
double average = (double) sum / n;
System.out.println("Average is " + average);
for (int i = 0; i < n; i++)
double deviation = totalmarks[i] - average;
System.out.println("Deviation of " + name[i] + " is " + deviation);
//end of method main
//end of class
answered Nov 11 at 6:20
Akshit Gupta
136
136
There is no need to store the reference ofsc.nextLine()
at all.
– Nicholas K
Nov 11 at 6:28
add a comment |
There is no need to store the reference ofsc.nextLine()
at all.
– Nicholas K
Nov 11 at 6:28
There is no need to store the reference of
sc.nextLine()
at all.– Nicholas K
Nov 11 at 6:28
There is no need to store the reference of
sc.nextLine()
at all.– Nicholas K
Nov 11 at 6:28
add a comment |
Please indent your code properly (your IDE can do that for you). I downvoted because if we cannot read your code, we cannot help you.
– Ole V.V.
Nov 11 at 7:17