Thread: Trouble implementing a solution plz help

1. Member
Join Date
Sep 2008
Posts
8
Rep Power
0

Trouble implementing a solution plz help

First of all I would like to say I've spent about 2 hours trying to solve this on my own and I failed. I'm not just asking for you to do my work for me. I tried one implementation using an ArrayList and another using two scanner objects , but both attempts were unsuccessful.

I have the following assignment

Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the inputted numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:

The minimum integer is 0
The sum of the positive integers is 0
The sum of the even integers is 0
The number of negative integers in the sequence is 0

This means that using all numbers your program reads (including the last number 0), you need to compute the minimum, the sum of only positive integers (integers that are greater than 0), the sum of even integers (integers that can be divided by 2, you can use "number%2 == 0"), and count how many negative integers (integers that are less than 0) in the sequence.

Note that the above is an output for the first test case. For other test cases, you will have different numbers.

Do not output a prompt to query for the numbers. The number 0 is included in the sequence of integers and should be included in all of your calculations.

As I briefly mentioned, I tried to accept the numbers from the user as one long string and then construct an array from that using a scanner object. I also tried storing the values in an ArrayList (my first experience with ArrayLists) and then using the x.toArray() method to construct an int array. Both techniques led to the compiler regurgitating countless error messages my way.

What's the best way to implement this?

Thanks.

2. Scanner class

Scanner (Java Platform SE 6)
Study the examples and pay special attention to the nextInt() method
You're idea about using arrayLists is correct since you don't know how many elements there are going to be.
You will need to use a "for" loop to populate the arraylist...
Java Code:
for ()
{
myArray[i] = scanner.nextInt();
}
or something close to that ... you can fill in the blanks.

Luck,
CJSL

3. why do you need to use arraylist? the assignment didn't ask you to keep track of all the int's entered.
Java Code:
while(!(n==0)){
// sum stuff here
sum += n;
n = input.nextInt();
}
n, sum = int
input = scanner

4. Member
Join Date
Sep 2008
Posts
8
Rep Power
0
Originally Posted by angryboy
why do you need to use arraylist? the assignment didn't ask you to keep track of all the int's entered.
What I need to output is
The minimum integer is 0
The sum of the positive integers is 0
The sum of the even integers is 0
The number of negative integers in the sequence is 0
I do need to store the numbers to be able to calculate those four answers.

5. is it something like this?
Java Code:
S:\SVN\chase>java Summer
Enter Integer. (enter 0 to stop)
-------------------------------------------------------
3 5 22 1 9
2 5 8 9 -1 15 -3
7 8 4 -2 0
-------------------------------------------------------
The minimum integer is -3
The sum of the positive integers is 98
The sum of the even integers is 42
The number of negative integers in the sequence is 3
I just used 4 int variables to hold the sum. and update it inside the while loop.

6. Member
Join Date
Sep 2008
Posts
8
Rep Power
0
Here is my attempt

Java Code:
// Assignment #: 2
import java.util.Scanner; //Used to accept the input intergers

public class Assignment2 {

public static void main(String[] args) {

int min = 0, psum = 0, esum = 0, ncount = 0; //Used to store the final output values. Updated in the while loop as each int is entered.

Scanner scan = new Scanner(System.in);

while (scan.hasNextInt()) {
if (scan.nextInt() == 0)
{
break;
}
else if (scan.nextInt() < min)
{
min = scan.nextInt();
}
else if (scan.nextInt() > 0)
{
psum += scan.nextInt();
}
else if (scan.nextInt() % 2 == 0)
{
esum += scan.nextInt();
}
else if (scan.nextInt() < 0)
{
ncount++;
}
}

System.out.println("The minimum interger is "+min);
System.out.println("The psum is "+psum);
System.out.println("The esum is "+esum);
System.out.println("The ncount is "+ncount);
}
}
but the output is always

The minimum interger is 0
The psum is 0
The esum is 0
The ncount is 0
regardless of the numbers entered.

What's wrong with my code? Thanks.

7. You are getting and discarding input every time you call scan.nextInt. You want to call this method once in the while loop, right at the beginning:

Java Code:
whilte (scan.hasNextInt())
{
int nextInt = scan.nextInt(); // call this once!
scan.nextLine();  // often useful to "swallow" the end of line char
if (nextInt == .....) // now do your tests on the int variable
{
//.... etc...

8. looking good man. couple of things to fix.

first, min should not be initialized to 0. it should be something large like 999999. what's the largest value for int before overflow? (hint: take a look at java.lang.Integer)

second, don't use else if, just use if statments.

lastly, fubarable has spoken it already...

9. Member
Join Date
Sep 2008
Posts
8
Rep Power
0
I got everything fixed and it works great thanks everybody.

10. So it's time to mark that thread is solved. Look at the Thread Tools menu on top of the first post of this thread.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•