confusing but interesting program

This program is hard to explain but I will try to do so with the best of my ability. First of all the program should ask the user how many events the user wants and then do the next part of the program the same amount as this number. The next part of the program should take user input which consists of something + a score the amount of times equal to the events. And then the program asks for a series of scores (such as 1 and 3 and whatever numbers) until the user inputs 0 and then stops the program.

Since this is confusing probably i will give you an example:

the user inputs:

4

(number of events)

then the user inputs:

bat 2

ball 3

hit 4

out 0

then the user inputs (these are the scores):

3

4

6

8

0

so the computer will output:

score 3: ball 1(this means that one ball was used which equals 3)

score 4: hit 1

score 6: ball 2(two balls were used to add to a score of 6)

score 8: hit 2

With this program I think I should use an array to seperate the different user inputs and a seperate array to seperate the scores...

Also, I got some of this done but I still have lots to go because this program is kind of confusing (took me a while to understand the program in english before i could start on it in java)

here is my code though... am i doing anything wrong so far?

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;

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: ");

// should this be a string array?

// i'm not sure because the program should read the item name

// and value of it's worth

String[] score_array =new String[event_number];

for (index = event_number; index > 0; index--){

events = kinput.readLine();

// i know this part doesn't work because it makes all the array values =

// to events but how do i make it so each value = seperate event?

for (c = 0; c < score_array.length; c++){

score_array[c] = events;

}

}

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;

}

}

}

}

Any help will be very appreciated ^_^

Thanks in advance!

[5719 byte] By [RAWR-itsanONIONa] at [2007-11-27 5:50:46]
# 1

I have nfi of what you want to do. Is this a game? Is there an objective? Please explain in a different way if possible.

Ah, on 3rd reading, I think I do understand sort of now. First and formost, consider using classes/OOP and getting as much clutter out of the main() method as possible. You could have an Event class that has name and score fields.

Message was edited by:

petes1234

petes1234a at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...
# 2

sorry for the confusion

btw what do you mean by "score fields"

practically what my program is trying to do is allow the user to create a game with a scoring pattern and then the user is to input the scores that players got in that game.

The program will then output the smallest amount of events that add up to the scores the players have got.

If the events are catching a ball for one point, kicking a ball for two points and running for four points, if one player got a score of five, the smallest amount of steps to gain that score would be to catch a ball and then run (4 + 1 = 5) instead of kicking two balls and then catching a ball. I hope this conveys the idea better.

Ya, i'll try and see if i can get classes to work, although i do find them really hard to use and call upon; they might be the only option.

Message was edited by:

RAWR-itsanONION

RAWR-itsanONIONa at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...
# 3
So start with the action with the highest score.Repeatedly subtract its value from players score.Record how many actions that took.When you can no longer subtract without going into negative, move to next action.Or if score equals zero, end.
floundera at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...
# 4

> btw what do you mean by "score fields"

a field or variable of a class. For instance you could create an Event class that starts out like so:

class Event

{

private String name = ""; // here is the Event name field

private int score = 0; // here is the Event score field

public Event(String name, int score)

{

this.name = name;

this.score = score;

}

public String getName()

{

return name;

}

public int getScore()

{

return score;

}

}

Then you can hold the event name and its score together in an ArrayList<Event>.

> Ya, i'll try and see if i can get classes to work,

> although i do find them really hard to use and call

> upon; they might be the only option.

I'm sure that you could create a functioning program without using classes, but if so, you might as well be using GW-BASIC. You wouldn't buy a Ferrari, and then only drive it in first gear. One of the true beauties of Java is its implementation of OOP. As long as you are creating a program for the purpose of learning to program, what the heck, learn to use OOP techniques too.

My 2 cents.

Message was edited by:

petes1234

petes1234a at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...
# 5

flounder, i kind of understand that part except im not sure how to make it so that when the program reads:

catch 1

it makes something called catch and gives it a value of 1.. for later i mean when displays the scores

if you want a score of 2 it would say

score 2:

catch 2 (because 2 catches are being used)

I think anyways

Also, thanks for your quick responses guys, but unfortunately i have to get off the computer and can't use it until probably tommorow evening so if you respond after this, by not responding I am not ignoring you I just have to go.

RAWR-itsanONIONa at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...
# 6

If you had an eventCount int variable, a List of Events (the prior class I showed above and used the Scanner class like so:

private static int eventCount = 0;

private static List<Event> eventList = new ArrayList<Event>();

private static Scanner scan = new Scanner(System.in);

you could loop through input 0 to < eventCount, collect each event name and score value, and then combine the two of them in an arraylist of event class

private static void getEventAndScore()

{

for (int i = 0; i < eventCount; i++)

{

System.out.print("Please enter an name for event " + (i+1) + ": ");

String name = scan.nextLine();

boolean done = false;

while (!done)

{

System.out.print("Please enter a score value: ");

if (scan.hasNextInt())

{

int scoreValue = scan.nextInt();

scan.nextLine();

// here we add the name string and scoreValue int into the

// eventList by creating a new instance of the Event class

eventList.add(new Event(name, scoreValue));

done = true;

}

else

{

scan.nextLine();

System.out.println("invalid input. It must be an integer");

}

}

}

}

petes1234a at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...
# 7

> flounder, i kind of understand that part except im

> not sure how to make it so that when the program

> reads:

> catch 1

Do as Pete suggested. Create a class that holds the event name and value. Store them in a sorted collection which you can iterate over.

floundera at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...
# 8
i'll try your suggestions and i'll see if I can get any progress, i will give you both 2 stars for your help so far. I'll repost if i make any progress or have any more questions.. thanks!
RAWR-itsanONIONa at 2007-7-12 15:38:50 > top of Java-index,Java Essentials,Java Programming...