Reputation: 1
File customer = new File("Cus.txt");
Scanner readCustomer = new Scanner(customer);
while(readCustomer.hasNextLine())
{
String line = readCustomer.nextLine();
String delims = ", ";
String[] split = line.split(delims);
int arr = Integer.parseInt(split[0]);
int ser = Integer.parseInt(split[1]);
int qui = Integer.parseInt(split[2]);
int appt = Integer.parseInt(split[3]);
int appL = Integer.parseInt(split[4]);
Customer newCustomer = new Customer(arr, ser, qui, appt, appL);
customerList.add(newCustomer);
System.out.println("Customer arrival: " + newCustomer);
}readCustomer.close();
OUTPUT
912, 4, 922, 0, 0
915, 5, -1, 10, 10
918, 0, -1, 5, 5
920, 0, -1, 10, 10
925, 6, 930, 0, 0
CUS.TXT FILE
915, 5, -1, 925, 10,
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,
I'm seriously at a loss and have no idea how to fix this. Does anyone see any errors or why it cannot read in my split[4]? Why is it copying what's in the int appt value?
Upvotes: 0
Views: 76
Reputation: 1323
@ Jamie I'm not able to understand how are you getting your output because you are printing object not the values. I have changed nothing as such in your code and its working fine for me. You might have missed something very small. Use the below code and you will be able to get the desired output.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadingInputIncorrectly {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Customer> customerList=new ArrayList<>();
File customer = new File("path to your Cus.txt file");
Scanner readCustomer = new Scanner(customer);
while (readCustomer.hasNextLine()) {
String line = readCustomer.nextLine();
String delims = ", ";
String[] split = line.split(delims);
int arr = Integer.parseInt(split[0]);
int ser = Integer.parseInt(split[1]);
int qui = Integer.parseInt(split[2]);
int appt = Integer.parseInt(split[3]);
int appL = Integer.parseInt(split[4]);
Customer newCustomer = new Customer(arr, ser, qui, appt, appL);
customerList.add(newCustomer);
System.out.println(newCustomer.num1+", "+newCustomer.num2+", "+newCustomer.num3+", "+newCustomer.num4+", "+newCustomer.num5+", ");
}
readCustomer.close();
}
}
class Customer{
int num1,num2,num3,num4,num5;
Customer(int num1,int num2,int num3,int num4,int num5){
this.num1=num1;
this.num2=num2;
this.num3=num3;
this.num4=num4;
this.num5=num5;
}
}
Output
915, 5, -1, 925, 10,
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,
Cus.txt file
915, 5, -1, 925, 10,
918, 0, -1, 920, 5,
920, 0, -1, 915, 10,
925, 6, 930, -1, 0,
Let me know if it works.
Upvotes: 1