2016-10-16 9 views
0

私はJavaを使い慣れていないため、テキストファイルから行ごとに車両を読み込み、車、トラック、自転車、アメリカ車、外国車)は、これらの車の後ろの線を通り、車を物に変換するコンストラクトに渡して、それらのオブジェクトを配列に入れることができます。車両は、お互いからテキストファイルの特定のレイアウトで拡張されます。私は、これらの行をコンストラクタに渡してコンストラクタからオブジェクトを返す方法を考え出すのに問題があります。次に、テキストファイルのレイアウトの例を示します。テキストファイルから行単位で車両を読み込み、Javaのコンストラクタにそれらの車両を渡す

vehicle 
owner's name (string) 
address (string) 
phone (string) 
email (string) 

car - extends of vehicle 
owner's name (string) 
address (string) 
phone (string) 
email (string) 
true or false for convertible (boolean) 
color (string) 

american car - extends of car 
owner's name (string) 
address (string) 
phone (string) 
email (string) 
true or false for convertible (boolean) 
color (string) 
true or false for made in Detroit (boolean) 
true or false for union shop (boolean) 

foreign car - extends of car 
owner's name (string) 
address (string) 
phone (string) 
email (string) 
true or false for convertible (boolean) 
color (string) 
country of manufacturer (string) 
import duty (float) 

bicycle - extends of vehicle 
owner's name (string) 
address (string) 
phone (string) 
email (string) 
number of speeds (int) 

truck - extends of vehicle 
owner's name (string) 
address (string) 
phone (string) 
email (string) 
number of tons (float) 
cost of truck (float) 
date purchased (format below in example) 

また、ここで私が使用していたサンプルデータさ:

foreign car 
aMarioy 
Mario's house 
(777) 777-7777 
[email protected] 
false 
black 
Italy 
4415.91 

truck 
aDougy 
Doug's house 
(123) 456-7890 
[email protected] 
30 
61234.56 
8/10/2003 

vehicle 
aRobby 
Rob's house 
(987) 654-3210 
[email protected] 

bicycle 
bTommy 
Tom's house 
(246) 810-1214 
[email protected] 
7 

truck 
bGeorge 
George's house 
(666) 666-6666 
[email protected] 
25 
51234.56 
12/4/2004 

vehicle 
bTim 
Tim's house 
(111) 111-1111 
[email protected] 

bicycle 
bJim 
Jim's house 
(555) 555-5555 
[email protected] 
5 

american car 
bJohn 
John's house 
(888) 888-8888 
[email protected] 
true 
green 
false 
true 

car 
cKen 
Ken's house 
(999) 999-9999 
[email protected] 
false 
orange 

foreign car 
cMario 
Mario's house 
(777) 777-7777 
[email protected] 
false 
black 
Italy 
4415.91 

truck 
zDoug 
Doug's house 
(123) 456-7890 
[email protected] 
30 
61234.56 
8/10/2003 

vehicle 
eRob 
Rob's house 
(987) 654-3210 
[email protected] 

bicycle 
fTom 
Tom's house 
(246) 810-1214 
[email protected] 
7 

american car 
gSam 
Sam's house 
(333) 333-3333 
[email protected] 
false 
blue 
true 
false 

をここでは私のコードです...

import java.io.FileNotFoundException; 
import java.util.Scanner; 
import java.io.File; 
import java.util.ArrayList; 

public class test4{ 
    public static void main(String[] args)throws FileNotFoundException { 
     try{ 
     Scanner file = new Scanner(new File(args[0])); 
     //ArrayList<Vehicle> vehicleList = new ArrayList<>(); 
     String line = new String(); 
     String newLine = new String(); 
     ArrayList<String> s1List = new ArrayList<>(); 
     ArrayList<String> s2List = new ArrayList<>(); 
     ArrayList<String> s3List = new ArrayList<>(); 
     ArrayList<String> s4List = new ArrayList<>(); 
     ArrayList<String> s5List = new ArrayList<>(); 
     ArrayList<String> s6List = new ArrayList<>(); 

     while(file.hasNextLine()){ 
      line = file.nextLine(); 
      if(line.equals("vehicle")){ 
       while(file.hasNextLine()){ 
       newLine = file.nextLine(); 
       if(!(newLine.equals(""))){ 
       s1List.add(newLine); 
       } 
       if(newLine.equals("")){ 
        break; 
       } 
       } 
      } 
      if(line.equals("truck")){ 
       while(file.hasNextLine()){ 
       newLine = file.nextLine(); 
       s2List.add(newLine); 
       if(newLine.equals("")){ 
        break; 
       } 
       } 
      } 
      if(line.equals("bicycle")){ 
       while(file.hasNextLine()){ 
       newLine = file.nextLine(); 
       s3List.add(newLine); 
       if(newLine.equals("")){ 
        break; 
       } 
       } 
      } 
      if(line.equals("car")){ 
       while(file.hasNextLine()){ 
       newLine = file.nextLine(); 
       s4List.add(newLine); 
       if(newLine.equals("")){ 
        break; 
       } 
       } 
      } 
      if(line.equals("american car")){ 
       while(file.hasNextLine()){ 
       newLine = file.nextLine(); 
       s5List.add(newLine); 
       if(newLine.equals("")){ 
        break; 
       } 
       } 
      } 
      if(line.equals("foreign car")){ 
       while(true){ 
       newLine = file.nextLine(); 
       s6List.add(newLine); 
       if(newLine.equals("")){ 
        break; 
       } 
       } 
      }                
     } 
     /* 
     String[] s1 = s1List.toArray(new String[0]); 
     String[] s2 = s2List.toArray(new String[0]); 
     String[] s3 = s3List.toArray(new String[0]); 
     String[] s4 = s4List.toArray(new String[0]); 
     String[] s5 = s5List.toArray(new String[0]); 
     String[] s6 = s6List.toArray(new String[0]); 
     */ 

     /* 
     System.out.println(s1.length); 


     for(String a: s1){ 
      System.out.println(a); 
     } 
     */ 


     /* 
     for(String a: s1List){ 
      System.out.println(a); 
     } 
     for(String a: s2List){ 
      System.out.println(a); 
     } 
     for(String a: s3List){ 
      System.out.println(a); 
     } 
     for(String a: s4List){ 
      System.out.println(a); 
     } 
     for(String a: s5List){ 
      System.out.println(a); 
     } 
     for(String a: s6List){ 
      System.out.println(a); 
     } 
     */ 



     }catch(FileNotFoundException ex){ 
     System.out.println("File not found."); 
     } 
    } 
    public boolean equals(Object o){ 
     if(this == o){ 
     return true; 
     } 
     if(o == null){ 
     return false; 
     } 
     return false; 
    } 
} 


class Vehicle{ 
    private String owner = new String(); 
    private String address = new String(); 
    private String phone = new String(); 
    private String email = new String(); 

    public Vehicle(String[] strArr){ 
     owner = strArr[0]; 
     address = strArr[1]; 
     phone = strArr[2]; 
     email = strArr[3]; 
    } 
} 

私は現在、テキストをスキャンするプログラムを持っています車両のためのファイルを作成し、空の行に停止するarraylistsに順次線を格納します。私は車両が常に特定のレイアウトであるが、異なる順序になるので、arraylistからコンストラクタに特定のインデックスを渡す方法はわかりません。私はarraylistのインデックスを数えて、コンストラクタに渡すことができるものを見つけることができます。

ありがとうございました。

答えて

2

入力ファイルをCSVに変換できますか?そして、あなたは、単にあなたがコンストラクタに渡す必要があるものは何でも/その車/車/トラックのためにすべてのデータを単一のラインを読み、持つことができます

https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/

...読み込み、解析する方がはるかに簡単になります。

もしそうでなければ、私は次のようなことをするでしょう。注 - これはC#にありますが、Javaでも同じことができます。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace VehicleParsing 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Will maintain the string in the current line 
      string line; 

      // Contains the vehicles 
      List<Vehicle> vehicles = new List<Vehicle>(); 

      // Contains all properties associated with the current vehicle being looked at 
      List<string> currentVehicleData = new List<string>(); 

      // Read the file and display it line by line. 
      System.IO.StreamReader file = new System.IO.StreamReader("C:/Users/Timot/Desktop/vehiclesample.txt"); 
      while ((line = file.ReadLine()) != null) 
      { 
       // Indicates we have read in all properties of the current vehicle 
       if(String.IsNullOrEmpty(line)) 
       { 
        string vehicleType = currentVehicleData[0]; 
        switch (vehicleType) 
        { 
         case "foreign car": 
          vehicles.Add(new ForeignCar(currentVehicleData)); 
          break; 
         case "truck": 
          vehicles.Add(new Truck(currentVehicleData)); 
          break; 
         case "vehicle": 
          vehicles.Add(new Vehicle(currentVehicleData)); 
          break; 
         case "bicycle": 
          vehicles.Add(new Bicycle(currentVehicleData)); 
          break; 
         case "american car": 
          vehicles.Add(new AmericanCar(currentVehicleData)); 
          break; 
         case "car": 
          vehicles.Add(new Car(currentVehicleData)); 
          break; 
         default: 
          throw new NotImplementedException(); 
        } 

        currentVehicleData = new List<string>(); 
       } 

       // Indicates the current vehicle's data is still being read 
       else 
       { 
        currentVehicleData.Add(line); 
       } 
      } 

      file.Close(); 
     } 
    } 
} 

とJavaで:テキストファイルから情報を読み取る際に

package com.company; 

import sun.reflect.generics.reflectiveObjects.NotImplementedException; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     // Holds the list of all vehicles found in the file 
     ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>(); 

     // Holds the data associated with the current vehicle being read 
     ArrayList<String> currentVehicleData = new ArrayList<String>(); 

     // Java knowledge lacking here - not certain why I can't simply let the FNF exception bubble up 
     Scanner file = null; 
     try { 
      file = new Scanner(new File("C:/Users/Timot/Desktop/vehiclesample.txt")); 
     } catch(FileNotFoundException fnfe) { 
      // Do something with this exception 
      return; 
     } 

     String line; 
     while(file.hasNextLine()) { 
      // Get next line 
      line = file.nextLine(); 

      // Next line is empty, so currentVehicleData has all the data associated the current vehicle 
      if(line.isEmpty()) { 
       vehicles.add(GetVehicle(currentVehicleData)); 
       currentVehicleData = new ArrayList<String>(); 
      } 

      // Still reading data for the current vehicle 
      else { 
       currentVehicleData.add(line); 
      } 
     } 

     // Add final vehicle 
     vehicles.add(GetVehicle(currentVehicleData)); 

     file.close(); 
    } 

    public static Vehicle GetVehicle(ArrayList<String> properties) 
    { 
     String vehicleType = properties.get(0); 
     switch(vehicleType) { 
      case "foreign car": 
       return new ForeignCar(properties); 
      case "truck": 
       return new Truck(properties); 
      case "vehicle": 
       return new Vehicle(properties); 
      case "bicycle": 
       return new Bicycle(properties); 
      case "american car": 
       return new AmericanCar(properties); 
      case "car": 
       return new Car(properties); 
      default: 
       throw new NotImplementedException(); 
     } 
    } 
} 
+0

こんにちは。私はあなたのif(String.IsNullOrEmpty(line))ステートメントのロジックを理解していません。その声明は、スイッチを内部で実行していない時間のほとんどを間違って返すのではないでしょうか? –

+0

if文は、テキストファイルに空行が見つかるたびにtrueと評価されます。これは、新しいビークルが定義されている場合も同様です。しかし、その時点まで、その空の線の前のすべてが単一のビークルのプロパティを構成します。だから、私がやっているのは、リスト内のすべてのプロパティを集めることです。次の行が空のときは、次の車両のプロパティを収集できるように、新しい車両オブジェクトを作成してプロパティリストを空にする –

+0

こんにちは。車のプロパティが終了した時を知るために空きスペースをチェックしているので、空きスペースとファイルの終わりをどうやって確認できますか?なぜなら、テキストファイルが空のスペースで終わらないと、最後のビークルをコンストラクタに渡さないからです。 –

-1

は、switchステートメントを使用します。それははるかに簡単になります!

関連する問題