This site requires JavaScript, please enable it in your browser!
Greenfoot back
someone1
someone1 wrote ...

2015/10/13

How to process multiple text files identified by the user

someone1 someone1

2015/10/13

#
Ok i have another problem which is there are 10 text files all with same names except the number on the end like this: games1.txt, games2.txt, games3.txt....games10.txt I want the user to enter the number of text files to be processed for example the user enters 4, so text files games1 games2 games3 and games4 will be processed but im not sure how to do this. Any help would be great :)
danpost danpost

2015/10/13

#
Use the number entered by the user as the limit in a for loop whose code processes the files.
someone1 someone1

2015/10/14

#
Could you plz show me an example
danpost danpost

2015/10/15

#
someone1 wrote...
Could you plz show me an example
If you had 'n' be the int value of the number of files to be processed:
for (int i=0; i<n; i++)
{
    String filename = "games"+i+".txt";
    // process the named file here
}
someone1 someone1

2015/10/15

#
ok thanks
danpost danpost

2015/10/15

#
I just noticed that you do not have a 'game0.txt'. Change my line 3 to:
String filename = "games"+(i+1)+".txt";
someone1 someone1

2015/10/15

#
Ok now i have another problem this is my code
 int userInput = 0;
  Scanner keyboard = new Scanner(System.in);
  System.out.print("What is the number: ");
  userInput = keyboard.nextInt();
  for(int i = 0; i < userInput; i++) {
    File file = new File ("Orders" + userInput + ".txt");
    Scanner hello = new Scanner(file);
    String str;
    while(hello.hasNext()) {
      str = wrap.next();
      String[] token = str.split(",");
      int orderNo = Integer.parseInt(token[0]);
      int quantityOrdered = Integer.parseInt(token[1]);
      int extrasOrdered = Integer.parseInt(token[2]);
      System.out.println(orderNo);
    }
  }
The thing is if the user enters the number 2 for the amount of text files to be processed and i try printing out the order number, it just processes the 'Orders2.txt' twice instead of order 1 then order 2
someone1 someone1

2015/10/15

#
Disregard my last post, i didnt read your other post, i think ive figured it out
You need to login to post a reply.