A list of 6 integers is input into ArrayList listInts. Complete the program to copy only the negative integers to a new ArrayList listNegInts. Output the number of negative elements, and the negatives list. Ex: For input 5 -2 0 9 -66 -4, output is:
When declaring listNegInts, don't specify a size. Instead, add elements using listNegInts.add(…)
Write a first for loop that copies only the negative integers to listNegInts, then write a second for loop that outputs.
Your copying for loop should have an if statement that only copies the current listInts element if negative.
The number of items in listNegInts can be determined using listNegInts.size(). No need to have a separate variable keeping track.
This is my code:
I have atttached picutres of what my expected output should be. Can anyone help fix my probelm? So that my output is correct.
If you posted what you tried to do, and what the results were, we might be in a position to help. But "does not work" gives us nothing to work with (short of doing the work ourselves, but I think the point of these exercises is that you are supposed to learn something).
I tried to help you but I have never used the Scanner class. I need an answer and fast. So I google "Java Scanner how to use it".
Now I get the idea (on a side note, where is the line where the scanner is closed?) and I am confused.
What do you expect from the input? A string made of integers separated by space i.e. "1 -3 5" or directly a list of integers i.e. 1 then -3 then 5?
This expectation has to be clarified in your code.
From your code it seems you expect the user to enter integers one at a time. You tell us it does not work as expected.
Either you use a debugger in a JavaIDE or you carefully read your code to imagine what the hell it is doing or you add after line 16: