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!

[7357 byte] By [RAWR-itsanONIONa] at [2007-11-27 6:24:30]
# 1

If you're using Java 1.5 or greater (which atm is really just java 6), you should replace a lot of that code with the scanner class (java.util.Scanner). Not only can it handle input but it can also handle the string tokens. I don't exactly understand what it is you want to do, but look at examples using the scanner class, should help you clean up that code a little bit.

Captain_Piercea at 2007-7-12 17:43:26 > top of Java-index,Java Essentials,Java Programming...
# 2

essentially what i'm trying to do is allow the user to input something that is then broken into tokens.. the first token staying as a string and saved and then the second token turned into an integer and saved into another variable. The program will continue doing this until all the events have been saved and can be used for later use. This is the problem I have, being unable to save each event seperately into tokens instead of the events overwriting themselves. Thank you for the scanner method advice but I've tried using it before and my java program says it doesn't have the scanner method so I think I need to find another option.

RAWR-itsanONIONa at 2007-7-12 17:43:26 > top of Java-index,Java Essentials,Java Programming...
# 3

The main method is too long. And strangly formatted.

Use a consistent indenting and brace placement scheme. And divide this long main() method into a number of smaller methods each of which do one small (documented) task: ie take some input which you describe and return some output, also described.

> This is the problem I have, being unable to save each event seperately into tokens

> instead of the events overwriting themselves

Java allows you to have references to objects that are collections of things - you can use arrays or Lists etc. References to other objects can be placed in these collections, and retrieved from them.

As for the events themselves, there should be a class for them whose state consists of the string and the number. (I don't know what you expect these string/number pairs to do, but you should be thinking of that now. The class will need methods corresponding to the desired behaviour.)

Parse the input, use it to construct an object of this class, place it in an array or List.

pbrockway2a at 2007-7-12 17:43:26 > top of Java-index,Java Essentials,Java Programming...
# 4

Alright I will try to do some of the stuff you have suggested. As to what I want these pairs to do is this:

I want the program to later ask the user for a set of scores (any amount the user wishes until they input 0) which I have done already in my program above. But then I want my program to use each score seperately and tell the user the least amount of events that can occur to get that score. For example:

These are what the user inputs as his/her events:

catch 3

throw 7

run 2

the scores they want are these:

4

5

7

11

This is what the program outputs:

score of 4:

run 2 // This means that the run event is done twice

score: 5

catch 1

run 2

score: 7

throw 1

score: 11

unable to obtain that score

Hope this kind of clears things up

Also, i'm not too sure how to call upon different methods if i were to make them because I am a really big novice to methods.

Thanks a lot!

RAWR-itsanONIONa at 2007-7-12 17:43:26 > top of Java-index,Java Essentials,Java Programming...
# 5

> score: 5

> catch 1

> run 2

2 runs and a catch = a score of 7, but that's just arithmetic.

> score: 7

> throw 1

True - but that's a real problem. How does the computer get to the answer (throw 1) instead of (catch 1, run 2) which is also 7?

A bit more thought is needed to precisely specify the problem: should the computer list the smallest set of events with a score of 7, or all of them, or something else?

(It occurs to me that you could also use a Map to hold the name of each event and the score associated with it.)

> Hope this kind of clears things up

> Also, i'm not too sure how to call upon different methods if i were to make them

> because I am a really big novice to methods.

You need to be able to use methods and the other basic "control of flow" devices before you can make any progress.

Start here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html

pbrockway2a at 2007-7-12 17:43:26 > top of Java-index,Java Essentials,Java Programming...
# 6

lol sorry for the wrong arithmetic for that one. What i'm thinking is that the program begins with the number saved in the score presented by the user (which is 7)

The program keeps minusing the events until that number becomes 0. As to how do we know that it's just one throw.. I was thinking about arranging the numbers in an array, this array could be checked to see what number in the array is closest to the user's score and subtract that number from the score.. this will be repeated until the score becomes 0. I have done this thing before with fibonacci numbers arrangement and I will try to find that file.

And also I would like to thank you for the link, i shall take it out.

One last thing, right now I have to get off the computer so my response to other responses will take a while. Thanks alot!

RAWR-itsanONIONa at 2007-7-12 17:43:26 > top of Java-index,Java Essentials,Java Programming...
# 7

OK - it sounds like you are writing a "making change" program. What I mean is it answers questions like "How would you give 83 cents change if you have an unlimited number of pennies/dimes/nickles/quarters etc"*

(A common workplace form of betting on sports events here is to buy tickets with random numbers representing the score. Quite apart from the abilities of the teams, certain numbers probably stand a better chance than others because they be formed from a greater number of plausible "event" sets.)

I'm thinking that you'll get good help here with the data structures needed, but you're more on your own with the basic language stuff - the tutorial I posted or something someone else can suggest.

* Hope I got the "events" right. Better to use sensible ones like penny/thruppence/sixpence/bob/florin/half crown.

pbrockway2a at 2007-7-12 17:43:26 > top of Java-index,Java Essentials,Java Programming...