So the prompt is here:
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 maximum integer is 0
The sum of the positive integers is 0
The sum of the odd integers is 0
The count of even integers in the sequence is 1
``````

This means that using all numbers your program reads (including the last number 0), you need to compute the maximum, the sum of only positive integers (integers that are greater than 0), the sum of odd integers (integers that cannot be divided by 2, you can use "num%2 != 0"), and count how many even integers (integers that can be divided by 2) 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.

My Code:

``````import java.util.Scanner;  // uses the Scanner package located in the "java.util" directory

public class Assignment2
{
public static void main (String[] args)
{
int number;  // declaring an integer: number

Scanner console = new Scanner(System.in);  // creates a new scanner

max_int = -1; // so the first int will be larger than this
even_count = 0; // 0 because we don't have any yet

while((num = console.nextInt()) != 0) {

do  // performs a do loop that executes several if statements to see if values meet the criteria
{

if(num > max_int)  // This calculates whether or not the integer is the greatest amount
{
max_int = num;  // sets max integer equal to the number
}

if(num > 0)  // Adds the number to the sum if the integer is positive.
{
pos_sum = pos_sum + num;
}

if (num % 2 != 0)  // Adds the integer to the sum of odds if the number is odd
{
odd_sum = odd_sum + num;
}

if (num % 2 == 0)  // This counts the number of even numbers
{
even_count++;
}

}

System.out.print("The maximum integer is" + max_int + "\n"
+ "The sum of the positive integers is" + pos_sum + "\n"
+ "The sum of the odd integers is" + odd_sum + "\n"
+ "The count of even integers in the sequence is" + even_count + "\n");

}

}
``````

I keep getting errors about parsing and the code expecting something and even some initializations. I've taken Java last semester, but after summer I kind of forgot all the syntax. I'm really have some big troubles with this, already spent 2 hours doing this and i'm getting migrains. If anyone can help correct me code, it'll be high appreciated. I don't even know if I'm doing it right, but I've tried unlike some people who just post their questions hoping someone can do all their hw. I'm here to learn and see what I did wrong.

In the future, you'll want to use the CODE tags - easily obtained by clicking on the word CODE, in brackets, above your editing window. That makes it a lot easier to read your code. You should also include and error report that your compiler generates. This will point us to your immediate points of failure, so you can clear those up and get something running.

Here's a line that will present a logical problem eventually:

``max_int = -1; // so the first int will be larger than this``

If I were your professor, I'd include a sequence like
-5,-89,-45,-2,-4,-10
in my tests. Of course, your code would report -1 as the maximum integer, right?

There are two ways you can address this. One would be to treat the first integer separately, and set max_int to the first number you see without comparison. The second would be to make use of the defined constant representing the minimal integer representable - I believe that'll be defined in the Math library. Of course, the user cannot enter an integer lower than that, so you'd be safe in assuming any number input was greater than or equal to that number.

More immediate concern for you is that you do not declare max_int or even_count, you just attempt to initialize them, and you neither declare nor initialize odd_sum or pos_sum.

A legitimate variable declaration consists of

type var_name { optional declaration } ;

where type is a primitive (int, float, char, etc) or a class accessible to this class (ie, declared or in the same package), the var_name is a legitimate variable name, and the declaration is an equal sign followed by an expression evaluating to something assignable to the type of the declaration.

public class Foo{
boolean TrueOrFalse = someOtherBoolean; //legitimate
float f = 56; // legitimate due to promotion, this will be stored as 56.0f
String s; //legitimate, though you can't read s until you assign something to it

q = 9; // not legitimate, how does the compiler know what type q ought to be?
}

Thanks for your reply, I'm still feeling uneasy about this. I process info slow so it takes awhile to process in my brain. I don't get if I'm doing it right or not. The instructor is telling us to use some loops to perform the tasks. It's due friday and I really want it to finish a a day or two before.

"I get reach end of file parsing" no matter how many brackets i put/remove it still says it. Am I doing this code right?

In the future, you'll want to use the CODE tags - easily obtained by clicking on the word CODE, in brackets, above your editing window. That makes it a lot easier to read your code. You should also include and error report that your compiler generates. This will point us to your immediate points of failure, so you can clear those up and get something running.

Here's a line that will present a logical problem eventually:

``max_int = -1; // so the first int will be larger than this``

If I were your professor, I'd include a sequence like
-5,-89,-45,-2,-4,-10
in my tests. Of course, your code would report -1 as the maximum integer, right?

There are two ways you can address this. One would be to treat the first integer separately, and set max_int to the first number you see without comparison. The second would be to make use of the defined constant representing the minimal integer representable - I believe that'll be defined in the Math library. Of course, the user cannot enter an integer lower than that, so you'd be safe in assuming any number input was greater than or equal to that number.

More immediate concern for you is that you do not declare max_int or even_count, you just attempt to initialize them, and you neither declare nor initialize odd_sum or pos_sum.

A legitimate variable declaration consists of

type var_name { optional declaration } ;

where type is a primitive (int, float, char, etc) or a class accessible to this class (ie, declared or in the same package), the var_name is a legitimate variable name, and the declaration is an equal sign followed by an expression evaluating to something assignable to the type of the declaration.

public class Foo{
boolean TrueOrFalse = someOtherBoolean; //legitimate
float f = 56; // legitimate due to promotion, this will be stored as 56.0f
String s; //legitimate, though you can't read s until you assign something to it

q = 9; // not legitimate, how does the compiler know what type q ought to be?
}

"I get reach end of file parsing"

Is that an error you get when trying to compile the program?
Some times you need to add some } at the end of the program.
Add one } and try compiling. Same error, then add another. etc

