Jump to content

number counter...

- - - - -

  • Please log in to reply
13 replies to this topic

#1
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
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????


#2
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
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

#3
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
waaaaiiiiiiii!!!!!!! thanks for the help!!!!! imma start to get down to work wid your suggestion... THANK YOU!!!!!:thumbup:

#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

Metalhead said:

- create 3 List<Integer> instances (for positives, negatives and zeros)

- print the three sizes of the Lists

3 simple ints arent good enough?

#5
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Metalhead, you are right. It is easier here to use list. Arrays are used in more complex problems.

#6
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
I tried making the code.. but all i can manage to scrape was this:

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
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Try something like this:


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
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
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
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
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"

#10
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
hmm mm*nods*... yeah... and i also modified it into using arrays just for variety.... A kazillion thanks!!!!!

#11
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
last question... what if the user wants to input 6 numbers? or 4? :D

#12
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
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