java - Scanner not reading input correctly -


 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 @ loss , have no idea how fix this. see errors or why cannot read in split[4]? why copying what's in int appt value?

@ jamie i'm not able understand how getting output because printing object not values. have changed nothing such in code , working fine me. might have missed small. use below code , able 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 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 works.


Comments