If that doesn't work then:
Does you editor have a matching {} feature? You place the cursor on a { or a } , select the "Find matching" menuitem and the cursor is moved to the matching } or {.

If your editor does have the feature, start at the middle of the program and match {}. Move out one level and do it again. Continue until a match is not found. I'll go through each requirement:

I would first take all the inputs then calculate. Store everything in an array(list). That would probably be easiest.

1. The maximum integer is...
Don't assume the maximum integer is anything, like jon said. I would create a variable, initialize it to the first element in the entries, and then compare it to each following element.

2. The sum of the positive integers is...
Create a int variable. Run through the entries array(list) with a for loop. Use a simple if statement check to see if the current number is >0. If so, add it to the int variable.

3. The sum of the odd integers is...
Same as above, use the mod command to check parity.

4. The count of even integers in the sequence is...
Same as above, but this time increment the variable by 1, not the value of the current number

"I get reach end of file parsing" no matter how many brackets i put/remove it still says it. Am I doing this code right?

Can you post the compiler output, so we can see what you're seeing?

I'll go through each requirement:

I would first take all the inputs then calculate. Store everything in an array(list). That would probably be easiest.

1. The maximum integer is...
Don't assume the maximum integer is anything, like jon said. I would create a variable, initialize it to the first element in the entries, and then compare it to each following element.

If you're storing everything in an arraylist, this makes sense. If you want to handle the input as it comes in, you can start by setting max to the smallest integer your machine can represent, which again is given as a constant. I was wrong on the class that holds it, it's in Integer - see Integer.MIN_VALUE. The advantage of processing the input on the way in is that it's a more efficient use of space, and also leads to better use of time, since you're only handling each input once. (you don't have to put it into and take it out of a structure)
The saving won't be significant in this program, of course, but it's worth thinking about efficiency in design any time you can. Good mental hygiene.

Since you're coming ahead of schedule, here's some things to think about:

Consider the arraylist and the as-you-go approaches.
- A typical tradeoff in programming is time efficiency versus space efficiency. That is, you write a program that runs a little faster by loading a whole bunch of stuff into memory for easier access, or you run a little smaller by leeting it go a little slower. Is that trade-off in effect? How would you determine which is likely to run faster, and which is likely to require more space in memory to run?

- A more important tradeoff is time/space efficiency versus coding and maintenance time. Efficiencies gained by writing obscure or difficult to read code end up costing hours of coding time to save some run time or to run a little tighter in memory. In most cases, code that's easier to read and modify is preferable to code that's more obscure but faster. Which of the two approaches is easier to understand from the coder's point of view?

Don't worry, you won't be tested on the answers, but you should practise thinking about these tradeoffs now, with simple programs, so you're better able to go by instinct later on.

Thank you for the replies, I've solved my java program! its working!

commented: Start new thread for a new question +0

i want a program to know the radius and circumferance thnx