0

I have this code currently:

input = new Scanner(new File("src/data/McDonalds Regular.csv"));
input.useDelimiter(",|\\n|$");
String tempChoice = input.next();
double price = input.nextDouble();
System.out.println(price + tempChoice);

with the input file being: Double Cheeseburger,$1.59,Beef,

My expected output is 1.59Double Cheesburger

instead I get a type mismatch error because when I do input.next() instead of input.nextDouble() it appears its scanning the token as $1.59, instead of just 1.59 like it should.

73est
  • 185
  • 2
  • 9

2 Answers2

1

Well let's assess what you've told it shall we?

  1. Each token is split by a comma.
  2. By calling next double, you've said that what comes next is a double.

So when it sees $ at the start, it's panicking! What the hell is this $ thing? What does it mean? Who are its Gods?

You haven't told it that your double starts with a dollar symbol. Instead, I would parse it as a String and follow this question to parse the String into a double.

Note: A double using floating point representation, so it is not advisable to represent something like money with it.

Edit

As a response to your comment, an alternative would be to add another possibility to your Regex.

,\\$|,|\\n|$

You can see a working example in this IDEOne.

Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89
  • yes but does the scanner not split by `',' OR \n OR '$'` I thought thats what line 2 set – 73est Mar 15 '15 at 19:13
  • 1
    Well it's hitting the comma first. The Regex parser will say *Is there a comma here?*. It hits the comma and says *Why yes there is! Split!*. You're not telling it that IF there is a `$` symbol right after the comma, split on that. – christopher Mar 15 '15 at 19:16
  • I understand. So could I use the delimeter (",$|\\n|,")? Edit: Didnt notice your edit before commenting, answers my follow up. – 73est Mar 15 '15 at 19:24
1

I would do the following :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = null;
        try {
            input = new Scanner(new File("c://McDonalds Regular.csv"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        input.useDelimiter(",");
        String curr = null;
        String tempChoice = "";

        double price = 0;

        while(input.hasNext()){
            curr = input.next();
            if(curr.startsWith("$")) price = Double.parseDouble(curr.substring(1)) ;
            else 
                tempChoice += curr;
        }

        System.out.println(price + " "+tempChoice);

    }

}

If you use multiple lines for input later, it would be better to use An ArrayList of String to store the input lines and split first on new line "/n" and then parse each line with a while loop or a Regex but Regex are difficult to debug...

firephil
  • 830
  • 10
  • 18
  • I don't understand the reasoning, and what `else{tempChoice += curr}` accomplish. – 73est Mar 15 '15 at 20:15
  • it adds to the order String a String token (in reallity a new String is created because Strings are immutable) – firephil Mar 15 '15 at 20:18