Results 1 to 5 of 5
  1. #1
    sjoelsteen156 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Question 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).

    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 + ".");
    
    	}
    
    }
    The error messages I get are the next ones (I copied the console view):
    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 08:56 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Creating random numbers

    Quote Originally Posted by sjoelsteen156 View Post
    ... and I get errors which I don't understand.
    Would you like to tell us more about those errors, or would you prefer members to play a guessing game?

    Whenever seeking help with any error, copy and post the entire error text; don't paraphrase.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sjoelsteen156 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Creating random numbers

    I am sorry, I tried my best to do put it properly now (edited the first reply).

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default 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:
    20
    30
    60
    Edit: Also instead of doing counter = counter + 1; you can do counter++;
    Last edited by PhQ; 09-24-2012 at 10:11 AM.

  5. #5
    sjoelsteen156 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Creating random numbers

    Thank you for your help, I saw what I did wrong...
    I forgot to write to the file (using PrintStream)..
    Stupid of me I did not see it before...

    And the counter++ is a very good solution, thanks for that.

Similar Threads

  1. Random numbers
    By zel2zel in forum AWT / Swing
    Replies: 3
    Last Post: 03-09-2011, 07:39 PM
  2. Is Random() Only For Numbers?
    By Salamander in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 11:02 AM
  3. Replies: 5
    Last Post: 12-21-2009, 02:17 PM
  4. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •