2014-12-02 71 views
0

如果有人可以请帮我解决这个简单的任务,我会永远感谢!我对Java非常陌生,无法理解如何去调试这个简单的代码。简单的java方法使用text.io

我需要:

•阅读对各行

上市的冰淇淋口味的外部文件•计数冰淇淋

总数•伯爵冰的数量名称为“草莓”

面霜•计算是“草莓”

这是我的冰淇淋的百分比就是我有这么远......

package icecreamcounter; 

import java.io.*; //Import TextIO 

public class IcecreamCounter { 

public static void main(String[] args) { 
    // Open file for reading; if it cannot be opened, end the program 
     try { 
    TextIO.readFile("icecream.dat"); 
    } 
    catch (IllegalArgumentException e) { 
    System.out.println("Can't open file \"icecream.dat\" for reading!"); 
    System.out.println("Please make sure the file is present before"); 
    System.out.println("running the program."); 
    System.exit(1); // Terminates the program. 

    //read the file 
    string flavor; 
    int strawb; //Total number of strawberry icecreams 
    int totalIcecream; // Total number of icecreams 
    double percentageStrawb; //Percentage of icecreams which are strawberry 

    strawb = 0; 
    percentageStrawb = (totalIcecream - strawb)/totalIcecream; 

    while (!TextIO.eof()) { // process one line of data. 
     flavor = TextIO.getlnString(); // Get the rest of the line. 
     totalIcecream = totalIcecream + 1; // Add one to the count of total icecreams 
     return totalIcecream; 
     if (flavor.equals("Strawberry") { // If icecream name is strawberry 
      strawb=strawb+1;   //then add one to the count of strawberry 
      return strawb; 
     } 
    } 

    System.out.println("Total number of cones is" totalIcecream); 
    System.out.println("Total number of strawberry cones is" strawb); 
    System.out.println("Percentage of strawberry cones is" percentageStrawb); 
    } 
} 

}

遇到的问题可能与读取文件的一部分,但它也让我在底部错误,当我尝试打印答案。另外,我是否必须返回值例如'totalIcecream'以便稍后打印出来?任何帮助将非常感激!

+0

第一件事,在'System.exit(1)',而不是对整个里面的程序后,要关闭'catch'。 – RealSkeptic 2014-12-02 22:07:41

+0

您是否强制使用TextIO? – 2014-12-02 22:17:20

+0

不幸的是:(这是一个任务,特别要求使用TextIO。 – Jessica 2014-12-03 03:09:44

回答

0

首先,在system.exit(1)之后,关闭catch块,因为之后的代码永远不会到达。

其次,在println,它被认为是,"this is some text" + somevariable

添加字符串变量或数为字符串要求一个加号。

0

我建议使用NIO,对我来说完美的作品,也很容易理解,这里使用Java的NIO API我的做法(可自从Java 1.5)

try { 
     Path pathToMyTextFile = Paths.get("c:/data/icecream.dat"); 

     //Here you must use the Charset according to the encoding of your TEXT file (I use ISO_8859_1) 
     List<String> linesInFile = Files.readAllLines(pathToMyTextFile, StandardCharsets.ISO_8859_1); 

     int totalNumberOfIceCreams = linesInFile.size(); 
     String targetFlavor = "Strawberry";//I would like to handle this in a variable 
     int numberOfTargetFlavor = 0; 

     for (String iceCreamLine : linesInFile) { 
      if (iceCreamLine.equals(targetFlavor)) { 
       numberOfTargetFlavor++; 
      } 
     } 

     //Here I calculate the percentage: 
     double percentage = (numberOfTargetFlavor * 100)/totalNumberOfIceCreams; 

     System.out.println("Total number of Ice Creams: " + totalNumberOfIceCreams); 
     System.out.println("Total number of " + targetFlavor + " Ice Creams: " + numberOfTargetFlavor); 
     System.out.println("Percentage of Ice Creams that are flavor " + targetFlavor + ": " + percentage + "%"); 

    } catch (IOException e) { 
     //Control your exception here 
     e.printStackTrace(); 
    } 

我使用一个文件称为“icecream.dat”,它位于我的c:/ data文件夹中。这个文件的内容如下:

Strawberry 
Chocolate 
Strawberry 
Orange 
Cheese 
Strawberry 
Raspberry 
Apple 
Strawberry 

我得到的输出是:

Total number of Ice Creams: 9 
Total number of Strawberry Ice Creams: 4 
Percentage of Ice Creams that are flavor Strawberry: 44.0% 

我知道这是不是TEXTIO,但也是一个不错的办法。如果您强制使用TextIO进行练习,那么现在如果您将来需要使用新的方法。

问候和快乐编码:)

+1

不幸的是我需要使用Text.IO,但是谢谢你,我一定会在将来尝试! – Jessica 2014-12-02 23:15:57