Results 1 to 2 of 2
  1. #1
    Lutro is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Need help with Scanner/hasNext

    Hey I'm new to java and having trouble with one of my assignments. I have a txt file that looks like this:

    Mike Johnson
    1590.45
    Christina Alve
    450.50
    Peter Fort
    1342.12
    etc..
    etc

    It's names with their corresponding sales listed consecutively. I have to calculate the payroll and display the output listing the names only in one column and Commission from sales in another. Iv'e figured out how to only get the names using hasNextDouble and nextLine. Otherwise it would keep giving me a Mismatched exception error because the first line is a string.

    while (inputFile.hasNext())
    {
    String str = inputFile.nextLine();
    if (inputFile.hasNextDouble())
    {
    inputFile.nextLine();
    System.out.println(str);

    So my question is how do I extract the sales individually and store them so I can calculate commission/bonus? I can't hardcode any of the numbers and limited to using scanner only.

    Any help is appreciated, thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Need help with Scanner/hasNext

    The Scanner class has a small quirk. The nextLine method will read an end-of-line but the nextDouble(nextInt etc) methods do not read the EOL. So you must do that yourself if you want to continue reading data.

Similar Threads

  1. hasNext, Ctrl-Z not working
    By kahwawashay in forum New To Java
    Replies: 15
    Last Post: 05-12-2012, 09:16 AM
  2. How to use Scanner ?
    By sandeep43 in forum New To Java
    Replies: 7
    Last Post: 08-10-2011, 01:29 PM
  3. Scanner to int?
    By N00Bie in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 12:14 AM
  4. hasNext call
    By scotts in forum New To Java
    Replies: 4
    Last Post: 06-20-2010, 03:58 PM
  5. Using scanner.hasNext() but recognize return
    By ScKaSx in forum New To Java
    Replies: 5
    Last Post: 01-27-2009, 10:11 AM

Posting Permissions

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