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