Reputation: 123
Not sure if the title will make much sense but I'm currently quite confused and not sure how to work around my problem.
I'm trying to request from the user, a destination, a max time period (in the format HH:MM) and a maximum number of changes, which so far I have done. It should then calculate each journey’s total mins along with number of changes and then compare both with the user’s criteria, I recently edited my program to use case
statements.
It does link to a .txt file that has the following data in it:
York
1
60
60
Alnwick
0
130
Alnwick
2
30
20
20
So my program asks for a destination, either York or Alnwick and a number of changes, maximum time, and so on but I can't figure out how to make it work with the chosen destination, current code to follow:
import java.io.FileReader;
import java.util.Scanner;
public class InputOutput {
public static void main(String[] args) throws Exception {
// these will never change (be re-assigned)
final Scanner console = new Scanner(System.in);
final Scanner INPUT = new Scanner(new FileReader("C:\\Users\\Luke\\workspace\\Coursework\\input.txt"));
System.out.print("-- MENU -- \n");
System.out.print("1: Blahblahblah \n");
System.out.print("2: Blahblahblah \n");
System.out.print("Q: Blahblahblah \n");
System.out.print("Pick an option: ");
int option = console.nextInt();
switch(option) {
case 1 :
while(INPUT.hasNextLine()) {
System.out.println(INPUT.nextLine());
}
break;
case 2 :
System.out.print("Specify desired location: ");
String destination = console.next();
System.out.print("Specify Max Time (HH:MM): ");
String choice = console.next();
// save the index of the colon
int colon = choice.indexOf(':');
// strip the hours preceding the colon then convert to int
int givenHours = Integer.parseInt(choice.substring(0, colon));
// strip the mins following the colon then convert to int
int givenMins = Integer.parseInt(choice.substring(colon + 1, choice.length()));
// calculate the time's total mins
int maxMins = (givenHours * 60) + givenMins;
System.out.print("Specify maximum changes: ");
int maxChange = console.nextInt();
// gui spacing
System.out.println();
int mins = INPUT.nextInt();
int change = INPUT.nextInt();
if ((mins > maxMins) || (change > maxChange)) {
System.out.format("Time: %02d:%02d, Changes: %d = Unsuitable \n", (mins / 60), (mins % 60), change);
}
else {
System.out.format("Time: %02d:%02d, Changes: %d = Suitable \n", (mins / 60), (mins % 60), change);
}
//Do stuff
break;
case 3 :
default :
//Default case, reprint menu?
}
}
}
Have edited it to reduce the size of the question for StackOverflow but if more code is needed please let me know - any further help would be greatly appreciated!
Upvotes: 0
Views: 437
Reputation: 4039
You should really learn how the Scanner works:
int Scanner.nextInt()
Returns the next int value that occurred in the next line.String Scanner.next()
Returns the next piece of String separated by the default delimiter which is space " "
. (You could use a different Delimiter with Scanner.useDelimiter(String)). In default case this returns the next single Word.String Scanner.nextLine()
Returns the next full line separated with the "\n"
Character.So if you want to get a destination that has two words for Example "New York" and you fetch it with Scanner.next() like you do. Then you take the time the same way. You will get destination="New"
and choice = "York"
which is not parsable for :
and will crash.
The other problem you have is that a Scanner works from start to end. So if you choose option 1
and print all the output from your input file you will reach the end and hasNextLine() == false
. Means you cannot get any INPUT.nextInt()
after that point. But you try when chosing option 2
after that.
Your prorgamm should start by reading in your input file into a data structure that stores all the informations for you. And get them from there in further process.
What is crashing in your code for now is that you start reading your text file with INPUT.nextInt()
but the first line of your text file is York
which has no Int value in it. You could repair that by adding:
[...]
System.out.println();
INPUT.nextLine(); // skips the first line which is York
int mins = INPUT.nextInt();
int change = INPUT.nextInt();
[...]
Upvotes: 1