This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Jay Rex

Rancher
+ Follow
since Oct 25, 2018
Jay likes ...
Java Windows
Cows and Likes
Cows
Total received
7
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jay Rex

Paul Clapham wrote:You're making the assumption that Outlook starts by loading the entire .PST file into memory, for a start. At least that was the context of your original question. And yes, Outlook would be completely unusable if it did that. So I believe it doesn't do that.



This thread has not offered any alternative way of thinking about how real applications load and save a single file, so that does lead to that assumption, so I challenged that with my real world example and your response was "So I believe it doesn't do that". Hence, write, delete, rename seems dubious as the way in which some applications work with a single file.

For my trivial programming task, write, delete, rename makes sense as the file is tiny, but that is not what I encounter as a user on a daily basis with real applications. My question comes from the perspective of trying to understand how real world applications deal with this problem. Hence, my three real world questions at the end of my initial post.
2 years ago

Gerard Gauthier wrote:I ask you to consider an ASCII text editor which opens an ASCII text file. Now you modify the contents of that file in the ASCII text editor. How do you update the persistent memory(we'll say hard drive) that contains the original? Can you put forth a scheme that'll work without creating a new file?



I can't, but that doesn't mean there isn't one. Hence my question

As a novice programmer, I find this hard to believe. I use Outlook all day at work. My current .PST file is 6GB, but I work on a PC where the .PST file is 20GB. Am I to believe that Outlook rewrites the 20GB file every time a new email arrives or every time I create a  task, or save a draft? Outlook would be completely unusable if that was the case.
2 years ago
I have a text file that has content which I read using a Scanner, line by line, adding each line to a List.

The content is modified by user interaction via a console application and saved.

A tutorial showed me that I need to do the following to save the file:

1. Write the content to a new file.
2. Delete the old file.
3. Rename the new file to the name of the old file.

My questions are:

1. When I open a file in Notepad and change some content inside an existing file, does the above procedure happen each time I press Save?
2. When I open an Excel spreadsheet, does the above procedure happen each time I press Save?
3. Is this the general way in which files are written by applications (Photoshop, Illustrator, etc.), which is invisible to the user?
2 years ago
Campbell:

What is fileDelimiter?




Products.txt


Inventory.txt


That technique of creating objects is error-prone


How would I do that in a better way?

I see you are up to date with try-with-resources


Thanks to you

Remember how important the documentation comments are.


The more I read about comments, the more I see that people advising against writing comments. It is usually advised to only write a single line comment if the method name would be too verbose to describe the intent. Are documentation comments seen in a different light?

That means you would consider making it a static method.


Where do I create the static method, inside Test.java or Product.java? Is one more correct than the other?


Winston:

What is Inventory? What does it do?


I was advised against storing quantity inside Product, which makes perfect sense, so I created an Inventory class that only stores the item code and the quantity.

What is Product.ID? It sounds like the way a database might distinguish between Products, but isn't strictly necessary for a Java Product class.


It's simply a unique product identifier.


Tim Holloway:
Thank you for your recollection. Very interesting. My coding style seems very procedure-oriented currently. I want to see how I can leverage the object based strengths of Java.


Liutauras Vilda:
Thank you for your post. That makes much sense.


Here is the outline of my console program currently:

Product.java
Inventory.java
Products.txt
Inventory.txt
Test.java: Contains all the methods required to make the program work.

The program does the following:

Products: List, Add, Edit, Delete
Inventory: List, Add, Edit, Delete
Read the Products from Products.txt and writes to a new copy of Products.txt after Add, Edit and Delete actions
Read the Inventory from Inventory.txt and writes to a new copy of Inventory.txt after Add, Edit and Delete actions

What would be a better name for Test.java?
2 years ago
I have a project that works using procedural code. I want to convert the code into a project that uses OOP to achieve the same result.

Currently I have:

A Product class with no members:



An Inventory class with no members:


and a class with no proper name, called Test, where all the procedural code is housed that works on the Product and Inventory objects.

Is the following the correct way of thinking about moving from procedural to OOP:

Currently, I have this method in Test.java:



I would move that to Product.java, with line 13 removed. What would I return:



Would this be the correct way of calling it from Test.java:



How do I implement line 13 in an OOP manner:


How would I make it work?
2 years ago
Thanks Campbell and Junilu.

Note to self: Use the search function before posting, as this has been covered already
2 years ago
I have two methods that I want to overload:





The compiler error I encounter:



If I rename the one method, the code compiles, but I want to know why this is considered a name clash.

Why is the code above not an example of method overloading where the data type of the parameters is different?

How do I fix this, so that I can overload these methods?
2 years ago
After posting that message, the following Wiki article was suggested, which is exactly what I was looking for:

https://coderanch.com/wiki/659777/User-Input
2 years ago
I'm writing my first console project that I would like to present as a small real project. By that I mean, it reads data from a file, a user can add or edit content and save the changes back to a file.

What is becoming concerning is that the code is littered with try catch blocks and if else blocks in an attempt to deal with poor user input.

If I ignore poor user input, the code reads much cleaner and is significantly shorter.

Naturally, if I do this, the program crashes each time the user:

1. types a letter instead of a number
2. types a number outside of the bounds of the menu options
3. presses enter instead of typing any input.

What strategies are there to deal with this, so that the intent of the program remains clear, while dealing with poor user input.
2 years ago
Human readable lists start at 1.

Java indices start at zero.

Hence the following:

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors. -- Leon Bambrick



I want to avoid introducing a magic number into my code, namely (- 1) which takes into account moving between the human based and the java based indices.

Currently I am using:


Is there a commonly defined succinct term that I can use instead of the above?
3 years ago

Liutauras Vilda wrote:Menu.value(userInput).menuOption();


Thank you, I didn't understand what Dave meant.

Liutauras Vilda wrote:In general, I find your code hard to read. First and main reason is poor formatting (blocks of code crammed to one line).


I read the link in Carey's signature where it spoke about cramming, but Oracle themselves do it so that is why I have done the same.
See: https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9
As we read from left to right, I thought it makes sense to include a block on one line if there is only one statement inside the block. Is that not correct? If not, why would Oracle format their code in a way that is discouraged?

Liutauras Vilda wrote:Another question: why index is String? At least in my head, when I think about index, I have in my mind a number, probably a whole number.


The rationale is that the input is already a String, so just keep it as that.

Liutauras Vilda wrote:I also would have an enum in a separate source file instead of having it in a Test class (even if that's just for test). Otherwise makes it harder to follow the code as really Test class and Menu enum aren't related in any sense.


This is a small project, so almost all the code will be in one file. I will remember that for larger projects.

The reason the class is called Test, is simply my workflow. If I want to convey a problem I am experiencing, I open a file called Test.java and start typing. This seems to be in line with SSCCE, which I read about in Carey's signature.

Junilu Lacar wrote:Using reflection to achieve what you said is technically possible but in my opinion, that's not the real problem.


Until this thread I didn't know Reflection existed, so I won't be using it

Junilu Lacar wrote:If you really want to get rid of the long switch statement, which for a simple program that has a limited lifetime isn't that bad, then I would try something like what I show below. This to me is a more obvious design:


Thank you for showing me that code. It has shown me the further reading I need to do before I understand what you are doing

salvin francis wrote:routingTable can be a map with key as Integer


As above
3 years ago

Dave Tolls wrote:You don't need that second loop, as you already have code in the enum that returns a Menu based on the given index.
You are essentially duplicating code at the moment.


The first loop prints the entire menu.
The second loop calls the appropriate menu method based on user input.

I don't see how I can remove the second loop, as I need to ask for the user input after printing the menu.
3 years ago
The code above was incorrect. Here is the corrected code, which works:

On lines 50 and 52, input should have been userInput.



Thank you for all the help. The enum constant specific class body has replaced a 40 line switch statement with a single for and if loop.

Please advise any improvements to the code above.
3 years ago
Thank you for the replies since my last post, but the suggestions are above my current level of understanding.

I found the following enum syntax inside Head First Java, which seems to be what I need:


I would like to use the format of the code example above, if possible.

Here is my code. It compiles, but it doesn't print the output inside menu1(), nor menu2():


It doesn't seem to matter if I change the access level to public or private, I achieve the same result, which is that the code compiles but never displays menu1(), nor menu2()

Please advise how to correct the above code.
3 years ago

Campbell Ritchie wrote:Do you really mean class methods? Remember that class methods are the opposite of instance methods.


It seems I may not be using the correct terminology. I think I am referring to instance methods.

I am referring to the following types of methods, when I said class methods:


So I currently have the following basic enum inside the class:



Do I understand you correctly that I could call option1() from inside the enum?

Your solution seems ideal, but I would need to keep the index and the menu text and the executeOption method. How would I do that?
3 years ago