2013-11-01 45 views
0

所以我有只有这一个.txt文件的内容:如何从文本文件中获取数字,而忽略前面的单词?

pizza 4 
bowling 2 
sleepover 1 

我想要做的是,例如在第一线,忽略了“比萨”的一部分,但保存为4一个整数。

这是我迄今为止的一小段代码。

public static void addToNumber() { 

    PrintWriter writer; 
    Int pizzaVotes, bowlingVotes, sleepOverVotes; 

    try { 
    writer = new PrintWriter(new FileWriter("TotalValue.txt")); 
    } 
    catch (IOException error) { 
    return; 
    } 


    // something like if (stringFound) 
    //  ignore it, skip to after the space, then put the number 
    //  into a variable of type int 
    //  for the first line the int could be called pizzaVotes 

     // pizzaVotes++; 

     // then replace the number 4 in the txt file with pizzaVote's value 
     // which is now 5. 
     // writer.print(pizzaVotes); but this just overwrites the whole file. 

     // All this will also be done for the other two lines, with bowlingVotes 
     // and sleepoverVotes. 

     writer.close(); 

    } // end of method 

我是初学者。正如你可以看到我的实际功能代码很短,我不知道继续。如果有人愿意指点我的方向,即使你只是给我一个网站的链接,这将是非常有益的...

编辑:我愚蠢以为PrintWriter可以读取文件

+4

'PrintWriter'只用于*写* - 它不读*。 –

+0

您必须首先**读取**数据,**将它们存储在适当的数据结构中,**处理**它们,然后将它们**写回到文件中。我建议你写最后写入文件的部分。 – Ingo

+0

在创建读取数据的代码之后,您可以使用“正则表达式”来获取数字,但这会让您遇到另一个问题:如果某一行有多个数字,例如'比萨4保龄球5'... –

回答

0

这其实很简单。所有你需要的是一个扫描仪,它的功能nextInt()

 // The name of the file which we will read from 
     String filename = "TotalValue.txt"; 

     // Prepare to read from the file, using a Scanner object 
     File file = new File(filename); 
     Scanner in = new Scanner(file); 

     int value = 0; 

     while(in.hasNextLine()){ 
      in.next(); 
      value = in.nextInt(); 
      //Do something with the value here, maybe store it into an ArrayList. 
     } 

我还没有测试此代码,但它应该工作,但在while循环value将是当前值的当前行。

+0

我试过这个,它给了我一个InputMismatchException错误... – user2946455

+0

但是,如果我从.txt文件中删除字符串,它会得到并保存值就好了......问题是我需要在文件中包含名称 – user2946455

+0

把'in.next();'放在'value = in.nextInt();'作为while循环中的第一行,看看会发生什么。 –

0

我不完全明白你的问题,所以发表评论,如果你想更清楚一些建议

这是一个常见的模式,你会在Java中使用:

Scanner sc=new Scanner(new File(.....)); 

while(sc.hasNextLine(){ 
    String[] line=sc.nextLine().split("\\s");//split the string up by writespace 
    //....parse tokens 
} 
// now do something 

在你的情况下,它似乎像你想要做的事情如:

Scanner sc=new Scanner(new File(.....)); 
FrequencyCloud<String> votesPerActivity=new FrequencyCloud<String>() 
while(sc.hasNextLine(){ 
    String[] line=sc.nextLine().split("\\s");//split the string up by writespace 
    //if you know the second token is a number, 1st is a category you can do 
    String activity=line[0]; 
    int votes=Integer.parseInt(line[1]); 
    while(votes>0){ 
     votesPerActivity.incremendCloud(activity);//no function in the FrequencyCloud for mass insert, yet 
     votes--; 
    } 
} 


///...do whatever you wanted to do, 
//votesPerActivity.getCount(activity) gets the # of votes for the activity 
/// for(String activity:votesPerActivity.keySet()) may be a useful line too 

FrequencyCloud:http://jdmaguire.ca/Code/JDMUtil/FrequencyCloud.java

+0

谢谢你的回答。使用外部类是唯一可行的方法吗?或者还有其他可能吗? – user2946455

+0

还有其他可能。如果你只想看每个文件的项目一次,你可以做HashMap votesPerActivity = ....然后再做votesPerActivity.put(activity,votes)。如果您打算每个文件多次查看这些项目,您可以这样做(votesPerActivity.contains(activity){votes + = votingPerActivity.get(activity);} votesPerActivity。放(活动,票) – Lan

0

String num = input.replaceAll(“[^ 0-9]”,“”).trim();

为了多样性的缘故,它使用正则表达式。

相关问题