This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

ArrayList mini-program  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So this is my code. As you can tell I am a beginner in java. It all started with the headfirst java book. I had some problems with String ArrayList that had to be an Int ArrayList, but it isn't important for now. So I was testing some ArrayLists and stuff and watched some youtube videos on that and tried to improve the code by myself. Now the problem is that it isn't working. The first part when the user types the input is ok, but when i try to delete the program just stops and doesnt respond to anything. Thank you for you help. Oh and I commented everything and I would kindly ask you to chech that and correct me if its wrong.
 
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

  doesnt respond to anything


IS the program stuck in an infinite loop?  Look at the while loops to see if any of them are stuck.  Add a print statement inside of all the loops that will show you which loop is stuck.
 
M. Gumblert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i found the infinite loop and it is ok now. But still isn't working. It doesn't asks for another userinput, just finished the program after one d elete. Here is how it looks now:
while (numOfDel != 3){
//while it is not 3(the number of userinputs) do the following things
for (int i = 0; i < array.size(); i++){
if (array.get(i) == del){
array.remove(i);

//a loop that is deleting the number if he finds it in the array and adds one to the variable numOfDel

}
numOfDel++;
}
}
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure to wrap all posted code in code tags to make it more readable.

Can you copy the program's print out and paste it here so we can see what happens when you execute the program?
Add some comments where you feel the program is doing the wrong thing.
 
M. Gumblert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is where the bad part begins.





As fot he output. It jus varies. Sometimes it cuts off after 2 numbers and sometimes it just doesn't cuts off and i can type numbers for eternity.
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy the program's output that shows what you are talking about.  A couple of examples would be good.
Add some comments where the program is not doing what you want.

When posting code, please don't break it up into sections.  Post the whole program in one section.

What does this comment mean:
Why is 1 being added to numOfDel?
 
Marshal
Posts: 6084
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

M. Gumblert wrote:


Check line 7 what your comment says and what actual code does. It increases numOfDel irrespective whether body of if statement has been entered.

I've never wrote such comments myself to explain what code is doing. I'd suggest you don't write them too. The main reason of mine was: they just clutter the code and prevents you from concentrating on actual code. Some of the comments are useful, however, they aren't present in this code. Useful comments are similar to "// changing value to 1 because it is an idle state". This comment also would be redundant going further, but that should give you an idea, what the comment should explain - WHY you are doing so, not WHAT you are doing.

Comments supposed to explain things which aren't obvious from the code. Don't confuse obvious and readable. While your code statements are obvious, some of them aren't readable in a program's context. The main reason of that are - again comments. Instead of explaining things in comments, try to explain them in a code.

For example:

Bad

Better

Also, you tend to write comments in the subsequent line explaining what the past statement does. Much better is to write comment first, and then the corresponding code construct (once again, much better is not to write such comments at all).


In the example above, you have variable numOfDel, however, comment down below says, the number of userinputs. Do you see how code and the comments are out of sync in your source code? It is much more dangerous to have misleading comment than not to have it. However, in this case is good as it revealed where you need to improve.

Give a try renaming your variables appropriately and removing all the comments so we could see whether you understand what the variables represent (or meant to represent), that way I think we could help you better to go back on the track.
 
M. Gumblert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I named my variables so they represesnt themselves. In every tutorial and/or book they use variables like "i" for loops and stuff. Now i renamed them. The code i working, however not how I want it.
I would want that the user can type only two numbers in. Because of this:


However, he asks for the next number and doesnt use it for the deleting process.
Here are the results from the Command Line:

Please enter an array of numbers, type 0 when finished
1
2
3
4
--------------------------
1
2
3
4
Delete please
1
2
3
--------------------------
3
4
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the program's output correct?  If not post the output and add some comments saying what is wrong with it and show the desired output.
 
Liutauras Vilda
Marshal
Posts: 6084
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

M. Gumblert wrote:I named my variables so they represesnt themselves.


Good that you took this exercise as it reveals that some of the parts you misinterpret, which possibly prevents you from coming up with working solution.

1. static ArrayList<Integer> array = new ArrayList<Integer>(); this variable "array" really doesn't represent an array, but a List or more precisely an ArrayList in terms of data structure. However, it doesn't really tell what it actually meant to hold inside. What that might be? numbers? what numbers? lottery numbers? (name it so it tells what that is).

2. int makingArrayInput = reader.nextInt(); in most cases you'll find that variables are best represented as nouns. So maybe really it is a user input? (which in Java you'd call it userInput). The idea behind not to include data structure name to a variable name, because an underlying data structure might change. Today is Array, tomorrow you might decide that better is to have different data structure for user inputs. So you'd need to rename all variable names which reflect that name - simply don't include data structure name to a variable. BAD: userInputForArray, BETTER: userInput.

3. while (makingArrayInput != 0) as you see from this line of code, it reads horribly. If you were change variable to userInput, that would read as: while (userInput != 0), way more expressive, isn't it?

4. for (int ForLoopVariable : array) you were right that for loop indices is a common practice to name variables i; j; k... however, since it is an enhanced-for loop, I think much better is to name element what it actually represents, that's because enhanced for loop iterates over the elements, and not the indices.

standard for loop:

enhanced-for loop:

5. NumOfNumbersIWantedToDelete does it really represent that? Maybe it is deletedNumbers (already deleted)?

6. iWantToDeleteThis = reader.nextInt(); maybe better would be to call numberToDelete?
 
Master Rancher
Posts: 2906
98
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP
with one replier chasing the possible error, and one improving the style of coding, you're in good hands.

I'd like to point out something in your code that might cause problems and is not easy to spot. This is the code snippet I'm referring to: (see your latest code snippet)

Have a look at this code and its output. Can you explain the outcome?
 
Liutauras Vilda
Marshal
Posts: 6084
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good point Piet!!!

@OP
If you can't compile this line of Piet's given code snippet:

Replace it with:

Have fun debugging, it really is
 
Marshal
Posts: 60749
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . If you can't compile this line of Piet's given code snippet: . . .

Isn't that Java9+ only code?
 
Liutauras Vilda
Marshal
Posts: 6084
418
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Liutauras Vilda wrote:. . . If you can't compile this line of Piet's given code snippet: . . .

Isn't that Java9+ only code?


Yep, that's what it is. So OP and possibly other readers would know what is a matter in case they are running prior Java 9 versions.
 
M. Gumblert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Norm Radder
the program output is correct. The thing that annoys me is even when the program doesn't satisfy the criterium that is in the "while" it asks for a number. It doesn't do anything with it. Just asks for it and when you enter it, it ends like nothing ever happened. It deletes the numbers you typed in, but doesn't delete the last one (when the while criteria wasn't satisfied). Why does it ask for one more input?
@Liutauras Vilda
I think I understand what you are saying and I will try to do as you suggested in my future codes. As for the ArrayList and what it represents, even though I am aware of it's meaning I failed naming it. Thank you for highlighting the problems in my style.
@PietSouris
Honestly this type of ArrayList (new ArrayList<>(List.of(0, 0));) is strange for me. I tried to understand what the whole class does, but I am not sure. I can't compile it either.
I don't understand what is in the new ArrayList. What did we "put" into the ArrayList.
Thank you all for your help, if someone have the nerves to explain it to me I would be very glad. Have a nice day!
 
Norm Radder
Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It deletes the numbers you typed in, but doesn't delete the last one (when the while criteria wasn't satisfied). Why does it ask for one more input? 


It's time to play computer with your program using paper and pencil.  Pretend to enter the required numbers one at a time.  Step through the statements one by one and record on the paper the results of each statement.  Doing that will show you where the problem is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!