Results 1 to 5 of 5
- 01-16-2013, 02:33 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 28
- Rep Power
- 0
Keeping console input on one line
I'm doing an exercise from my javabook, in where I'm supposed to get output like:
"Enter a student record: Maria 5 72 91 84 89 78
Maria's grade is 82.8"
Where name and the numbers on that line are things typed in the console.
With my code:
it ends up looking like this :Java Code:import java.util.*; public class C4E8 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Enter a student record: "); String name = console.next(); int amount = console.nextInt(); printGPA(amount, name); } public static void printGPA(int amount, String name) { Scanner console = new Scanner(System.in); System.out.print(" "); double grade = console.nextDouble(); double sum = grade; for (int i = 2 ; i <= amount ; i++) { System.out.print(" "); grade = console.nextDouble(); sum += grade; } System.out.println(name + "'s grade is "+sum/amount); } }
"Enter a student record: Joe
3
30
20
10
Joe's grade is 20.0
"
How do I fix it so that the numbers typed in are on same line as where I type in the name?
- 01-16-2013, 04:00 PM #2
Re: Keeping console input on one line
Then you have to read the whole line at once and parse it. The console.nextXXX() causes a new line.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-16-2013, 04:01 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 22
- Rep Power
- 0
Re: Keeping console input on one line
Hi LasseA you just need to declare on console variable as a static member of the class. Because the problem is when using two Scanner the first run is taking the buffer of the console.
Instead of two Scanner, declare just one like this
static Scanner console = new Scanner(System.in);
Regards.Last edited by jfabian; 01-16-2013 at 04:04 PM.
- 01-16-2013, 04:08 PM #4
Re: Keeping console input on one line
How will that solve his problem? He's reading multiple inputs which are terminated by a CR/LF.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-16-2013, 04:59 PM #5
Re: Keeping console input on one line
Don't press Enter until all the data has been entered on one line. Every press of Enter moves to a new line.How do I fix it so that the numbers typed in are on same line as where I type in the name?If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
How to get input from Console
By karma in forum New To JavaReplies: 8Last Post: 08-13-2010, 09:32 PM -
How to erase line(s) from a console?
By Tamu in forum Advanced JavaReplies: 3Last Post: 11-02-2008, 02:00 PM -
who to take user input ,not by console...
By Shyam Singh in forum New To JavaReplies: 3Last Post: 06-13-2008, 10:09 PM -
how to take input from console in jsp
By veena in forum New To JavaReplies: 1Last Post: 05-06-2008, 04:39 AM -
Taking input from console
By Java Tip in forum Java TipReplies: 0Last Post: 11-05-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks