• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping duplicate statements in arrays  RSS feed

 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am a beginner in Java and this is my first time doing arrays. My code purpose is to get an input of numbers of so many size and allocate them to the positive, negative or neither statements. The problem I am having is that I do not know how to stop duplicate positive, negative and neither statements to appear when running. I tried taking the if statements out using various methods, but I have been getting araysoutofbound errors.

So I just need statements like these to appear:

Positive: # # #
Negative: # # #
or
Neither

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just ran your code, and it didn't give an outofbounds exception:

C:\slop>java Meh
Enter size: 5
Enter 5 integers: -1
3
0
3
-11
Negative -1Positive: 3Neither Positive: 3Negative -11

Do you want a single line printed that lists all the positives, then another with the negatives?

if so, then it doesn't make send to print the "Negative" or "Positive" as you loop through the array contents.  you need to build up the list of number, and then when you're done looping through, print out what you want to see.
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I just ran your code, and it didn't give an outofbounds exception:

C:\slop>java Meh
Enter size: 5
Enter 5 integers: -1
3
0
3
-11
Negative -1Positive: 3Neither Positive: 3Negative -11

Do you want a single line printed that lists all the positives, then another with the negatives?

if so, then it doesn't make send to print the "Negative" or "Positive" as you loop through the array contents.  you need to build up the list of number, and then when you're done looping through, print out what you want to see.


Yes basically if I do a run I want to see:

Enter size: 6
Enter 6 integers: -4 -12 0 -4 -9 -10
Negative: -4 -12 -4 -9 -10

The problem is I have no idea what the if else condition will be out the loop due to if I use say posneg[i] out the loop then I get the arraysoutofboundexception error.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Do you want to iterate the array thrice, once looking for negative numbers, once for 0 and one for positive?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so...stop thinking about java.  think about it in English (or any natural language).  What do you need to do?

ask the user how many numbers - this seems to work
get the numbers from the user - this seems to work
save the numbers to an array- this seems to work.

so now what do you need to do?

you want to print the literal string "Negative", followed by the negative numbers.  So you need to either
a)  have them already in a way you can print them right now,
or
b) find them and print them as you come across them.

You'd then need to do something similar for the positive numbers.

think on that, give it a try, and let us know what you come up with.
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to do the problem by creating boolean flags to track the first number entered and also did the same with the neither statement.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done Please show us how you managed it.
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the late reply I been busy lately studying

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!