Help in writing a litle program for a friend

Reply

Join Date: Apr 2009
Posts: 6
Reputation: trombadorez is an unknown quantity at this point 
Solved Threads: 0
trombadorez trombadorez is offline Offline
Newbie Poster

Help in writing a litle program for a friend

 
0
  #1
Apr 15th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 458
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 37
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Help in writing a litle program for a friend

 
0
  #2
Apr 15th, 2009
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.
Last edited by Grn Xtrm; Apr 15th, 2009 at 4:41 pm.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: trombadorez is an unknown quantity at this point 
Solved Threads: 0
trombadorez trombadorez is offline Offline
Newbie Poster

Re: Help in writing a litle program for a friend

 
0
  #3
Apr 15th, 2009
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);
}
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Help in writing a litle program for a friend

 
0
  #4
Apr 15th, 2009
As the user enters numbers add them to a List ( http://java.sun.com/j2se/1.4.2/docs/...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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 458
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 37
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Help in writing a litle program for a friend

 
0
  #5
Apr 15th, 2009
I would get rid of this
  1. int numero;

I do input using the predefined scanner class
  1. Scanner scan = new Scanner(System.in);
  2. System.out.println("Enter how many numbers will be added");
  3. int nums = scan.nextInt();
  4. System.out.println("Input the numbers");
  5. for(int index = 0; index<nums; ++index)
  6. {
  7. int input = scan.nextInt();
  8. numero.add(input);
  9. }
That's how I add user input numbers into the list.
By the way. Are you Italian?
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: trombadorez is an unknown quantity at this point 
Solved Threads: 0
trombadorez trombadorez is offline Offline
Newbie Poster

Re: Help in writing a litle program for a friend

 
0
  #6
Apr 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 458
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 37
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training

Re: Help in writing a litle program for a friend

 
0
  #7
Apr 15th, 2009
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.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Help in writing a litle program for a friend

 
0
  #8
Apr 16th, 2009
Now you have your numbers, follow the instructions in post #4 above to randomise and output them.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,380
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Help in writing a litle program for a friend

 
0
  #9
Apr 16th, 2009
And don't forget to acknowledge the help you received here to your instructor, ah, I mean friend, of course. ;-)
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: trombadorez is an unknown quantity at this point 
Solved Threads: 0
trombadorez trombadorez is offline Offline
Newbie Poster

Re: Help in writing a litle program for a friend

 
0
  #10
Apr 16th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC