You are viewing our Forum Archives. To view or take place in current topics click here.
Java help Reading a single file multiple times
Posted:

Java help Reading a single file multiple timesPosted:

5FDP_Jekyll
  • Supporter
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Alright, so I am trying to make a different tool for Ark: Survival Evolved. This one will take the level of any dino you have tamed and figure out what level it tamed out at. I have been trying to use while loops to do this along with a comparison of values, one is given by the user, if that value matches up with one that is read from the file, then it outputs a result. However, if it does not correspond to one, then I would like the user to be able to go back in and re-enter the value. I have had quite a bit of frustration with this supposed to be simple program. I do believe why I know I'm unable to do it how I want. That's because it will go through the entire list if no match is found and then is unable to return to the top of the list, causing an infinite loop of asking for an input and will never make a match.Is there a way I could do this without the need to change my code drastically or not? Below is the code related to the while loop which I believe is the only things related to my issue.

try {
         Scanner s = new Scanner(new File("levels.txt"));
         
         while (exptrue == 0){
            System.out.println("Enter experience required to level up: ");
            exp = reader.nextInt();
            while (s.hasNextInt()){
               readexp = s.nextInt();
               if (exp == readexp) {
               exptrue = 1;
               break;   
               }
               
               /*else if (count == 70 && exptrue == 0) {
                  System.out.println("The experience entered is not " +
                        "an accepted value, please try again.");
               count = 0;
               break;
            }*/
            
            else {
               count++;
            }
            
            }
         }
         s.close();
      } catch (FileNotFoundException e) {
         File a = new File("levels.txt");
         String b = a.getAbsolutePath();
         System.out.println(b);
         System.out.println("Error: Missing File (levels.txt)");
         System.exit(1);
      }
      
      tame_level = (level - count);
      
      System.out.println("This creature was tamed at level " + tame_level);

   }
#2. Posted:
Xaldin
  • 2 Million
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Closing the scanner and reopening it should have the same effect as a rewind function
#3. Posted:
5FDP_Jekyll
  • V5 Launch
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Xaldin wrote Closing the scanner and reopening it should have the same effect as a rewind function
I've tried a couple different ways to do that all of which result in termination due to closure of the scanner. If you have a way of implementing it into my code, I'd appreciate your input on it. Specifically, I'm trying to do it if count = 70. E.G, if no matches were found.
#4. Posted:
5FDP_Jekyll
  • E3 2017
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201112Year Member
Posts: 2,048
Reputation Power: 100
5FDP_Jekyll wrote
Xaldin wrote Closing the scanner and reopening it should have the same effect as a rewind function
I've tried a couple different ways to do that all of which result in termination due to closure of the scanner. If you have a way of implementing it into my code, I'd appreciate your input on it. Specifically, I'm trying to do it if count = 70. E.G, if no matches were found.
Never mind, I just had to change where I put the closure in at. Figured heading to bed might help me think, lol.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.