Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JAVA CODE CLARIFICATION

Posted on 2008-10-24
9
202 Views
Last Modified: 2013-11-23
Here is my script that works.  I need to have the numbers, when generated, not be duplicated.  I understand that I should use an array, but I do not know how.  Some clarification would be appreciated.  Thank you.

import java.util.Random;
public class Math
{
   public static void main ( String args[] )
   {
      Random randomNumbers = new Random();
      int number;
      for ( int counter = 1; counter <= 30; counter++)
      {
         number = 1 + randomNumbers.nextInt( 49 );
         System.out.printf("%d   ", number);
         if ( counter % 6 == 0 )
            System.out.println();
      }
   }
}
0
Comment
Question by:suekathur
  • 5
  • 4
9 Comments
 
LVL 16

Accepted Solution

by:
sh0e earned 125 total points
ID: 22801527

import java.util.Random;
public class Math
{
   public static void main ( String args[] )
   {
      Random randomNumbers = new Random();
      int number;
bool[] oldnums = new int[50];
      for ( int counter = 1; counter <= 30; counter++)
      {
while(oldnums[number]){number = 1 + randomNumbers.nextInt( 49 );}
 
         System.out.printf("%d   ", number);
         if ( counter % 6 == 0 )
            System.out.println();
      }
   }
}

Open in new window

0
 

Author Comment

by:suekathur
ID: 22803659
The way I read the code is that you want to use a boolean stating that oldnums will equal a new integer and if the old number equals the same number then continue with the random number selection.  Am I reading this right?
I also tried running the new code and it does not work so I know things are missing.  Can you point me in the right direction, please.  Thanks
0
 
LVL 16

Assisted Solution

by:sh0e
sh0e earned 125 total points
ID: 22804043
Correct.  It's the easiest  way.  Just two lines.

I made a typo, it's boolean not bool.

import java.util.Random;
public class Math
{
   public static void main ( String args[] )
   {
      Random randomNumbers = new Random();
      int number;
boolean[] oldnums = new int[50];
      for ( int counter = 1; counter <= 30; counter++)
      {
while(oldnums[number]){number = 1 + randomNumbers.nextInt( 49 );}
 
         System.out.printf("%d   ", number);
         if ( counter % 6 == 0 )
            System.out.println();
      }
   }
}

Open in new window

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:suekathur
ID: 22804150
When running the code I get the following error:
incompatible types
found: int[]
required: boolean[]
boolean[] oldnums = new int[50];
I know that the boolean will need to be changed to a string and I have looked on the internet for possible solutions but I do not understand their reasoning.  I found this:  String myString = Integer.toString(my int value).









0
 
LVL 16

Assisted Solution

by:sh0e
sh0e earned 125 total points
ID: 22804234
Sorry, I don't know what got into me.  I initialized the boolean array wrong.
import java.util.Random;
public class Math
{
   public static void main ( String args[] )
   {
      Random randomNumbers = new Random();
      int number;
boolean[] oldnums = new boolean[50];
      for ( int counter = 1; counter <= 30; counter++)
      {
while(oldnums[number]){number = 1 + randomNumbers.nextInt( 49 );}
 
         System.out.printf("%d   ", number);
         if ( counter % 6 == 0 )
            System.out.println();
      }
   }
}

Open in new window

0
 

Author Comment

by:suekathur
ID: 22804403
Receiving the error: variable number might not have been initialized
while(oldnums[number]){number = 1 + randomNumbers.nextInt( 49 );}
the error happens under the [number] in the code.
Since the number has been intialized as an int number in the above code I do not know whyI am getting this.
0
 
LVL 16

Assisted Solution

by:sh0e
sh0e earned 125 total points
ID: 22804457

import java.util.Random;
public class Math
{
   public static void main ( String args[] )
   {
      Random randomNumbers = new Random();
      int number;
boolean[] oldnums = new boolean[50];
      for ( int counter = 1; counter <= 30; counter++){
         do{number = 1 + randomNumbers.nextInt( 49 );}while(oldnums[number]);
 
         System.out.printf("%d   ", number);
         if ( counter % 6 == 0 )
            System.out.println();
      }
   }
}

Open in new window

0
 

Author Comment

by:suekathur
ID: 22804743
This still does not work.  I am going to try a different method - maybe two arrays instead of one.
Consider this closed.
Thanks for your help.
0
 

Author Closing Comment

by:suekathur
ID: 31509891
Thank you for your help.  This is getting too complicated for me to understand so I am going to try something else.  It was suggested utilizing two arrays instead of one so I will try that,
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 63
runtime exception 2 42
Desingning Refactoring existing code 2 26
restrict decimal places for double datatype 10 21
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question