Junilu Lacar

Sheriff
+ Follow
since Feb 26, 2001
Junilu likes ...
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
Merit badge: grant badges
Forum Moderator
Junilu Lacar currently moderates these forums:
Cows and Likes
Cows
Total received
300
In last 30 days
0
Total given
146
Likes
Total received
3004
Received in last 30 days
8
Total given
687
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt Green check
expand Ranch Hand Scavenger Hunt Green check
expand Greenhorn Scavenger Hunt

Recent posts by Junilu Lacar

The output cannot be determined implies there will eventually be output, except it can't be determined ahead of time. If the program hangs indefinitely, there will no output. So D and F are not quite the same.
You could encapsulate application settings in a class, say AppConfiguration. Then you'd go to that class/object to access all your settings. If you were using a dependency injection framework like Spring, you could abstract that away even further.
5 hours ago

Campbell Ritchie wrote:

. . . it's private to whatever particular Button object has been created. . . .

No, it is accessible directly to every instance of Button.


You're quite right that a private instance variable is accessible by any other instance of that class. I should have said something like "the isPressed instance variable is not accessibly by anything other than Button instances" instead. Kind of proves my point about a little clarity not hurting though.
11 hours ago
I know these are just semantics but a little clarity never hurts:

1. The isPressed variable is not global. "Global" means everything can get to it. It's private in whatever class it's declared in and since it's not static, then it's private to whatever particular Button object has been created.

2. The pressed() method is probably better named as just press(), which conveys an imperative "do something" rather than "something was done". Semantically, pressed and isPressed convey the same idea, which I don't think is the intent. For example, if you want to clear a list of all elements, you call List.clear(), not List.cleared().

Names and the ideas they convey are extremely important in programs so it's good to choose a name that conveys an idea precisely, to avoid ambiguity.

14 hours ago

Cris Marinescu wrote:Thank you for hints. I solved it!


Cool. Scroll up this thread and find the solution I came up with that uses the TreeSet methods I was alluding to: lower() and last().
18 hours ago
Welcome to the Ranch!

Optimization means to find a way to make the most efficient use of available resources. By "resources" we usually mean memory or processing power and when it comes to this, you want to use as little of it as possible.

Interpretation is something entirely different. It means figuring out what something means. For a computer, interpreting code is called "parsing" and is the process of turning text, which we programmers write, into executable instructions that the computer can then follow to accomplish some task.

The executable instructions that are produced from parsing the code can sometimes be optimized. That is, there are known shortcuts that can be applied instead of the longer or less efficient form produced by the interpreter. One example might be inlining a method call. If the optimizer sees that a certain small chunk of code can be copied and executed in place instead of making a more expensive jump to a subroutine, it will make the copy on the fly and do away with the method call. This can improve performance if the subroutine is called many times in succession, like a loop.

My advice to programmers is to write clean/clear code first and extract small tasks to private methods. Small private methods have a better chance of getting inlined anyway, so you get the benefit of clear readable code that is later optimized for performance on the fly.


If the code in the doSomethingWith() method is small and isolated, meaning it deals only with the parameter or any local variables, it will probably get inlined as though you had written all that code on line 3.
18 hours ago
Another tip: you don't have to declare a variable for everything. You only need the string to be turned into an array of characters and use it one time. That is, instead of assigning the array of characters to a variable that you subsequently iterate over, you can use it directly, like so:


You can use the enhanced for loop for this because you're going to process all the elements and you don't need an index to add values to the set.
19 hours ago
Hint: The Java API Documentation is worth looking over to see if there are any methods that make it easy to do something. There are at least a couple of methods there that make returning the second largest digit fairly straightforward.
19 hours ago
Create a Treeset
Iterate over the characters in the string
if the character is a digit, add its numeric value to the Treeset

if there are at least two values in the set, return the greatest value in the treeset that is lower than the largest value, otherwise return -1.

Can you turn that description of the algorithm into code?
19 hours ago

Ray Gilbert wrote:Tried chatGPT and I feel a lot better. Apparently, it is as stupid as I am, It can't solve it either. Lol


This won't make you feel better but ChatGPT is just a program. It only gives as good as it gets, so it's kind of like talking to a mirror.

Corey seems to have found a way to prompt it to give back some pretty decent code. He even showed that ChatGPT can even "understand" how to refactor code. https://coderanch.com/t/769972/java/ChatGPT-Java
21 hours ago
Another option for designing the API is to throw a RuntimeException when the list is empty and dequeue is called.

The standard Deque has methods that will throw a NoSuchElementException if you try to call certain methods when the deque is empty.
22 hours ago
One of the best ways to make your post easier to read is to put code tags around any code examples you include in your posts.

You can do this by putting [code] at the beginning of the block of code and a matching [/code] tag at the end of the block.

For example, if you did this in the message editor

It would get posted as this:

Hi! I just started learning Java. Is this the right way to display a message?

An even easier way to do that is by using the "Code" button that's in the row of formatting buttons above the post editor. Simply select all of the text you want to be formatted as code and then click the "Code" button. That will add the appropriate code tags around the selected text.

Syntax Highlighting Support
By default, the code tag will use Java syntax highlighting rules but highlighting for other languages like Python, XML, and SQL is also supported. The easiest way to change the language rules used for syntax highlighting is to select the language from the dropdown list (it shows "Java" by default) before you click the Code button. If you already know what the right value is for the language you want, you can also add that value manually in the code start tag. For Java, it would be code=java, for Python, it would be code=python, code=xml for XML, and code=sql for SQL. Check out the dropdown list to the left of the "Code" button in the message editor window to see all the languages supported.

Pro Tip: Don't try to use other formatting tags inside code tags
Only write code inside the code tags. Anything else in the tags makes it difficult for people to help you. Don't add any formatting tags; if the code contains [b]...[/b] tags or [color=#x003f00]...[/color] tags, people replying to your question cannot copy'n'paste the code to execute it for themselves.

Pro Tip: Change the starting line number to match your actual code
By default, the code tag will also show line numbers on the left margin of the code listing, starting at line #1. This makes it easy to reference a particular line when discussing your code. For example, people can say "Look at line 5 in the code you posted."

Sometimes it helps to match the line numbers in the code you're posting to the actual line numbers that you have in your program editor. This makes it easy to talk about any error or exception messages that include line numbers. For example, if the stack trace tells you that there's an exception on line 42 of your code, you might want to post just lines 40-44 to give people an idea of what's going on around that particular line without posting all 50 lines of code you've written. However, since the code tag starts line numbers at Line 1, it makes it harder to relate the code you post to information in the exception message. It will be helpful if the line numbers in the code you posted also matched so instead of starting from 1, the line numbers would start from 40.

To do this, just add a firstline option to the code start tag, like so: [code=java:firstline[40]]. In the editor, it would look something like this:

and this is what it would look like when you post it:

I got an error message about a NullPointerException on line 42. This is what I have written:

Advanced options and examples

The RanchGuide contains more examples for using other advanced options (e.g. using a different language than Java) as well.

It's about being nice -- when you're nice to people, they'll be nice to you

Formatting your code helps spare people who read your post from wasting valuable brain cycles on deciphering it - they can use them instead to formulate the answer to your question! Taking the time to ensure that your code is presented properly shows that you respect everyone's time enough to ShowSomeEffort when posting your question. And surely it goes without saying that you will post correctly‑formatted code. Except, maybe, if you think your own poor formatting is hiding some other error.

If you forget to do this when posting some code, you can edit your post for a short period afterwards by clicking on the Edit icon at its right top.

NOTE: Please keep lines inside code blocks under 80 characters. Firstly, it's easier to read; secondly, excessively long lines tend to mess up the windowing, making threads hard to read - particularly if you don't have very high screen resolutions.

Tip: (and this applies to any post) Before you submit, always use the 'Preview' button to check your post. If you see an active scrollbar at the bottom of any of your code blocks; it means that it contains lines that are definitely too long, but 80 characters is even better.

Finally, note that most people won't even try to read your code if you post much more than about 15 to 20 lines. Before you post your entire program and say "what's wrong with this?", try to isolate the portion where the problem lies and just post that.

When possible, post an SSCCE (Short Self-Contained Correct Example).

Also take a look on our other tips on HowToAskQuestionsOnJavaRanch.
22 hours ago
Welcome to the Ranch!

The forum software allows you to format code. You can even start at a specific line number if you use the firstline option in the code start tag.

Check out our wiki page that tells you all about how to use code tags to format code.
22 hours ago
Line 9 is not allowed. Java is a strictly typed language and when you declare the variable list on line 6 and assign a new ArrayList<String>() to it, Java now knows that the list should only allow String values to be added to it. On line 9, you try to add an int value, which Java can detect and say "No, you can't do that."

All this happens at compile time when Java is trying to generate bytecode, hence it's a compile-time error. A compile-time error happens when you write something that the Java compiler can determine is against the rules of the language. Adding an int value to a List<String> is against the rules of the language.
22 hours ago
Maybe this belongs in MD instead of PD.

The guy is doing a card trick. He's shuffling the deck and then quickly and nonchalantly goes through these steps:
1. Memorizes the mixed deck in small chunks, like memorizing 4 or 5 phone numbers
2. Shuffle #1 - to get the Aces in order
3. Shuffle #2 - to organize by suit, without disturbing the Aces
4. Shuffle #3 - to arrange the cards in the right order, but then he cuts the cards a few times after the shuffle.