2017-04-06 5 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(); 

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ファイル

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

は、私は途方に暮れて真剣だとこの問題を解決する方法は考えています。誰もが何かエラーを見たり、なぜ私の分割[4]で読むことができないのですか? int appt値に何がコピーされているのですか?

+1

カスタムコンストラクタに重複した 'appL'を説明するエラーがあると思われます。それかtoString()メソッドです。 – Gray

+0

私はこれを解決するためにデバッガを使うことを学びたいと思います。 http://www.vogella.com/tutorials/EclipseDebugging/article.html – Gray

+0

お客様のコンストラクタの実装をご紹介してください。 –

答えて

1

@ Jamie値ではなくオブジェクトを印刷しているので、出力をどのように取得しているのか分かりません。私はあなたのコードでは何も変えておらず、私のためにうまく働いています。非常に小さいものを見逃したかもしれません。以下のコードを使用すると、目的の出力を得ることができます。

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

この(または任意の)回答が役に立つ場合は、それをアップしてください。これがあなたの質問に答えた場合は、それを受け入れられた回答としてマークしてください。ありがとう! –

関連する問題