Results 1 to 7 of 7
  1. #1
    JavaJ is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Problem with scanner

    Good day...
    I am trying to get input from user through console ..
    the input is supposed to be assigned to proper variables
    I am using the scanner to get the four variables
    -1 or negative should quit.. While testing I don't get the desired result..
    I can't trace the problem, Any suggestions are appreciated..

    import java.io.*;
    import java.util.*;

    /**
    *
    *
    */
    public class CreateClientsFile {
    private PrintWriter pr;

    public void openFile() throws FileNotFoundException
    {
    try
    {
    pr = new PrintWriter("c:\\clients.txt ");
    }
    catch(FileNotFoundException e){
    System.out.println("There was a problem while opening clients.txt file");
    e.printStackTrace();
    }
    }

    public void addRecords(){
    System.out.println("To terminate input, type -1 for the id number.");
    System.out.println("Enter account id(>=0), first name, last name and balance");

    Scanner sc = new Scanner(System.in);

    int id = 0;
    while(id >= 0){
    try{
    id = sc.nextInt();
    if (id < 0)
    break;
    String firstName = sc.nextLine();
    String lastName = sc.nextLine();
    double balance = sc.nextDouble();
    }

    catch(InputMismatchException e)
    {
    System.out.println("The data you've typed isn't valid please try again!");
    sc.nextLine();
    }

    }}

    public void closeFile()
    {
    pr.close();
    }


    }

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

    Default

    It would help if you posted your code using code tags and actually describing the problem. The only hint for not getting the desired result is you have faulty code.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JavaJ is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your concern...

    The Goal : Get account records from the console " Id , FirstName, LastName, Balance", in order to write them to a file later on..
    The problem: I am using a scanner to get the input "One line is a record the space is the separator " but I can't assign the proper values to the variables, I am using nextIn, nextLine, nextLine, next double. -1 for Id means that the while loop should quit.
    Is there a problem with scanner delimiter?
    thanks for your help in advance
    PHP Code:
    import java.io.*;
    import java.util.*;
    
    /**
    *
    *
    */
    public class CreateClientsFile {
    private PrintWriter pr;
    
    public void openFile() throws FileNotFoundException
    {
    try
    {
    pr = new PrintWriter("c:\\clients.txt ");
    }
    catch(FileNotFoundException e){
    System.out.println("There was a problem while opening clients.txt file");
    e.printStackTrace();
    }
    }
    
    public void addRecords(){
    System.out.println("To terminate input, type -1 for the id number.");
    System.out.println("Enter account id(>=0), first name, last name and balance");
    
    Scanner sc = new Scanner(System.in);
    
    int id = 0;
    while(id >= 0){
    try{
    id = sc.nextInt();
    if (id < 0)
    break;
    String firstName = sc.nextLine();
    String lastName = sc.nextLine();
    double balance = sc.nextDouble();
    }
    
    catch(InputMismatchException e)
    {
    System.out.println("The data you've typed isn't valid please try again!");
    sc.nextLine();
    }
    
    }}
    
    public void closeFile()
    {
    pr.close();
    }
    
    
    }
    Desired output should look something like this :
    Java Code:
    To terminate input, type -1 for the id number.
    Enter account id(>=0), first name, last name and balance
    > 28 John Smith 100.9
    Last edited by JavaJ; 03-16-2010 at 08:34 PM.

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

    Default

    Ok, the first problem is that you won't get the input in one line. You have to either get the input in one line using nextLine() and then split it, or you get the input one by one. Hint: put a sysout in front of every Scanner call:
    Java Code:
    try{
      System.out.println("Enter the ID);
      id = sc.nextInt();
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Vio
    Vio is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    18
    Rep Power
    0

    Default

    Can't he use the .useDelimiter(" ") where a whitespace is used as delimiter between inputs.
    Java Code:
    String firstName = sc.next();
    String lastName = sc.next();
    double balance = sc.nextDouble();
    should do it then I think.

    And try not to use the break.
    Last edited by Vio; 03-16-2010 at 09:29 PM.

  6. #6
    JavaJ is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your help
    next() for strings worked perfectly...

    but I didn't understand :
    And try not to use the break.

    The code is now working ok with if + break , if I remove them the while loop won't exist when -1 is inputed

  7. #7
    Vio
    Vio is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    18
    Rep Power
    0

    Default

    Well my professor always told me, try not to use the break. there should be more elegant solutions, for example you could do this:
    Java Code:
    while(id >= 0){
       try{
           id = sc.nextInt();
           if (id >= 0){
               String firstName = sc.next();
               String lastName = sc.next();
               double balance = sc.nextDouble();
           }
    ...
    ...

Similar Threads

  1. Problem with while loop and the scanner method
    By hhh80p in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 12:47 PM
  2. problem with Scanner in Getting users input
    By kliane in forum New To Java
    Replies: 8
    Last Post: 01-17-2010, 05:37 PM
  3. Problem with scanner
    By Kangaroo128 in forum New To Java
    Replies: 11
    Last Post: 09-01-2009, 09:07 AM
  4. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 01:06 AM
  5. Scanner input problem
    By slayer_azure in forum New To Java
    Replies: 3
    Last Post: 05-26-2008, 11:49 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
  •