Results 1 to 5 of 5
  1. #1
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default 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:
    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);
    	}
    }
    it ends up looking like this :
    "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?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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

  3. #3
    jfabian is offline Member
    Join Date
    Jun 2012
    Posts
    22
    Rep Power
    0

    Default 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.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default Re: Keeping console input on one line

    How do I fix it so that the numbers typed in are on same line as where I type in the name?
    Don't press Enter until all the data has been entered on one line. Every press of Enter moves to a new line.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. How to get input from Console
    By karma in forum New To Java
    Replies: 8
    Last Post: 08-13-2010, 09:32 PM
  2. How to erase line(s) from a console?
    By Tamu in forum Advanced Java
    Replies: 3
    Last Post: 11-02-2008, 02:00 PM
  3. who to take user input ,not by console...
    By Shyam Singh in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 10:09 PM
  4. how to take input from console in jsp
    By veena in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 04:39 AM
  5. Taking input from console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •