2016-09-21 3 views
0

リンクされたリストを使用してテキストファイルを読みました。リンクされたリストの複数の要素を持つインデックスの製品

20 10 8 

4.5 8.45 12.2 

8.0 2.5 4.0 

1.0 15.0 18.0 

3.5 3.5 3.5 

6.0 5.0 10.0 

それぞれの列は、それぞれ長さ、幅、高さを表します。

テキストファイルを読むと、各行は1つのノードとして扱われます。 私は、各行の製品、ボリュームを見つける方法があるかどうか確認しようとしていますか?

もし私がgetVolume()メソッドを作成した別のクラスから個別に(l、w、h)読み込んだ値を呼び出す方法を見つける必要があります。

これまでのところ私のコードです:常にあなたのすべての助けを事前に感謝しています!


import java.util.LinkedList; 
import java.util.*; 
import java.io.*; 

public class Runtime2 { 

    public static void main(String args[])throws Exception { 
     String content = new String(); 
     int count=0; 
     File file = new File("dims.txt"); 
     LinkedList<String> list = new LinkedList<String>(); 

     try { 
      Scanner sc = new Scanner(new FileInputStream(file)); 
      while (sc.hasNextLine()){ 
       content = sc.nextLine(); 
       list.add(content); 
      } 
      sc.close(); 
     } 

     catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println(); 
     } 


     Iterator i = list.iterator(); 
     while (i.hasNext()) { 
      System.out.print("Node " + count++ + ": "); 
      System.out.println(i.next()); 
     } 

public class Box { 
    double length; 
    double width; 
    double height; 

    Box(double l, double w, double h) { 
     length=l; 
     width=w; 
     height=h; 
    } 


    double getVolume() { 
     double vol=length*width*height; 
     return vol; 
    } 


    boolean isCube() { 
     if(length==width && width==height) { 
      System.out.println("The box is a cube."); 
     } 
     else 
      System.out.println("The box is not a cube."); 

     return true; 
    } 
} 

答えて

0
private static double getVol(String line) 
    // line = "2 3 4.5" 
    String[] nums = line.split(" "); 
    int len = nums.length; 
    double vol = 1; 
    for (int i = 0 ; i < len; i++){ 
     vol*=Double.parseDouble(nums[i]); 
    } 
    return vol; 
各行/列のため
0

使用nextDouble三回:あなたのBoxクラスを定義するのは良い仕事をしてきたDemo

double a, b, c; 
    while(sc.hasNextLine()){ 
     a = sc.nextDouble(); 
     b = sc.nextDouble(); 
     c = sc.nextDouble(); 
     Box box = new Box(a, b, c);   //System.out.println(a + " " + b + " " + c + "\n"); 
     System.out.println("Volm of box with dimension {" + a + "," + b + "," + c + "} is :" + 
           box.getVolume() + "\n"); 
    } 
0

をエンコードします今のところファイルの各行。あなたが欠けている部分は、ファイルの各行からBoxオブジェクトへのマッピングです。

new演算子:new Box(l, w, h)を使用して、新しいBoxオブジェクトを作成できます。

次のステップは、ファイル内の行から長さ、幅、高さを取得することです。あなたはすでに、変数を変数にする方法を考え出しました。次のステップは、Stringsplit methodを使用して値を抽出することです。たとえば:

String string = "a b c"; 
String[] values = string.split(" "); 
// values[0] now contains "a"; 
// values[1] now contains "b"; 
// values[2] now contains "c"; 

最後に、あなたのリストに新しい `ボックスを追加するListaddメソッドを使用します。私はあなたがこのような何か試みる場合がありますので、ファイルの読み取りは、独自の方法の中にカプセル化されるようにコードを構造化お勧めします。私は、輸入し、簡潔にするためBoxクラスの定義を省略しました

public class Runtime2 { 

    public static void main(String args[]) throws Exception { 
     List<Box> boxes = readBoxesFromFile(new File("dims.txt")); 

     for (int i = 0; i < boxes.size(); i++) { 
      System.out.println("Box " + i + " volume: " 
       + boxes.get(i).getVolume()); 
     } 
    } 

    private static List<Box> readBoxesFromFile(File file) { 
     List<Box> boxes = new LinkedList<Box>(); 

     /* 
     * - Create File object 
     * - Create Scanner for File 
     * - For each line in the file 
     *  Split the line into a string array 
     *  Create a 'Box' instance 
     *  Add the Box to your list 
     */ 

     return boxes; 
    } 

} 

を、しかし、あなたはすでにあなたの質問の中で定義されたものを持っています。

これを念頭に置いて、ファイルをListBoxオブジェクトに変換する方法を構築するために、これらの部分をまとめてみてください。

関連する問題