• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Can anyone help me fix my code?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

3
-2
-66
-4
Hints:

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.
Screen-Shot-2020-12-10-at-1.54.31-AM.png
[Thumbnail for Screen-Shot-2020-12-10-at-1.54.31-AM.png]
 
Saloon Keeper
Posts: 7355
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output now, and at what point in the code does it start to deviate from what you expected? What have you done so far to debug this?
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to get rid of Num_Elements and try to do numbers but it does not work.
 
Tim Moores
Saloon Keeper
Posts: 7355
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 40
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Java IDE or you carefully read your code to imagine what the hell it is doing or you add after line 16:

What do you see?



 
Marshal
Posts: 75866
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Pata wrote:. . . So I google "Java Scanner how to use it". . .

That's hazardous. You can pick up all sorts of bad advice and not know it's bad.

. . . on a side note, where is the line where the scanner is closed? . . .

Nowhere. the Scannerr is reading System.in and shouldn't be closed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic