Can anybody help me with a Java program?

MournSanity

Diamond Member
Feb 24, 2002
3,126
0
0
I'm trying to make a program with three different arrays, evenList, oddList, and negativeList. The program takes 10 integer inputs and from those assigns the even integers to evenList, odd integers to oddList and so on. Then it displays the contents of each array. It seems simple enough, but I'm kind of stumped. Here is my partial source code, I'm trying to get it to work right. Can anybody help me out?



import TerminalIO.KeyboardReader;

public class sorter
{
public static void main (String [] args)
{
int[] evenList = new int[10];
int[] oddList = new int[10];
int[] negativeList = new int[10];

int n1,n2,n3,n4,n5,n6,n7,n8,n9,n10;

KeyboardReader input = new KeyboardReader();


System.out.print("Input an integer: ");
n1 = input.readInt();

if (n1 > 0 && n1 % 2 = 0)
n1 = evenList;
else (n1 < 0 && % 2 = 0)
n1 = oddList;
else n1 = negativeList;



System.out.print("Input an integer: ");
n2 = input.readInt();
System.out.print("Input an integer: ");
n3 = input.readInt();
System.out.print("Input an integer: ");
n4 = input.readInt();
System.out.print("Input an integer: ");
n5 = input.readInt();
System.out.print("Input an integer: ");
n6 = input.readInt();
System.out.print("Input an integer: ");
n7 = input.readInt();
System.out.print("Input an integer: ");
n8 = input.readInt();
System.out.print("Input an integer: ");
n9 = input.readInt();
System.out.print("Input an integer: ");
n10 = input.readInt();



}
}


As you can see, I'm trying to sort it as the user inputs the integer. But I screwed up the if statement and everything, and I don't know how to display the contents of the array. You don't know how grateful I would be if I get some help.

 

fs5

Lifer
Jun 10, 2000
11,774
1
0
first off equality is represented by == not =, second, n1 = evenList doesn't make much sense, you're assigning a single number to an array.
Also you should move the part where you decide if it's odd/even/negative into a function so it's reuseable.

it's late, I'm sure you'll get some more help else where.

(ps. support the thread in my sig)
 

BigJ

Lifer
Nov 18, 2001
21,335
1
81
You assign values in an array by giving the value at a specific index a value, not the array itself. So array evenList[0] would be the first value in the array, while evenList[9] would be the last value.

The way you have it setup, you're going to need an index variable for even, odd, and negative lists. Once you add an a value to the array at the index, you then increment the index.

For primitive data types, == indicates that something is equal to each other, = assigns a value.
 

MournSanity

Diamond Member
Feb 24, 2002
3,126
0
0
Ok, so would it be something like this?


if (n1 >= 0 && n1 % 2 = 0)
evenList[1] = n1;
else if (n1 < 0 && n1 % 2 = 0)
negativeList[1] = n1;
else oddList[1] = n1;


This doesn't compile though...but am I on the right track?
 

BigJ

Lifer
Nov 18, 2001
21,335
1
81
Originally posted by: hypersonic5
Ok, so would it be something like this?

int evenListIndex, oddListIndex, negativeListIndex;


if (n1 >= 0 && n1 % 2 == 0) {
evenList[evenListIndex] = n1;
evenListIndex++;
}
else if (n1 < 0 && n1 % 2 == 0) {
negativeList[negativeListIndex] = n1;
negativeListIndex++;
}
else {
oddList[oddListIndex] = n1;
oddListIndex++;
}


This doesn't compile though...but am I on the right track?

See my changes in bold. Also notice the use of ==.

Notice however, this is still wrong. An odd negative number will never be in the negative array.
 

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
Can you use lists?
If so, it's simple.

If not, then create an array of size 10, read all numbers to that one array and use 3 counters. odd counter, even counter, and negative counter. You won't need 10 different ints. When it's done, create the 3 arrays with a size of it's corresponding counter. Then just loop through and copy the numbers to whichever array they belong to.

for even you need to use

if ( ( number > 0 ) && ( number % 2 == 0 ) )

odd

if ( ( number > 0 ) && ( number % 2 == 1 ) )

else

it's negative.
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
heh...i know this is waaaaaay past over due on your hwk. but i dont see why you wrote your program the way you did, it doesn't make sense.
you should've used a while loop

int i = 0, even = 0, odd = 0, neg = 0;

while(true) {
if (i == 10) break;
System.out.print("Input an integer: ");
n = input.readInt();
if (n >= 0 && n % 2 = 0)
evenList[even++] = n;
else if (n >= 0 && % 2 != 0)
oddList[odd++] = n;
else
negativeList[neg++] = n;
}

 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: Bacardi151
heh...i know this is waaaaaay past over due on your hwk. but i dont see why you wrote your program the way you did, it doesn't make sense.
you should've used a while loop

int i = 0, even = 0, odd = 0, neg = 0;

while(true) {
if (i == 10) break;
System.out.print("Input an integer: ");
n = input.readInt();
if (n >= 0 && n % 2 = 0)
evenList[even++] = n;
else if (n >= 0 && % 2 != 0)
oddList[odd++] = n;
else
negativeList[neg++] = n;
}

that seems pretty good (sans the i problem)...though, are even and odd numbers supposed to be void of negative numbers? If not, you would potentially have the same number in multiple arrays, though it's a very simple change of code to allow this

oh, and break statements usually aren't preferred. Just use a for loop
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
Originally posted by: MAME
Originally posted by: Bacardi151
heh...i know this is waaaaaay past over due on your hwk. but i dont see why you wrote your program the way you did, it doesn't make sense.
you should've used a while loop

int i = 0, even = 0, odd = 0, neg = 0;

while(true) {
if (i == 10) break;
System.out.print("Input an integer: ");
n = input.readInt();
if (n >= 0 && n % 2 = 0)
evenList[even++] = n;
else if (n >= 0 && % 2 != 0)
oddList[odd++] = n;
else
negativeList[neg++] = n;
}

that seems pretty good...though, are even and odd numbers supposed to be void of negative numbers? If not, you would potentially have the same number in multiple arrays, though it's a very simple change of code to allow this

oh, and break statements usually aren't preferred. Just use a for loop

i would imagine that you only put positive integers in the first two arrays, and whatever is negative (odd or even) will be put in the negative array.
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
oh yeah, i need to put an increment for i, before it loops around.

but yeah a for loop could do it as well, (for int i = 0, even = 0, odd = 0, neg = 0; i < 10; i++) {...}

on another note, why is it less preferred to use while (do/while) statements? or are you talking about break in general? less efficient or ?
but i should've actually have put while(i < 10)
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: Bacardi151
oh yeah, i need to put an increment for i, before it loops around.

but yeah a for loop could do it as well, (for int i = 0, even = 0, odd = 0, neg = 0; i < 10; i++) {...}

on another note, why is it less preferred to use while (do/while) statements? or are you talking about break in general? less efficient or ?
but i should've actually have put while(i < 10)

breaks in the middle of loops probably are a poor choice for a number of reasons:
the location of the loop control could be anywhere in the loop, the terminating clause is not easily deciphered, future updates require more work as you have to search for every break in order to assess the situation

same as GOTO statements I'd imagine, less flow with the code
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
i have no experience with reading/updating other peoples code so i can't comment on that.

but sometimes you're forced to use while loops, if there's no fixed number of iterations, although in this case there is.

but imagine if the program was to ask for an integer input until the person put in a specific value which means quit. you wouldn't be able to implement that using a for loop.

instead you use an if statement to check if the specified exit value/string, is inputted and if it is, then you get out of the loop by a break statement. i really dont see the hardship in reading that.
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: Bacardi151
i have no experience with reading/updating other peoples code so i can't comment on that.

but sometimes you're forced to use while loops, if there's no fixed number of iterations, although in this case there is.

but imagine if the program was to ask for an integer input until the person put in a specific value which means quit. you wouldn't be able to implement that using a for loop.

instead you use an if statement to check if the specified exit value/string, is inputted and if it is, then you get out of the loop by a break statement. i really dont see the hardship in reading that.

FYI: for loops are while loops, thus anything a while loop can do, a for loop can as well

If you want to control input from a user, you could do:
while(input != (exiting input) )

There nothing overly horrible with your example, but it's just small scale. Think if the code was production level, say 1,000 lines of code for one loop. You don't want to trace every break statement and condition

I can speak with my teachers if you'd like, but assuming I pass my Art History final in 5 hours, I won't likely seem them in person ever again come wednesday ;)
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
Originally posted by: MAME
Originally posted by: Bacardi151
i have no experience with reading/updating other peoples code so i can't comment on that.

but sometimes you're forced to use while loops, if there's no fixed number of iterations, although in this case there is.

but imagine if the program was to ask for an integer input until the person put in a specific value which means quit. you wouldn't be able to implement that using a for loop.

instead you use an if statement to check if the specified exit value/string, is inputted and if it is, then you get out of the loop by a break statement. i really dont see the hardship in reading that.

FYI: for loops are while loops, thus anything a while loop can do, a for loop can as well

If you want to control input from a user, you could do:
while(input != (exiting input) )

There nothing overly horrible with your example, but it's just small scale. Think if the code was production level, say 1,000 lines of code for one loop. You don't want to trace every break statement and condition

I can speak with my teachers if you'd like, but assuming I pass my Art History final in 5 hours, I won't likely seem them in person ever again come wednesday ;)


well how do you suggest you do an infinite loop with a for loop? set the limit to a very large number? it seems impractical, or perhaps you set it to something that will never be true. such as (for i = 0; i < -1; i++) , but yet again, it seems impractical. and the break statement is obviously put at the very top next to an if statement because that's the first thing you should check before doing any unnecessary computations. and i doubt anybody puts 1000 lines of code inside a while loop, let alone 100.
 

neutralizer

Lifer
Oct 4, 2001
11,552
1
0
Wouldn't it be easier to use Arraylists so that the array has minimal size and so its easier to print?
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: Bacardi151
Originally posted by: MAME
Originally posted by: Bacardi151
i have no experience with reading/updating other peoples code so i can't comment on that.

but sometimes you're forced to use while loops, if there's no fixed number of iterations, although in this case there is.

but imagine if the program was to ask for an integer input until the person put in a specific value which means quit. you wouldn't be able to implement that using a for loop.

instead you use an if statement to check if the specified exit value/string, is inputted and if it is, then you get out of the loop by a break statement. i really dont see the hardship in reading that.

FYI: for loops are while loops, thus anything a while loop can do, a for loop can as well

If you want to control input from a user, you could do:
while(input != (exiting input) )

There nothing overly horrible with your example, but it's just small scale. Think if the code was production level, say 1,000 lines of code for one loop. You don't want to trace every break statement and condition

I can speak with my teachers if you'd like, but assuming I pass my Art History final in 5 hours, I won't likely seem them in person ever again come wednesday ;)


well how do you suggest you do an infinite loop with a for loop? set the limit to a very large number? it seems impractical, or perhaps you set it to something that will never be true. such as (for i = 0; i < -1; i++) , but yet again, it seems impractical. and the break statement is obviously put at the very top next to an if statement because that's the first thing you should check before doing any unnecessary computations. and i doubt anybody puts 1000 lines of code inside a while loop, let alone 100.

an infinite for loop = for( ;;; )

and people easily put a 100+ lines of code in a loop
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
Originally posted by: MAME
Originally posted by: Bacardi151
Originally posted by: MAME
Originally posted by: Bacardi151
i have no experience with reading/updating other peoples code so i can't comment on that.

but sometimes you're forced to use while loops, if there's no fixed number of iterations, although in this case there is.

but imagine if the program was to ask for an integer input until the person put in a specific value which means quit. you wouldn't be able to implement that using a for loop.

instead you use an if statement to check if the specified exit value/string, is inputted and if it is, then you get out of the loop by a break statement. i really dont see the hardship in reading that.

FYI: for loops are while loops, thus anything a while loop can do, a for loop can as well

If you want to control input from a user, you could do:
while(input != (exiting input) )

There nothing overly horrible with your example, but it's just small scale. Think if the code was production level, say 1,000 lines of code for one loop. You don't want to trace every break statement and condition

I can speak with my teachers if you'd like, but assuming I pass my Art History final in 5 hours, I won't likely seem them in person ever again come wednesday ;)


well how do you suggest you do an infinite loop with a for loop? set the limit to a very large number? it seems impractical, or perhaps you set it to something that will never be true. such as (for i = 0; i < -1; i++) , but yet again, it seems impractical. and the break statement is obviously put at the very top next to an if statement because that's the first thing you should check before doing any unnecessary computations. and i doubt anybody puts 1000 lines of code inside a while loop, let alone 100.

an infinite for loop = for( ;;; )

and people easily put a 100+ lines of code in a loop


100+ is very different from 1000+. which one's it gonna be?

for(; ; ; )...very bad coding, why write something like that? when you have a while loop that is designed to loop until you break out of it, while a for loop is used primarly to loop around a fixed # of times. i think the whole "break" statement = bad , is something a teacher has fed you.

lines with 100+ code in a loop is bad coding, and very hard to trace, probably the accumulated lines of code is some dozen but that's because there's functions inside the loop that does certain things. i don't see why a loop would consist of hundreds of lines of code when it can be summarized in different methods and recycled to use elsewhere
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: Bacardi151
Originally posted by: MAME
Originally posted by: Bacardi151
Originally posted by: MAME
Originally posted by: Bacardi151
i have no experience with reading/updating other peoples code so i can't comment on that.

