2016-09-21 57 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 

每列分别代表长度,宽度和高度。

当我读取文本文件时,每行被视为单个节点。 我想看看有没有办法找到每一行的产品,音量?

如果这是不可能的,那么我需要找到一种方法来从另一个类中创建一个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三次每行/列: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类的好工作编码到目前为止您的文件的每一行。缺少的内容是将文件的每一行映射到Box对象。

您可以使用new运算符:new Box(l, w, h)来创建新的Box对象。

下一步是从文件中的一行中获取长度,宽度和高度。您已经计算出如何将该行变为String变量。下一步是使用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方法到新的`盒子追加到列表中。我建议构建你的代码,以使文件读取它自己的方法中封装,所以你可以尝试这样的事:

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

} 

我省略了进口,并Box类简洁的定义,但你已经在你的问题中定义了那些。

考虑到这一点,请尝试将这些部分组合在一起,以构建将文件转换为Box对象的List的方法。