Strange program
ok so I have posted what I had on this before but I've changed things a lot so i decided to create a new topic
Mainly the program asks the user for a number of events. It will then ask the user to input what those events are (the user will input a number of these equal to the number of events inputed). The program will then break the user's input into the name of the event followed by the score that the event is worth in this format:
catch 4
This means that the event of catch is given a value of 4.
I will use these values later on in the program... the user will input scores and the program will find the least amount of events to occur to be able to reach that score.
However, I am not at that part and am still trying to figure out how to break what the user has inputted correctly.
So far this is what I have:
import java.io.*;
import java.util.*;
publicclass TrivialApplication{
publicstaticvoid main(String args[])throws IOException{
int event_number = 1;
int index = 0;
String events ="ha";
int scores = 1;
int c = 0;
int h = 0;
String first ="";
int second = 0;
System.out.println("Type 0 to exit the program");
BufferedReader kinput =
new BufferedReader (new InputStreamReader (System.in));
while (event_number > 0){
System.out.print("\nInput the number of events you want: ");
try{
event_number = Integer.parseInt(kinput.readLine());
}catch (NumberFormatException nfe){
System.out.print("\nThat is not a number, please restate: ");
event_number = Integer.parseInt(kinput.readLine());
}
while (event_number < 0){
System.out.print("\nPlease enter a positive number of events: "); event_number = Integer.parseInt(kinput.readLine());
}
if (event_number == 0){
System.out.println("Program has ended");
break;
}
System.out.print("\nInput the events and the scores created"); System.out.println(" because of them: ");
for (index = event_number; index > 0; index--){
events = kinput.readLine();
StringTokenizer myTokenizer =new StringTokenizer(events);
String [] new_string =new String[myTokenizer.countTokens()];
for (h = 0; myTokenizer.hasMoreTokens(); h++){
new_string[h] = myTokenizer.nextToken();
}
first = new_string[0];
try{
second = Integer.parseInt(new_string[1]);
}catch (NumberFormatException nfe){
System.out.print("\nError, please rewrite: ");
events = kinput.readLine();
myTokenizer =new StringTokenizer(events);
new_string =new String[myTokenizer.countTokens()];
for (h = 0; myTokenizer.hasMoreTokens(); h++){
new_string[h] = myTokenizer.nextToken();
}
first = new_string[0];
second = Integer.parseInt(new_string[1]);
}
if (second > 0){
}else{
System.out.print("\nError, please rewrite: ");
events = kinput.readLine();
myTokenizer =new StringTokenizer(events);
new_string =new String[myTokenizer.countTokens()];
for (h = 0; myTokenizer.hasMoreTokens(); h++){
new_string[h] = myTokenizer.nextToken();
}
first = new_string[0];
second = Integer.parseInt(new_string[1]);
}
}
System.out.print("\nInput various scores that the program"); System.out.println(" will test your game on:");
while (scores != 0){
try{
scores = Integer.parseInt(kinput.readLine());
}catch (NumberFormatException nfe){
System.out.print("\nNumbers only Please: ");
scores = Integer.parseInt(kinput.readLine());
}
}
while (scores == 0){
break;
}
}
}
}
This will break the user's input into tokens and make the first token equal to the String first while the second token is equal to the integer second. This catches most exceptions except the exception of the user inputting less than 2 tokens... an error occures. Also, I can't find out how to make it so that the 2 tokens of each event are saved seperately. The thing is... after each event the String first and the integer second will be replaced by the next event's values. What i'm trying to do is save it seperately so that all of the values from each of the user's inputted events is saved for later access.
Thanks A LOT in advance.. This has been giving me lots of trouble!

