2013-11-01 46 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();

爲了多樣性的緣故,它使用正則表達式。

相關問題