Results 1 to 5 of 5
Thread: Creating random numbers
- 09-23-2012, 11:29 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 6
- Rep Power
- 0
Creating random numbers
Hi. I just started programming Java two weeks ago and I am starting to get the basics (I think).
But now I have written a program (in Eclipse) and I get errors which I don't understand.
Has anybody got an idea of what is wrong with this code? (I want to create some numbers
in between -1 and 1 and add them up).
The error messages I get are the next ones (I copied the console view):Java Code:import java.util.Scanner; import java.io.FileNotFoundException; import java.io.File; import java.util.Random; public class Randomnesstester_class { public static void main(String[] args) throws FileNotFoundException{ Random myRandom = new Random(); Scanner diskScanner = new Scanner(new File("Randomnessfile")); Scanner myScanner = new Scanner(System.in); System.out.println("How many numbers do you want to be created?"); int numberOfNumbers = 0; numberOfNumbers = myScanner.nextInt(); int counter = 0; int number = 0; int sum = 0; while (counter < numberOfNumbers) { number = myRandom.nextInt(3)-1; counter = counter + 1; } counter = 0; number = 0; while (counter < numberOfNumbers) { number = diskScanner.nextInt(); sum = sum + number; counter = counter + 1; } System.out.println("The sum of the " + numberOfNumbers + "randomly created numbers is " + sum + "."); } }
How many numbers do you want to be created?
20
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Randomnesstester_class.main(Randomnesstester_class.java:33)
Thanks in advance =)Last edited by sjoelsteen156; 09-24-2012 at 07:56 AM.
- 09-24-2012, 01:48 AM #2
Re: Creating random numbers
Why do they call it rush hour when nothing moves? - Robin Williams
- 09-24-2012, 07:57 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 6
- Rep Power
- 0
Re: Creating random numbers
I am sorry, I tried my best to do put it properly now (edited the first reply).
- 09-24-2012, 08:54 AM #4
Re: Creating random numbers
Your code works fine for me.
I believe you need to add some ints into the Randomnessfile.
This is what I put into the file:
Edit: Also instead of doing counter = counter + 1; you can do counter++;20
30
60Last edited by PhQ; 09-24-2012 at 09:11 AM.
- 09-24-2012, 04:13 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Random numbers
By zel2zel in forum AWT / SwingReplies: 3Last Post: 03-09-2011, 06:39 PM -
Is Random() Only For Numbers?
By Salamander in forum New To JavaReplies: 2Last Post: 02-07-2011, 10:02 AM -
hard question .. help me .. about creating random numbers
By soldier in forum New To JavaReplies: 5Last Post: 12-21-2009, 01:17 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks