its me again.. i just want to ask what you would use with this kind of task.... its not that i'm asking for the code... but just what methods, say... is okay with this... here goes...
create a program that inputs five numbers and determines and prints the number of negative number inputs, the number of positive numbers input, and the number of zeros input...
suggestions would be a BIIIIIIIIIG help... i'll try to do the rest...
THANK YOU!!!!!
P.S. i think we'd be using arrays here.... yes????
13 replies to this topic
#1
Posted 01 December 2010 - 05:44 AM
|
|
|
#2
Posted 01 December 2010 - 07:26 AM
Arrays are not very flexible and often require a little more coding to use.
Usually it is better to use a List, Set or Map, depending on it's use.
In your case I would use a List, because duplicate values van occur (because it is user input), so you can't use a Set, and you don't need key-value-pairs so you don't need a Map.
So I would;
- read the user input (5 times)
- add it to a List<Integer>-instance
- create 3 List<Integer> instances (for positives, negatives and zeros)
- iterate over the List with input-numbers and add it to the proper List
- print the three sizes of the Lists
Usually it is better to use a List, Set or Map, depending on it's use.
In your case I would use a List, because duplicate values van occur (because it is user input), so you can't use a Set, and you don't need key-value-pairs so you don't need a Map.
So I would;
- read the user input (5 times)
- add it to a List<Integer>-instance
- create 3 List<Integer> instances (for positives, negatives and zeros)
- iterate over the List with input-numbers and add it to the proper List
- print the three sizes of the Lists
#3
Posted 01 December 2010 - 07:46 AM
waaaaiiiiiiii!!!!!!! thanks for the help!!!!! imma start to get down to work wid your suggestion... THANK YOU!!!!!:thumbup:
#4
Posted 01 December 2010 - 08:14 AM
Metalhead said:
- create 3 List<Integer> instances (for positives, negatives and zeros)
- print the three sizes of the Lists
- print the three sizes of the Lists
3 simple ints arent good enough?
#5
Posted 01 December 2010 - 09:03 AM
Metalhead, you are right. It is easier here to use list. Arrays are used in more complex problems.
#6
Posted 01 December 2010 - 09:30 AM
I tried making the code.. but all i can manage to scrape was this:
*sigh*... i'm really dumb....:confused:
String a=JOptionPane.showInputDialog ("Enter first number of array:");
int f=Integer.parseInt (a);
String b=JOptionPane.showInputDialog ("Enter second number of array:");
int g=Integer.parseInt (b);
String c=JOptionPane.showInputDialog ("Enter third number of array:");
int h=Integer.parseInt ©;
String d=JOptionPane.showInputDialog ("Enter fourth number of array:");
int i=Integer.parseInt (d);
String e=JOptionPane.showInputDialog ("Enter fifth number of array:");
int j=Integer.parseInt (e);
ArrayList n=new ArrayList();
n.add(f);
n.add(g);
n.add(h);
n.add(i);
n.add(j);
Object[] k=n.toArray();
int m=k[0];
ArrayList negative=new ArrayList();
ArrayList zero=new ArrayList ();
ArrayList positive=new ArrayList ();
for (int o; o<k.length; o++){
if (k[o]<0){
negative.add(k);
}
else if (k[o]==0){
zero.add(k);
}
else{
positive.add(k);
}
}
Object [] l=negative.toArray();
Object [] p=zero.toArray();
Object [] q=positive.toArray();
System.out.println ("Number of negative numbers in array: "+l.length);
System.out.println ("Number of zeros in array: "+p.length);
System.out.println ("Number of positive numbers in array: "+q.length);
}
}
and it still had 3 errors...*sigh*... i'm really dumb....:confused:
#7
Posted 01 December 2010 - 10:01 AM
Try something like this:
I haven't tried it so I'm not 100% sure it will compile with no errors.
ArrayList <Integer> numbers = new ArrayList<Integer>();
try{
String a=JOptionPane.showInputDialog ("Enter first number of array:");
int x=Integer.parseInt(a);
numbers.add(x);
a=JOptionPane.showInputDialog ("Enter second number of array:");
x=Integer.parseInt(a);
numbers.add(x);
a=JOptionPane.showInputDialog ("Enter third number of array:");
x=Integer.parseInt(a);
numbers.add(x);
a=JOptionPane.showInputDialog ("Enter fourth number of array:");
x=Integer.parseInt(a);
numbers.add(x);
} catch(NumberFormatException e){
//if the user doesn't input a number or hits the cancel button, code here will be executed
}
int positive=0, zero=0, negative=0;
int size=numbers.size();
for(int i=0;i<size;i++){
int t=numbers.get(i);
if(t<0) negative++;
else if(t==0) zero++;
else positive++;
}
System.out.println("Number of positive numbers " + positive);
System.out.println("Number of negative numbers " + negative);
System.out.println("Number of zeros " + zero);
I haven't tried it so I'm not 100% sure it will compile with no errors.
#8
Posted 01 December 2010 - 10:22 AM
OMG!!!!! You just totally solved my problem!!!!!!! Thanks a lot!!!!!!(jumps up and down the chair)Waaaaaaaaiiiiiiii!!!!!!! i compiled it and it worked!!!!! can't wait to analyze this with my minute pea-sized brain...:thumbup:
#9
Posted 01 December 2010 - 10:33 AM
You're welcome :D
Anyway, try to modify it like this: if the user gives a wrong input, the program re-asks the question.
Example: "Input third number". The user inputs "54fn" and the program asks again "Input third number"
Anyway, try to modify it like this: if the user gives a wrong input, the program re-asks the question.
Example: "Input third number". The user inputs "54fn" and the program asks again "Input third number"
#10
Posted 01 December 2010 - 10:49 AM
hmm mm*nods*... yeah... and i also modified it into using arrays just for variety.... A kazillion thanks!!!!!
#11
Posted 01 December 2010 - 10:52 AM
last question... what if the user wants to input 6 numbers? or 4? :D
#12
Posted 01 December 2010 - 11:01 AM
i dink using a loop... after specifying how many numbers you want to enter.... is that it???
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









