0

I am trying to create a program to generate random number and store into arraylist. Meanwhile getting the number stored to do multiply.

I have store the generated number but how do I actually get the number that I wanted. For this example, i trying to get the 3rd number, which step did i missed out?

public class arraylist { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); Random rand = new Random(); int numtogen; int third; Scanner scan = new Scanner(System.in); System.out.println("How many number do you want to generate: "); numtogen = scan.nextInt(); System.out.println("What number do you want to multiply with the third number: "); third = scan.nextInt(); HashSet<Integer> generated = new HashSet<Integer>(); // Prevent repeat for (int x = 1; x <= numtogen; ++x) { while (true) { // generate a range from 0-100 int ranNum = rand.nextInt(100); if (generated.contains(ranNum)) { continue; } else { list.add(ranNum); System.out.println("Number " + x + "=" + " = " + ranNum); break; } } } int numinlist; while (!list.isEmpty()) { // Integer[] numlist= numbinlist.hasNextInt; // int answer = numlist[2]*third; // System.out.println("Answer to first number = "+answer); } } } 
2
  • 1
    What you're trying to do is rather unclear. Commented Apr 24, 2013 at 7:04
  • The 3rd random number is just a random number. The List or Set is redundant so you can just do int triple = rand.nextInt(100) * 3; regardless of what they enter (I assume this is just an exercise ;) Commented Apr 24, 2013 at 7:04

3 Answers 3

3

You can change the generation of non-repeating numbers to:

Set<Integer> generated = new LinkedHashSet<Integer>(); // Prevent repeat while (generated.size() < numtogen) { generated.add(rand.nextInt(100)); } List<Integer> list = new ArrayList<Integer>(generated); 

I'll edit this answer as soon as I understand your actual problem and know the answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Well optimized code ! . newbieprogrammer you do not need to use infinite while loop and break it.
3

If you want to get the 3rd generated number try this:

int number = list.get(2); 

3 Comments

the third number would actually be list.get(2);
+1 seems like a good guess. The question needs editing anyway. Oh, fix the index, please
oh..list.get to retrieve the number..thanks thanks that is what I looking for.
1

change this:

for (int x = 1; x <= numtogen; ++x) { while (true) { // generate a range from 0-100 int ranNum = rand.nextInt(100); if (generated.contains(ranNum)) { continue; } else { list.add(ranNum); System.out.println("Number " + x + "=" + " = " + ranNum); break; } } } 

to:

for (int x = 1; x <= numtogen; ++x) { // generate a range from 0-100 int ranNum = rand.nextInt(100); if (generated.contains(ranNum)) { continue; } else { list.add(ranNum); System.out.println("Number " + x + "=" + " = " + ranNum); } } 

Then, all you need to do to access the 3rd number is something like:

int answer = (list.get(2) * third); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.