Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Can't get seven random numbers, only one.

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't get seven random numbers, only one.

    I want my program to show seven random numbers, but it does only display one. I got this in a class:

    if (command.equals("Random numbers")) {
    			int tal = modell.getTal();
    			vy.skrivNyLottorad(tal);
    		}


    getTal is in the class Model:

    public class Model {
    	private int tal;
     
    	public int getTal() {
    		Random randomNr = new Random();
     
    		for (int i = 0; i < 7; i++) {
    			tal = randomNr.nextInt(35);
    		}
    		return tal;
    	}


    The method skrivNyLottorad from the first piece of code is here:

    public void skrivNyLottorad(int lottorad) {
    		textruta.append(lottorad + "\n");
    	}


    What is my problem? it should display seven numbers, but it does only display one.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't get seven random numbers, only one.

    Most people give too much code, but this is one of those rare examples when someone has not given enough.

    We need to know the context of the if statement from your first code example. Is it in a loop? What is vy? What is command?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Can't get seven random numbers, only one.

    You are correctly generating 7 random numbers. However after you generate each random number, you are storing it in the int variable tal, effectively overwriting the last random number you generated. After you do this 7 times, you are returning the 7th random int you generated.

  4. #4
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't get seven random numbers, only one.

    Here are some more of the code. I thought that was the problem, that the last number is the number that is going to display. So I think, that I have to store all seven numbers in a string? or char? how do I do that?

    val1 = new JMenuItem("Random numbers");
    		val1.addActionListener(kontroll);


    if (command.equals("Random numbers")) {
    			int tal = modell.getTal();
    			vy.skrivNyLottorad(tal);
    		}


    import java.util.Random;
     
    public class Model {
    	private int tal;
     
    	public int getTal() {
    		Random randomNr = new Random();
     
    		for (int i = 0; i < 7; i++) {
    			tal = randomNr.nextInt(35);
    		}
    		return tal;
    	}
     
    	public static void main(String[] args) {
    		Model modell = new Model();
    		System.out.println(modell.getTal());
    	}
    }


    public void skrivNyLottorad(int lottorad) {
    		textruta.append(lottorad + "\n");
    	}

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get seven random numbers, only one.

    You could always store each value in an array and then you'd have them all.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't get seven random numbers, only one.

    int [] tal = new int[7];

    for (int x =0; x < tal.length; x++)
    {
    tal[x] = randomNr.nextInt(35);

    }

Similar Threads

  1. random numbers in array please help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 01:54 AM
  2. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  3. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM
  4. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM