Suane Mane

Ranch Hand
+ Follow
since Jan 31, 2019
Suane likes ...
IntelliJ IDE MySQL Database Java
Merit badge: grant badges
Biography
Lost in java
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Suane Mane

fred rosenberger wrote:I would suggest you stop writing code.  slow down.  back up a bit.

Programming is really 90% thinking, and 10% typing.  (Some may disagree on the exact percentages <cough><cough><Campbell><cough><cough>).

I try to never write code until i've thought through the problem A LOT.  What do i really want to do?  When do I want to do it?  What is a good way to accomplish my task?  What is the MINIMUM amount of code I can write to add value to what I want to do?

Assuming your ArrayList is populated, start by JUST printing out each value in the list.  Once that's done, try only printing values greater than the minimum.  Once that works, add in the condition to not print the maximum.

If you look at your current code, you have a loop that iterates the same number of times as there are elements in the list, but you don't actually look at the elements in the list.  Then, if there were 30 elements in the list, you'd print out your error messages (potentially) that many times.

Consider writing lots of small methods.  write a method to get input.  Write a method to validate input. Avoid hard-coding values in the middle of your code.  Define a variable like "final int upperLimit = 9", then use that variable in your code - that makes it easier to update later, when your specs change.



That is good advice when trying to actually learn coding and when time is on your side and everything is fine. However, I didn't have the luxury of any of that at the time. Hopefully soon before actually graduating I can properly learn.

Thanks all for the answers. I did manage to figure out assignment after all.
3 years ago
Thanks all. I did manage to figure most of it out in time for submission!
3 years ago

Paul Clapham wrote:

Suane Mane wrote:Also I thought the "else..." part of the code covered the part that would have to be in the try block because that covers all the numbers that can be entered from 0 to 9, which means it can hold 2.5, 4.6, 7.3 etc.



No. A try-block is specifically for catching exceptions. It's the Integer.parseInt() method that will throw an exception, so it's the code that needs to be in the try-block. The rest of the code checking for < 0 and > 9 doesn't need to be in the try-block and the code might be clearer if it wasn't.



If I surround only that part with the try block then the rest of the method becomes unusable:
3 years ago

this creates the empty array list.


this adds values to the empty list.

The full program consists of a window with text fields where the user can enter numbers and click a button to add them to a list. So I used that to create the initial list which is initialised outside of this method earlier in the code.
This method is meant to look through the currently created list and pick out the values that are within the range selected by the user and add them to the new list, then print that list. Example of the steps I did:
Number added
[1]
Number added
[1, 2]
Number added
[1, 2, 3]
Number added
[1, 2, 3, 4]
Number added
[1, 2, 3, 4, 5]
This is the final version of the initial list.
Now I'm using the range method by entering values 2 for min and 4 for max. It should add to the new list the numbers from the initial list that correspond with that range, therefore 2, 3 and 4. However I'm getting this:
[2, 4, 2, 4, 2, 4, 2, 4, 2, 4]
3 years ago
Seen. Wanted to update this topic with another try/catch problem of the same program.



The aim of this method is to show the number of occurrences of user entered number. If the text field is empty, it's supposed to show the number of values currently in the list (only testing on console for now. Once it is fully functional I will try to add the message in the window so it's visible to the user).
I'm pretty sure the code that is raising the exception is within the try block, yet the exception still goes through.
3 years ago
I'm only testing it so far on the console because I will move on to displaying the messages on the frame later. I wanted to make sure all the methods work as intended before adding them to labels or looking into how to best add them to the panels.
Also I thought the "else..." part of the code covered the part that would have to be in the try block because that covers all the numbers that can be entered from 0 to 9, which means it can hold 2.5, 4.6, 7.3 etc.
3 years ago
I'm trying to make this method work:


The aim of this method is to display the values within a specific range of a list. The user gets to enter the min and max values for the range into text fields.
I thought the best way was to store the values found in a new list. But the output of that is:
[2, 4, 2, 4, 2, 4, 2, 4, 2, 4]

I tried it on a list of values [1, 2, 3, 4, 5] and selected min 2 and max 4.
3 years ago
I have another topic about the full code this part is from but it started out with a different question and branched out. Thought it might not be accurate to keep posting there when I'm asking different questions.

I'm trying to catch the NumberFormatException and write my own message and allow the program to continue after it.


Output:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2.45"
3 years ago
Trying to tackle some error handling if a user enters a double instead of int. I used a try/catch but it still stopped the program when the exception occurred. Am I placing it in the wrong place?

3 years ago
Yes, I realised that. Took me so long. I don't really know what I was thinking when I decided to use the 3 variables. It just slipped my mind that I had to read the values from the textfields. So I guess I need none of the variables or arguments now.
3 years ago
I think I figured it out. I swapped the line

to be at the top of main() so that the window shows up first before any request for numbers.
I've further fixed and tested the methods. Add and search work but the range is not working as intended.


Output:
Number added
[2]
Number added
[2, 4]
Number added
[2, 4, 5]
Number added
[2, 4, 5, 2]
Number added
[2, 4, 5, 2, 8]
Number added
[2, 4, 5, 2, 8, 4]


Output:
There is/are 2 occurrence(s) of the number 2
There is/are 1 occurrence(s) of the number 5



Output:
Here is the list of number between 2 and 5:
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
[2, 4, 5, 2, 8, 4]
3 years ago
This is the updated full code (fixed the methods a bit as well):
3 years ago
Thank you! Now however it only pops up the window after I've entered 3 numbers in the console for some reason.
3 years ago

Carey Brown wrote:






Thank you very much! I simplified my code to


Too bad I can't test it until I fix the window showing up problem.
3 years ago

Piet Souris wrote:
a way is to put this part in main(), and give these variables as arguments to your constructor. What I would certainly do is to put some text before usng scanner,nextInt, for instance:

Then the user sees that he/she is supposed to input something.



I looked at this link for help with constructors and based on that and your advice I changed code to:


and the main:


But the variables are not recognised in the methods anymore after this change. What am I doing wrong?
3 years ago