# Simple counting program using loops

Page 1 of 1

## 11 Replies - 9154 Views - Last Post: 14 October 2010 - 08:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=195054&amp;s=d4297eaf94fec24424bae1453b838e2a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MoMahar26

Reputation: 1
• Posts: 13
• Joined: 13-January 10

# Simple counting program using loops

Posted 14 October 2010 - 07:42 PM

I am having an incredibly difficult time kicking this problem off.
Here is the exact program that my professor wants:

Write a JAVA program that reads an unspecified number integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating point number.

The sample run should look as follows:

Enter and integer value, the program exits if the input is 0:
1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25

We have only gone over "for loops" in class, so other examples I have seen using while loops, etc., have just confused me more. I understand how to do the looping part, I believe, but I am having trouble understanding how to get the users input without being able to store individuals values (n1,n2,n3..) since the user is entering in integers in an unspecified number.

Here is what I know I need to do:
Test to see if the number is positive, negative, or zero.
If positive, add to the count, positive integer count, and total.
If negative, add to the count, negative integer count, and total.
If zero, stop.

Any help would be appreciative, but please keep in mind that I must use a for loop.

Thank You!

Is This A Good Question/Topic? 0

## Replies To: Simple counting program using loops

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:10 PM

Welcome at DIC

Rule #1 of this forum that was showed to you in a big pink rectangle when you posted:
We will not do your homework for you! Do not ask us to give you code!

[rules][/rules]

We will be glad to help if you have problem you with the code you wrote.

### #3 MoMahar26

Reputation: 1
• Posts: 13
• Joined: 13-January 10

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:18 PM

See, I think I have the code. At least for the loops. However, I don't understand how to get the integers from the user in order to put them into my program (if this makes sense.)

I suppose my question would be:
How do I get user input as individual integers in order to plug them into my loop?
Basically, I'm having problems beginning the code, and thus have none to post.

For reference, here is what I have already. It is very incomplete, as I'm not sure how to proceed.

```package programcounting;
import java.util.Scanner;

/**
*
* @author mmahar1
*/

public class Main
{//begin of Main

public static void main(String[] args)
{//begin of main
//declarations
Scanner keyboard=new Scanner(System.in);
int count, poscount, negcount;

}//end of main

```

### #4 b0ng01

Reputation: 38
• Posts: 169
• Joined: 16-July 10

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:18 PM

```
Integer num = null;
Scanner s = new Scanner(System.in);
num = s.nextInt();

```

### #5 MoMahar26

Reputation: 1
• Posts: 13
• Joined: 13-January 10

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:24 PM

Could you explain:

Integer num = null;

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:32 PM

MoMahar26, on 14 October 2010 - 09:24 PM, said:

Could you explain:

Integer num = null;

Actually not a very good idea at all to use an Integer object there
(Double autoboxing for nothing. (Ignore that sentence for now)
This will do the job and is more simple to understand and will have less overhead

```Scanner s = new Scanner(System.in);
int num = s.nextInt();

```

This post has been edited by pbl: 14 October 2010 - 08:33 PM

### #7 b0ng01

Reputation: 38
• Posts: 169
• Joined: 16-July 10

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:35 PM

I see no reason as why using an Integer there matters. Clarity is another issue, but there is no benefit to using an int instead of an Integer. I used Integer, because it is a common programming practice for me. Use an Object, unless there is a reason to use the primitive. The auto-boxing is an issue, but I can't see that being a reason not to do it.

I am curious what's the difference in two. How much overhead does auto-boxing add?

This post has been edited by b0ng01: 14 October 2010 - 08:40 PM

### #8 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:42 PM

b0ng01, on 14 October 2010 - 10:35 PM, said:

...Use an Object, unless there is a reason to use the primitive. The auto-boxing is an issue, but I can't see that being a reason not to do it.

I am curious what's the difference in two. How much overhead does auto-boxing add?

Woah...You got it backwards buddy. If possible, use a fundamental type. They are simpler to use, to cast, it requires no object references, no autoboxing.

### #9 MoMahar26

Reputation: 1
• Posts: 13
• Joined: 13-January 10

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:46 PM

So using this will allow the program to take each integer the user inputs, or just the first one?
Here is how my code is progressing:

```package programcounting;
import java.util.Scanner;

/**
*
* @author mmahar1
*/

public class Main
{//begin of Main

public static void main(String[] args)
{//begin of main
//declarations
Scanner keyboard=new Scanner(System.in);
int count, poscount, negcount, total;

//get user input
System.out.println("Enter an integer value, the program exits if the "
+ "input is 0:");
int num=keyboard.nextInt();

//for loops

}//end of main

}//end of Main

```

### #10 b0ng01

Reputation: 38
• Posts: 169
• Joined: 16-July 10

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:50 PM

This is beyond the scope of the OP, but if I am coding and I know I am going to be doing operations on an int. It makes more sense to just make it an Integer. In this instance is it overkill yes, but what's the cost? Can you give me one legitimate reason that using the primitive is better, I don't mean minuscule performance differences.

### #11 MoMahar26

Reputation: 1
• Posts: 13
• Joined: 13-January 10

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:58 PM

b0ng01, on 14 October 2010 - 07:50 PM, said:

This is beyond the scope of the OP, but if I am coding and I know I am going to be doing operations on an int. It makes more sense to just make it an Integer. In this instance is it overkill yes, but what's the cost? Can you give me one legitimate reason that using the primitive is better, I don't mean minuscule performance differences.

The only reason I didn't incorporate it into my code is just because I'm not sure how to use it. I'm only familiar with int. I'm in intro, and my professor has mentioned it but I just don't understand it clearly.

### #12 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Simple counting program using loops

Posted 14 October 2010 - 08:59 PM

b0ng01, on 14 October 2010 - 10:50 PM, said:

This is beyond the scope of the OP, but if I am coding and I know I am going to be doing operations on an int. It makes more sense to just make it an Integer. In this instance is it overkill yes, but what's the cost? Can you give me one legitimate reason that using the primitive is better, I don't mean minuscule performance differences.

Can we keep this post on-topic please? If you want to ask the merits of it, make a post in the Java Programmers forum. You'll get more feedback there anyway. Thanks you.

MoMahar26, on 14 October 2010 - 10:58 PM, said:

The only reason I didn't incorporate it into my code is just because I'm not sure how to use it. I'm only familiar with int. I'm in intro, and my professor has mentioned it but I just don't understand it clearly.

int will do just fine, if not better.