Computing.Net > Forums > Programming > java: problem with loop and random

java: problem with loop and random

Reply to Message Icon

Original Message
Name: Tor Arne Fornes
Date: September 10, 2003 at 09:10:47 Pacific
Subject: java: problem with loop and random
OS: windows xp
CPU/Ram: amd 900Mhz/256
Comment:

I have a problem with a program that generates X numbers of numbers between 1 and 6. X is to be set by an user(Input). The problem is that the result is to be show'n in one JOptionMessageDialog with 5 numbers in a row. If there are more than 5 numbers then it's suposed to start on a new line.

This is my code so far:
import javax.swing.JOptionPane;
import java.util.Random;

class random2 {

public static void main(String[] args) {
final int border = 6;
Random randomGen = new Random();
String antallLest = JOptionPane.showInputDialog(null, "Number of numbers to be generated: ");
int antall = Integer.parseInt(antallLest);

for (int i = 0; i antall ; i++) {
int terningkast = randomGen.nextInt(border) + 1;}

}
}


Report Offensive Message For Removal

Response Number 1
Name: JavaToad
Date: September 10, 2003 at 13:19:21 Pacific
Subject: java: problem with loop and random
Reply: (edit)

So what's your question?


Report Offensive Follow Up For Removal

Response Number 2
Name: Tor Arne Fornes
Date: September 10, 2003 at 13:24:04 Pacific
Subject: java: problem with loop and random
Reply: (edit)

How do i get the result in one JOptionPane MessageDialog with 5 numbers in a row? If there are more than 5 numbers then it's suposed to start on a new line.

I have tried to get this to work, but i'm simply out of idea's.


Report Offensive Follow Up For Removal

Response Number 3
Name: JToad
Date: September 10, 2003 at 13:44:42 Pacific
Subject: java: problem with loop and random
Reply: (edit)

Okay, are you saying when you place more than five numbers, the next chars inline don't show up?

if this is true, there are several solutions availible. depending on the stipulations of your project, ie you have to use a joptionpane, then there may be problems.

but first try playing with the joptionpane constructor by creating it after you know how many lines of numbers you want to print, then create a string like:

int a=0, b=0;
StringBuffer strbuf = new StringBuffer( );
while( b total_num_values ) {
if( a == vals_i_want_to_print_per_line ){
a = 0;
strbuf.append( String.valueOf('\n') );
}
strbuf.append( Integer.toString( val[b] )
++a; ++b;
}

this might now work if joptionpane's message component is a label or jlable ( they only can hold one line). then you will have to build your own class to handle multiple lines, then use show( ), hide( ) and destroy( ) ... etc.


Report Offensive Follow Up For Removal

Response Number 4
Name: Tor Arne Fornes
Date: September 10, 2003 at 14:12:40 Pacific
Subject: java: problem with loop and random
Reply: (edit)

sorry, that's not the solution on my problem.
I think i'm bad to make myself clear.
The problem is to get ONE messagebox that shows the X number of generated numbers.
X = antall(see the program code)


Report Offensive Follow Up For Removal

Response Number 5
Name: JToad
Date: September 10, 2003 at 14:39:31 Pacific
Subject: java: problem with loop and random
Reply: (edit)

You code:

show a option pane and get a string.

convert the user's input to an int 'input'.

use a random num generator to generate random values 'input' times.

then what? i can't figure the program's goal.

/**I have a problem with a program that generates X numbers of numbers between 1 and 6.*/

does this mean you want to generate at most 6 values? or does this mean you want your random number to be on the number line between 1 & 6? but then still what is the goal of the program?!


Report Offensive Follow Up For Removal


Response Number 6
Name: Tor Arne Fornes
Date: September 10, 2003 at 14:58:55 Pacific
Subject: java: problem with loop and random
Reply: (edit)

The goal of the program is to get the user's input(x) to be shown in a JOptionpane.MessageDialog box as x numbers of random generated numbers between 1 and 6.


Report Offensive Follow Up For Removal

Response Number 7
Name: JToad
Date: September 10, 2003 at 15:09:28 Pacific
Subject: java: problem with loop and random
Reply: (edit)

Okay, now maybe i know what to do.
to get the numbers between 1 & 6, you can use the mod operator %7.

hence for input 'x';

suppose x=0;

x%7 equals 0; not in range, thus do the loop again ...
if x=1;
x%7 equals 1;
x++;
x%7 equals 2;
...
x=7;
x%7 equals 0; not in range, do loop again with new random num.

then use the algorithm discussed above to add new lines.


Report Offensive Follow Up For Removal

Response Number 8
Name: Razavn
Date: September 12, 2003 at 03:20:42 Pacific
Subject: java: problem with loop and random
Reply: (edit)

Or first you built a string and after five chars you add a "\n"(new line)


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: java: problem with loop and random

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software