2017-04-06 46 views
0
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(); 

輸出掃描儀無法讀取輸入正確

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, 

我是認真在虧損,而且不知道如何解決這個問題。有沒有人看到任何錯誤或爲什麼它不能讀取我的分裂[4]?爲什麼要複製int appt值中的內容?

+1

我懷疑你的Custom構造函數中有一個錯誤,它解釋了重複的'appL'。或者那個或者toString()方法。 – Gray

+0

我會學習使用調試器來解決這個問題。 http://www.vogella.com/tutorials/EclipseDebugging/article.html – Gray

+0

請您向我們展示客戶構造函數的實現 –

回答

1

@傑米我無法理解你如何獲得你的輸出,因爲你打印的對象不是值。我在你的代碼中沒有改變任何東西,它對我的​​工作很好。你可能錯過了一些非常小的東西。使用下面的代碼,你將能夠得到所需的輸出。

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; 
    } 
} 

輸出

915, 5, -1, 925, 10, 
918, 0, -1, 920, 5, 
920, 0, -1, 915, 10, 
925, 6, 930, -1, 0, 

Cus.txt文件

915, 5, -1, 925, 10, 
918, 0, -1, 920, 5, 
920, 0, -1, 915, 10, 
925, 6, 930, -1, 0, 

讓我知道,如果它的工作原理。

+0

如果您發現此(或任何)答案有幫助,請立即加註。如果這回答了您的問題,請將其標記爲已接受的答案。謝謝! –