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

    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
    Christina Alve
    Peter Fort

    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())

    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
    Rep Power

    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, 10:16 AM
  2. How to use Scanner ?
    By sandeep43 in forum New To Java
    Replies: 7
    Last Post: 08-10-2011, 02:29 PM
  3. Scanner to int?
    By N00Bie in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 01:14 AM
  4. hasNext call
    By scotts in forum New To Java
    Replies: 4
    Last Post: 06-20-2010, 04:58 PM
  5. Using scanner.hasNext() but recognize return
    By ScKaSx in forum New To Java
    Replies: 5
    Last Post: 01-27-2009, 11: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