but sometimes you're forced to use while loops, if there's no fixed number of iterations, although in this case there is.

but imagine if the program was to ask for an integer input until the person put in a specific value which means quit. you wouldn't be able to implement that using a for loop.

instead you use an if statement to check if the specified exit value/string, is inputted and if it is, then you get out of the loop by a break statement. i really dont see the hardship in reading that.

FYI: for loops are while loops, thus anything a while loop can do, a for loop can as well

If you want to control input from a user, you could do:
while(input != (exiting input) )

There nothing overly horrible with your example, but it's just small scale. Think if the code was production level, say 1,000 lines of code for one loop. You don't want to trace every break statement and condition

I can speak with my teachers if you'd like, but assuming I pass my Art History final in 5 hours, I won't likely seem them in person ever again come wednesday ;)


well how do you suggest you do an infinite loop with a for loop? set the limit to a very large number? it seems impractical, or perhaps you set it to something that will never be true. such as (for i = 0; i < -1; i++) , but yet again, it seems impractical. and the break statement is obviously put at the very top next to an if statement because that's the first thing you should check before doing any unnecessary computations. and i doubt anybody puts 1000 lines of code inside a while loop, let alone 100.

an infinite for loop = for( ;;; )

and people easily put a 100+ lines of code in a loop


100+ is very different from 1000+. which one's it gonna be?

for(; ; ; )...very bad coding, why write something like that? when you have a while loop that is designed to loop until you break out of it, while a for loop is used primarly to loop around a fixed # of times. i think the whole "break" statement = bad , is something a teacher has fed you.

lines with 100+ code in a loop is bad coding, and very hard to trace, probably the accumulated lines of code is some dozen but that's because there's functions inside the loop that does certain things. i don't see why a loop would consist of hundreds of lines of code when it can be summarized in different methods and recycled to use elsewhere

for( ;;; ) is not 'very bad coding', it's the same as while(true). Personally I would rather use while(true) but that's besides the point. I was just answering your question:
well how do you suggest you do an infinite loop with a for loop?

As for lines of code in a loop, I will ask my professor come wednesday to give me a concrete example of the largest size you would expect
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
Originally posted by: MAME
an infinite for loop = for( ;;; )

and people can easily put a 100+ lines of code in a loop but shouldn't. It's a bad programming practice. God invented functions for a reason.
Fixed. ;)
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
if (n1 > 0 && n1 % 2 = 0)
n1 = evenList;
else (n1 < 0 && % 2 = 0)
n1 = oddList;
else n1 = negativeList;

As a mathematician, I don't agree with how you are sorting your lists.

even = ... -4, -2, 0, 2, 4, ...
odd = ... -3, -1, 1, 3, ...
negative = ... -4, -3, -2, -1

-3 would appear in *both* the negative and the odd list.

A better test would be:
if (val % 2 = 0)
{
evenList[[/b]i] = val;
i++;
}

else
{
oddList[j] = val;
j++;
}

if (val < 0)
{
negativeList[k] = val;
k++;
}
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
Originally posted by: Kyteland
if (n1 > 0 && n1 % 2 = 0)
n1 = evenList;
else (n1 < 0 && % 2 = 0)
n1 = oddList;
else n1 = negativeList;

As a mathematician, I don't agree with how you are sorting your lists.

even = ... -4, -2, 0, 2, 4, ...
odd = ... -3, -1, 1, 3, ...
negative = ... -4, -3, -2, -1

-3 would appear in *both* the negative and the odd list.

A better test would be:
if (val % 2 = 0)
{
evenList[[/b]i] = val;
i++;
}

else
{
oddList[j] = val;
j++;
}

if (val < 0)
{
negativeList[k] = val;
k++;
}


that piece of code will put negative values in even and oddlist, that was not intended, since you're suppose to separate the negative values and put them in the negativeList

doing

if (positive & even) put in evenList
else if (positive & odd) put in oddList
else put in negativeList // this else statement assumes it's negative because the above conditions have tried to see if the value is positive already
 

Bacardi151

Senior member
Dec 15, 2003
540
0
0
well that's how i interpreted the problem, that he wanted to separate positive even, positive odd, and negative to its respective arrays
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: Kyteland
Originally posted by: MAME
an infinite for loop = for( ;;; )

and people can easily put a 100+ lines of code in a loop but shouldn't. It's a bad programming practice. God invented functions for a reason.
Fixed. ;)

100 function calls!