I want to delimit ,\\s
but I also want to delimit \n
, what I thought of so far was ,\\s||\n
but this didn't work, anyone got an idea? It of course worked as a delimiter, but it gave back IPHONE , 7.0, 4, ., 7, A, false, 0
whilst I want back IPHONE 7, 4.7, A10, GSM, JET BLACK, 32GB, TRUE, 700
The file I'm scanning is this:
IPHONE 7, 4.7, A10, GSM, JET BLACK, 32GB, TRUE, 700
IPAD AIR 2, 9.7, A8, TRUE, SILVER, 64GB, 400
The code I'm using to scan it, is this:
public static iPhone read(Scanner sc) {
boolean touchtech = false;
//int price = 12;
sc.next();
sc.useDelimiter(",\\s||\n");
String model = sc.next();
double screensize = sc.nextDouble();
String processor = sc.next();
String modem = sc.next();
String color = sc.next();
String memory = sc.next();
String touchtechtest = sc.next();
if(touchtechtest.equals("TRUE")) {
touchtech = true;
}
int price = sc.nextInt();
sc.close();
iPhone res = new iPhone(model, screensize, processor, modem, color, memory, touchtech, price);
return res;
}
Sometimes String.class itself is more than enough to handle your needs. Why don't you split the line instead using regex and operate on the result? e.g.
public static iPhone read(Scanner sc) { // Better yet just make it received a String
final String line = sc.nextLine();
final String [] result = line.split("(,)\\s*");
// count if the number of inputs are correct
if (result.length == 8) {
boolean touchtech = false;
final String model = result[0];
final double screensize = Double.parseDouble(result[1]);
final String processor = result[2];
final String modem = result[3];
final String color = result[4];
final String memory = result[5];
final String touchtechtest = result[6];
if(touchtechtest.equals("TRUE")) {
touchtech = true;
}
final int price = Integer.parseInt(result[7]);
return new iPhone(model, screensize, processor, modem, color, memory, touchtech, price);
}
return new iPhone();// empty iphone
}