So I have a .txt file with only this as the contents:

pizza 4
bowling 2
sleepover 1

What I'm trying to do is, for example in the first line, ignore the "pizza" part but save the 4 as an integer.

Here is the little bit of code I have so far.

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

I am a beginner. As you can see my actual, functioning code is very short and I don't know to proceed. If anyone would be so kind as to point me in the right direction, even if you just give me a link to a site, it would be extremely helpful...

EDIT: I stupidly thought PrintWriter could read a file

有帮助吗?

解决方案

It's pretty simple actually. All you need is a Scanner, and it's function 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.
        }

I have not tested this code, but it should work, but the value in the while loop is going to be the current value of the current line.

其他提示

I don't fully understand your question, so comment if you want some clearer advice

Here is a common pattern you'll use in 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

In your case, it seems like you want to do something like:

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

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

For sake of diversity this uses regular expressions.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top