Results 1 to 20 of 30
Thread: add all the numbers in a string
- 09-28-2009, 04:13 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
-
How is the String given to the program, and what have you tried so far?
- 09-28-2009, 04:30 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
the string is an input from a txt file. i need something that will give me the sum of all the numbers so i can divide it by numTrials and get an average to output.Java Code:import java.util.Scanner; import java.util.Random; import java.io.IOException; import java.io.PrintWriter; import java.io.File; public class BottleCapPrize { public static void main(String [] args)throws IOException { Scanner in; in = new Scanner(System.in); int numTrials = 20; PrintWriter outFile = new PrintWriter (new File("bottleCap.txt")); for (int i = 0; i < numTrials; i++) { int counter = 0; int randomNumber = 0; do { randomNumber = ((int)(0+ Math.random()* 5)); counter++; } while (randomNumber != 1); outFile.println(counter); } outFile.close ( ); String token = ""; File fileName = new File("bottleCap.txt"); Scanner inFile = new Scanner(fileName); while (inFile.hasNext()) { token = inFile.next( ); } inFile.close(); } }
-
Your code has little to do with the problem at hand and in fact appears to be solving a different problem. Did you download this code from some online source?
- 09-28-2009, 05:19 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
no i didnt, it is an assignment and i dont know how to do a particular part. i am supposed to output a txt file that has the number of times it takes to win a 1 in 5 chance of winning a drink top game. then i import it back and use it to calculate an average number that you would have to buy in order to win. im sure it is right up until this point where i dont know how to get a sum of all the output so i can get an average. i cant use parsing because im just testing with 20 trials, but when i submit it has to use 1000 trials, and thats too many variable to make.
-
You need to parse the Strings in the file one way or another. Even if you use the Scanner and call nextInt, the Strings are being parsed behind the scenes in the Scanner. I suggest that you do just this, change your while loop to read while (in.hasNextInt()) and read in the ints with in.nextInt into an int variable, and then add this int into a summation variable. Increment a counter variable each time an int is read in. Then you should be able to figure out how to solve this problem.
- 09-28-2009, 11:52 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
im sorry, but i cant really follow what your saying. im very new, can i get like a kind of skeleton of an example of what you mean?
- 09-28-2009, 10:30 PM #8
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
alright i dont know if this is right, but i have done this:
but the only problem is that the numbers are outputting like this:Java Code:while (inFile.hasNext()) { token = inFile.next( ); int total = Integer.parseInt(token); System.out.print(total); } inFile.close();
i dont know how to add them all together. is there one command that can add all the numbers in a string together and set it as an int?143211381111121623112
- 09-29-2009, 02:06 AM #9
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
i have parsed the number, but now i need to add each individual number together.
- 09-29-2009, 08:44 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Don't think on the compiler. Write out your algorithm before you start typing out your code.
You will be getting numbers inside the loop and adding the numbers to a running total. The running total must start at zero before the loop starts and needs to be available after the loop ends.
- 09-29-2009, 09:00 PM #11
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
- 09-29-2009, 09:48 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
I do. You don't seem to be understanding my response though because it tells you how to solve the problem.
- 09-29-2009, 10:05 PM #13
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
well, in theory you answered my question, but in reality i already know the answer, i just dont know how to apply it to code. this is my entire problem, this might make it easier for you to feel my pain.
these are my exact instructions:Java Code:import java.util.Scanner; import java.util.Random; import java.io.IOException; import java.io.PrintWriter; import java.io.File; public class BottleCapPrize { public static void main(String [] args)throws IOException { Scanner in; in = new Scanner(System.in); int numTrials = 20; PrintWriter outFile = new PrintWriter (new File("bottleCap.txt")); for (int i = 0; i < numTrials; i++) { int counter = 0; int randomNumber = 0; do { randomNumber = ((int)(0+ Math.random()* 5)); counter++; } while (randomNumber != 1); outFile.println(counter); } outFile.close ( ); String token = ""; File fileName = new File("bottleCap.txt"); Scanner inFile = new Scanner(fileName); while (inFile.hasNext()) { token = inFile.next( ); } inFile.close(); } }
i am at the end. i have been on this one assignment for almost a week and it is driving me crazy. i have no idea how to get it to work, and i am in a distance learning class so i dont have a teacher to ask. maybe what im asking now makes a little more sense.Instructions: Write a program that uses the Monte Carlo sampling method to estimate the
average number of bottles of Boost someone would have to drink to win a prize. There is a one
in five chance that a bottle cap will have a prize.
1. Create a new project called 5.06 Monte Carlo Method in the
Mod05 Assignments folder.
2. Create a class called BottleCapPrize in the newly created
project folder.
3. Determine how many times a die must be rolled in order to
win a prize. (This represents one trial.) Print this value to a
text file.
4. Conduct at least 1,000 trials.
5. Read the data back in from all of the trials.
6. Calculate the average number of times a die must be rolled in order to win a prize.
7. Print the result to the screen.
Suggestion: Write this program in stages. First, work on the part that conducts trials and
print the results to the screen; however, during testing only use about 20 trials. Second,
print the results of each trial (the number of rolls to get a prize) to a file. Check the file to
verify it matches the screen output. Third, read the trial data back in and calculate the
average. Fourth, print the results.
Expected Output: When your program runs, your output should simply print a message
indicating the average number of bottles of Boost you would need to drink to win a prize
- 09-29-2009, 10:17 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
int total = 0;
while(there are still some numbers to be added) {
read a number.
add the value of that number to the total
}
total now has the sum.
- 09-29-2009, 10:28 PM #15
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
how do you read a number from a string of several numbers? i mean i get what your saying, i know what to do in pseudo code, but i dont know the actual statements to use to get the result i need so i can calculate the average and then print it.
- 09-29-2009, 10:31 PM #16
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Look at your first post. The numbers are on different lines aren't they?
So read one line at a time => read one number at a time.
How to calculate the average? As the total loop is running, add a count variable that counts how many times the loop ran. The rest should be obvious from there.
- 09-29-2009, 11:10 PM #17
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
"So read one line at a time => read one number at a time. " <-------------this is what i am trying to figure out how to do.
- 09-29-2009, 11:18 PM #18
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If you use FileReader/BufferedReader combination then you'll find the readLine method very useful. If you use the Scanner then that you will only need to be calling nextInt to get the next number. Read the API specs for those classes and methods.
-
- 09-30-2009, 12:52 AM #20
Member
- Join Date
- Sep 2009
- Posts
- 11
- Rep Power
- 0
alright, i have this. i tried to do everything that was stated. when i compile and run my virtual machine in blue j just goes and produces no output. usually that happens when a loop isnt satisfied. does my code look kind of similar to what you guys were talking about? does anyone know if a loop isnt satisfied, or anything that could cause my virtual machine to stay tied up?Java Code:int sum = 0; while (in.hasNextInt()) { token = inFile.next( ); int num = Integer.parseInt(token); int nums = in.nextInt(); sum = nums + nums; int average = sum/numTrials; System.out.println(average); } inFile.close(); } }
Similar Threads
-
parsing numbers in a string
By rsoler in forum Advanced JavaReplies: 4Last Post: 03-31-2009, 06:05 AM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
validating a string for numbers and letters?
By lockmac in forum New To JavaReplies: 1Last Post: 08-09-2007, 09:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks