He wants to input in the program an undefinite number of (numbers like 1, 2 etc), all positive. With that done he wants the program to randomize these numbers and show them one at a time in a random distribution. i thought it was easier when he asked but i found out while writing it that its not that easy. i need help. anyone?

Recommended Answers

All 14 Replies

First you can have the user input the number of values to input into the data structure(arrays are a good choice). Then use a for loop to ask the user to put in each value.

thx for ut reply.. heres what ive done. could u kindly check it for me?

//allow for input
import javax.swing.*;
import java.util.*;

public class Sorteggio
{
    public static void main (String[] args)
    {

    //to add the numbers
    int numero;
    ArrayList numero = new ArrayList();

    //loop part
    boolean cont = false;
    do
    {
        cont = false;

        //allows for input
        int choice = Integer.parseInt(JOptionPane.showInputDialog("Type\n"+"1 to add new number\2"+"2 to show random number"));

        if(choice==1)
        {
            //metti il numero
            int scelta = Integer.parseIntJOptionPane.showInputDialog("what number would you like to add");
            //add the number
            numero.add(scelta);
        }
        if(choice==2)
        {
            String output = "your number is..\n";
            output+=((int)randomnumber);
            //we randomize the numbers
            int randomnumber = Math.random(numero);
            JOptionPane.showMessageDialog(null, output);
        }
        //input to repeat
        String repeat = JOptionPane.showInputDialog("Again?");
        if(repeat.equals("si"))
            cont = true;
    }
    while(cont);
    }
}

As the user enters numbers add them to a List ( http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html ). Then in a loop generate a random integer between 0 and the size() of the list. Get(int index) the element at that index, print it, then remove(int index) that element. Loop until the List is empty.

I would get rid of this

int numero;

I do input using the predefined scanner class

Scanner scan = new Scanner(System.in);
System.out.println("Enter how many numbers will be added");
int nums = scan.nextInt();
System.out.println("Input the numbers");
for(int index = 0; index<nums; ++index)
{
	int input = scan.nextInt();
	numero.add(input);
}

That's how I add user input numbers into the list.
By the way. Are you Italian?

yeah i am from rome.

sry but i am very new to java and this is the first prog i get to do.. should i add that part like this?

import java.util.*;

public class Sorteggio
{
    public static void main (String[] args)
    {

    //to add the numbers
    ArrayList numero = new ArrayList();

    //loop part
    boolean cont = false;
    do
    {
        cont = false;

        //allows for input
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter how many numbers will be added");
        int nums = scan.nextInt();
        System.out.println("Input the numbers");
        for(int index = 0; index<nums; ++index)
        {
            int input = scan.nextInt();
            numero.add(input);
        }

how should i continue i think i was doing it wrong before

The code segment I provided earlier is just to get the numbers into the list. Sorry, but I don't know that much about random numbers. Try searching on google.com.

Now you have your numbers, follow the instructions in post #4 above to randomise and output them.

And don't forget to acknowledge the help you received here to your instructor, ah, I mean friend, of course. ;-)

thank you guys for the help however its a bit too complicated for me at this level. this is not a course im taking but personal interest in programming and i thought java is good intro to programming in general. ive been learning using pdf books for the past week and i let a few friends now about it. this particular friends needs this prog in less than two weeks but i might not be able to learn what i need to learn and then write it by then. it would help a lot if u could tell me the steps and procedure to make this work. also im a bit lost between the posts and im still unsure if my previous attempt worked or i should completely rewrite it. thx for ur time!

If you were to make this Friend into a Java class, would it implement Professor?

If you were to make this Friend into a Java class, would it implement Professor?

the simple fact you cannot conceive an individual learning to program as personal interest somehow gives me some credit. im learning as a passion towards the world of coding and computers, and linux too. but you.. such mistrust in people is a reflection of you who are and the programs you probably write: unhelpful, full of bugs and lack of faith.
thx u for ur post anyway, very insightful.

It was a joke, relax. But if you really want to learn, you'll take the suggestions you've already been given, learn how to code those individual pieces, then put them together.

It was a joke, relax. But if you really want to learn, you'll take the suggestions you've already been given, learn how to code those individual pieces, then put them together.

yes, a joke attempting at ridiculing me. after having already made it clear to whom this program is for. funny guy ur the one who needs to take it easy and try to be helpful like the other guys who posted instead of now even stating the obvious of what i ought to do.

I would try to be helpful if you asked a specific question. "I don't know how to do this, please help" is fine, but people have already given step by step explanations of what you need to do to accomplish your goal. At this point, you need to explain what you are having trouble with and (preferably) post code of what you've attempted in order for us to help you. Also, keep in mind that insulting the people on this site isn't a good way to get help, whether it is justified or not. Especially over a minor jab that was said in jest. Anyway, peace, good luck on your program, and if you still need help, I will help you as much as you need. Also - to me, it doesn't matter what the program is for as long as the person writing it is learning something (so I don't care whether this is for your job, your school, your friend, etc). Just throwing that out there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.