This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

C Robinson

+ Follow
since Jan 05, 2019
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by C Robinson

Thanks again for the awesome support!

The reason is that the private modifier means that only code inside the same top-level class' body is allowed to access the member. The value field is a member of the Currency enum, which is declared inside the D3 top-level class. That means that EVERY piece of code inside the D3 class' body can access it directly. The main() method is inside the D3 class, so it can access the Currency.value field directly.

Another way of thinking about this is that private means "private to the code in this source file", but only if you have no more than one top-level class in your source file.

So, if I had created a separate class and main method, the value.value would not have worked?

That makes sense. My previous Professor had us writing classes with their own methods separately, but I moved on to the second class now.

It's likely because professors and authors don't want to distract from the lesson they're trying to teach by introducing new types.

This is also correct as I have overcomplicated some of the code I've written already and this Professor is reeling me back in and keeping me focused on using and understanding how the basics work.

The instructor probably knows it's not a good idea but reasons that "They'll figure this out later, I just want to show them the motion and how the mechanics of the arm works." To me, this is totally wrong.

In this circumstance, I think it may be more the format of the class. It being online, there's really only so much that can be lectured on and the rest should be reading. This is the pain of online classes. This course is lighter on the course load and reading, which I am thankful for, but a lack of grasp of some of the basics may be the impact of that.

I have the textbook, but the order in which the class is teaching subjects is out of order with the textbook, so you can't jump to Chapter 19 without reading Chapters 9-18 as a subject matter like this builds on itself. I'm trying to read the book in order anyway to make sure I dont leave anything behind.

Thanks again for the help. I tell my fellow students that this is a great resource for support.

1 year ago

Thanks again for the support!

I appreciate the explanation Stephan. (And that you managed to right size it to my minimal level of experience; I know that's tough for folks to do sometimes when they have as much experience and knowledge as you all do)

For classes/methods that are final, it really doesn't matter that much. Note also that if you want to access the property from another class, you have no choice but to call the method, because fields should be private.  

My Professor seemed surprised that calling the value would be from the getValue() method would work because, as you say, the "value field is private" He did not think the commented out section of code should increment the "total" variable. Taking a stab at why it does, either:

A) The original Currency enum Object that is created is instantiated with a "dollar-value" when it is instantiated through the constructor. As the reference to it's memory address is passed into the array, wallet Vector in main, wallet Vector in the currencyPrint() method, Object "o" in the for:each loop, and ultimately into the Currency object "value" created in the currencyPrint() method: the "dollar-value" initially assigned to the memory address is still associated with it, so:

this simply references a field that is already associated with that object? But as you say, the field is private, so how would the piece of code above be able to use it to increment the total variable?

If you meant to ask if there was a better way to access the value property of a Currency object when you have a reference to it of type Object, then the only thing you CAN do it cast it. But as Campbell has pointed out, you want to avoid having to cast, and use generic type parameters in the way they were intended.

This is exactly what I meant, and I followed what Campbell was saying, however, with these assignments, whether it is generally accepted convention or not, I believe they are teaching the material in a certain way so we can grasp the concept with the easiest process, correct or not, and then they expand on it later. I find this a lot when new learners ask questions online; a lot of times we get lost in answers of "don't do it that way, this is a better way" and that way is not something we have been taught. It's 100% the correct answer by all accounts, but to a different question.

But going by the original code, the entire purpose of this exercise is to mix objects of different types and then cast them back at a later time. Realize though that in the real world, you will want to avoid code that like.

This expresses an understanding of that. Keep in mind, you all can say "Why would they teach them to do that that way? This is a much better way to do it" while from this end, I'm trying to figure out why the things that they are teaching us work at all.

In object oriented programming, you want to create a class that actually represents a wallet and to which you add instance methods.

This would have been my first approach, but this likely wasn't a requirement to keep learning the concepts less complicated. This is an online class, so if we get lost in the weeds, there is a lot of exchanging emails to try to sort it out?

You might notice that I used int to represent the value of a denomination, and I used BigDecimal to print it. That is because you should NEVER use floating point types such as float and double to represent exact values, such as monetary amounts. When you use floating point types, all arithmetic operations may cause rounding errors. With BigDecimal, you can move rounding operations to the end of the calculation and explicitly specify how the rounding is done

This seems pretty straightforward, I'm surprised we had not learned this yet as I am in the second Java course.

Thanks again everyone for all the support and advice!
1 year ago

Thanks for the link and the advice!

As far as:

-Using ArrayList instead
-Not using instanceOf multiple times
-Type of data or whether it fits into an enum
-Using List<E> in the method headings

We did not get to set these parameters for the assignment, these were requirements.

As far as there being "something wrong with the design of code that uses instanceOf multiple times", I'm obviously new at this and wouldn't know what that something is. I'm posting here to learn things, so understanding what exactly is wrong and why is key.

I posted in the Beginners forum as I thought this would be the most appropriate place to get an answer and explanation to the question I asked regarding my specific problem and some advice to help me learn.

You all delivered on the latter, and I appreciate the constructive criticism.

Still hoping for a direct explanation to the former.

Thanks again!

1 year ago

I am writing a for class that uses Enumerations to create 4 types of Objects, randomly select a random number of them for a "wallet" Vector, display the contents, shuffle them, display again, and then use exactly 4 methods to print each enum type separately using instanceOf. The "Paper Currency" output should calculate the total value of the Currency as well. I've completed the assignment and it worked, but without calling the getValue() method. I've corrected that, but my inquiry surrounds understanding why it worked the other way.

I cannot use methods outside of the ones used here, however, I can (probably) be more efficient in my approach.

That said, the question I have is in relation to the calculation of the Paper Currency value. I initially tried to call the getValue() method on Object "o" in the currencyPrint() method. This is now commented out. It worked, but I received indication from my Professor that this wasn't correct as I was not using the getValue() method.

Currency enum and getValue() method

currencyPrint() method

I would ask:

1) Why need the getValue() method if my first approach worked?

2) Was there a better way to achieve what I have other than moving the reference BACK to a Currency Object? (I tried casting Object "o" and changing the parameters of getValue() and it did not work)

3) General observations about approach, etc. I know I have no comments (yet).

1 year ago

I  have a program I am writing to compare an external file list of names to a user entered name.

While I am typing in a name from the list, exact to case, it is still returning a false boolean value on the match,

I even created System output lines to confirm the values match and yet I cannot get it to return true.

Ive tried equals. and equals.IgnoreCase() as well and while the case is identical on the Strings, I cannot get it to return TRUE.

I understand there may be other ways to approach this program, I have to try to use the processes we have learned in our course (which I have mentioned here) if I can, even if there is a better way of doing it. I'm assuming I will learn that in the next course.

I'm just trying to understand what I am not understanding properly.

Any help is appreciated


Demo Program:

1 year ago
So, I have to write a program for a course and I've been beating my head against the keyboard trying to figure out what I'm doing wrong. I apologize in advance for the lengthy post, but I'm sure this is something simple and it's time ot call in some Pro-Level help as, with Finals week approaching, I cannot have one bug taking me down for more than the 3 hours it already has.

The program is: create a class that creates lottery drawing objects with methods that will check user number picks against a randomly generated 5 digit "drawing" and report back how many numbers are a match.

Simple enough, right? Somehow I've made a mess of it.

Here is my class: (I will note, I primarily use JOPtionPane for user interaction as I just like the interface better, so anywhere you see "System.out.print" its my effort to see whats going on and debug. Disregard this.(I'm leaving it in case you can explain what I'm seeing with that output because I cannot figure it out)

And here is my Demo Program:

The errors I am currently fighting with:
1) Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1
at RobinsonC_Lottery.matchedNumbers(
at RobinsonC_LotteryDemo.main(

This one has me stumped. I read through my matchedNumbers() method out loud in the class and it sounds like this:

"FOR instances of this loop where int variable index that has been initialized to zero are evaluated to be less than the length of the lotteryDrawing array:
IF LotteryDrawing array's element value at subscript position matching index2's value exactly matches playerPicks arrays element value at the same subscript position:
increment "matches" variable 1X. Increment index2 - new iteration of For Loop. (repeat)

The intent here is for the index 2 variable to increment concurrently for both arrays, stepping through the arrays concurrently and if theres a match, then increment matches.

Then, "matches" will be used to set the size of the matchedNumbers array, then another for loop should record the matching numbers in that array for return to Main Method.

I know an ArrayList will automatically expand but that was not listed as a part of the assignment so I can't use that. I've been docked before for including things we haven't been taught and/or the assignment doesnt ask for. I need to use the practices here properly to demonstrate an understanding of the basics and after 3 hours of grinding on this, I'm stumped.

I dropped all these System output lines in to see whats going on and I cannot figure out why index seems to be incrementing more times for lotteryDrawing than playerPicks.

Here's the System Output I get.

Constructor index value-1
Constructor index value-2
Constructor index value-3
Constructor index value-4
Constructor index value-5
Before FOR - lotterydraw length - 5
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
6--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
4--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
8--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
0-matchedNumbers length
Before FOR - lotterydraw length - 5
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
6--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
4--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
8--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
0-matchedNumbers length
Before FOR - lotterydraw length - 5
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
6--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
4--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
8--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
0-matchedNumbers length

2) When it matches ANY of the numbers it reports a grand prize winner - Looking at my Demo program, I feel like I pretty clearly outline the else clause in the if statement that controls this so I believe problem 1) is whats contributing to problem 2)

You might ask why I use standard FOR loops and not enhanced ones, and  this may be contributing to my issue: The answer is because I sort of understand the enhanced for, but I'm not really getting where I would use the new int variable in these circumstances where I am comparing values to each other, and the chapter in the text explained B before A, so I haven't been able to break that down.

If anyone can help me clear the roadblock I would appreciate it. As always, any otherwise critiques are also welcome but please list separately from suggestions on this solution because I copy and paste them into a separate file of "Get better at this stuff" lol.

As far as comments, I get graded on how concise they are and complete so I cant just jot down kinda what I think it's doing because I'll  just have to go back later and retype them all - that drives this bad habit of not having any at rough draft.


EDIT: I figured this out after walking away for a while:

1) each reference to the method in the main program (for length or for toString the return array) executed the method, incrementing the "matches" variable in the method.

2) This was an issue:

First off, the index variables throughout are local to their methods and wont impact each other. I thought they were, because I was watching the weird value change activity due to number 1) above. Then, having the matchedNumbers index match that of 2 much longer arrays will not work.

I couldn't figure out how to remove the post, so at least if it's going to stay, it will have a solution.
1 year ago

Good morning!

I am working on a project that involves creating a game of Rock, Paper, Scissors against a computer opponent using the main method as a "traffic cop", directing traffic to other methods.

I am working on my user selection method and it will be an int value returning method based on String input from the user.

I figured to validate user input and bar invalid text I could use a while loop as a gate to the next part of the method (at least, I've done this in other assignments and the Prof. has not indicated this is incorrect)

My issue is my equalsIgnoreCase statement is throwing an error in Eclipse as I am trying to compare the user input string to "rock", "paper", or "scissors", as so:

I tried separating the string literals with or's ("||") as well and it eliminated the method error, but moved it to the literals, stating the first two were invalid.

Figured I'd "phone a friend" on this one because google searches seemed to only teach me about the difference between equals, equalsIgnoreCase, and compareTo; and maybe I missed it, but nothing references multiple literals.

Complete code for the method below, thanks for the assist and, as always, any critique/advice! (Pardon the no commenting, I do this last (I know, I know; bad habit))

1 year ago

Liutauras Vilda wrote:At line 19 you got semicolon at the end of loop declaration, so that finishes statement and "month" variable goes out of scope, remove that one as that is a mistake to do.


I knew this rule, just didn't see it, probably because of the excessive commenting, as you say.
1 year ago

Liutauras Vilda wrote:C Robinson, wanted to point out one thing, the way you described your problem and explained what issues you ran into - it was exemplary.

Being able to describe clearly a scenario which you attempted, what have you tried already, what went wrong, and what did you expect actually is a good indication that you are heading correct way as a programmer - that's an important skill.

Thank you very much for the encouragement!!

Liutauras Vilda wrote:Actually one more thing. Please also pay the same attention to the comments you write in your code. As they are - they are pretty useless, because they don't tell anything useful or anything what we couldn't acquire from the code already. And I think if you were not have those written, you might would have spot that unnecessary semi-colon yourself, because your eyes wouldn't be hooked by the comment which is in the following line.

I appreciate the insight and feedback too. Is the concern more that there are too many overall useless comments or that I am not including comments that would be needed? I am very early in my development, and my class requires you comment everything, ad nauseam, or risk losing points. While I may not include them in my own programs, only where I anticipate some explanation is needed, I have a 98 in the class at mid-term, so I'm going to toe the line.

As always, I learn a lot every time I come here, it's really awesome of this community to be so available to support each other and beginners like me!
1 year ago

Good Morning!

I am writing a program for a class that requests we use nested loops to request user input and compute the average rainfall over a user submitted number of years.

I am not looking for assistance with completing the assignment, more I have ran into an error that I cannot figure out how to resolve.

This is what I have so far:

The error I am getting is that month in the last output statement cannot be resolved to a variable, even though it's initialized in the for statement?

Just to test, I attempted to initialize the variable at the beginning of the code and as I expected, it told me I had a duplicate variable.

I though my issue was that the compiler cannot confirm the statements in the for loop will be executed, so it cannot assume the variable will be initialized (for example: if years is NOT greater than or equal to months - boolean in for) so I added an if statement that loops to require a result that will have that for statement boolean to initially be true. This did not resolve the issue, which I anticipated as I figured that was not explicit enough for the compiler.

It's weird because we also have an example program in the textbook to model after and it's written pretty much the same way without mentioning an issue with the variable created in the for.

I googled and found indirectly related posts that really did not clarify, so here I am.

Thanks in advance,
1 year ago

Is there a way to turn off the module prompt on Eclipse?

I clicked through the tabs and preferences, but could not find an explicit option to not request this, yet I see videos of people working with Eclipse where they are not prompted for it.

I've figured out the work-around to click "Don't Create", however, I'm sure some of my classmates could benefit from the info?

1 year ago

Thanks for the reply!

First, the video link was very helpful in that I was able to see that Eclipse is not asking the presenter (and maybe you) to create a module with every new Java project, so maybe this is an option I need to click off.

Here'e what I get when I click on new Java Project and then type in the title of my Project in the first box and click "Finish":

I found a work-around: I just click "Don't Create" and it doesn't create the "Module" and I can create a new class and write my Hello World program as normal.

Still, I'd like to know what the module is and it's purpose. I found this article, but it's a little high level for me at this point:

Also, in the tutorial that was posted, the presenter creates a "package" to his personal website, which I don't have. When I create a new project I just create a new class and go? I'm assuming that's ok......

Thanks again for the help!
1 year ago

Good evening,

My current programming class at my university just started, we are learning Java. The professor has instructed us to download and install Eclipse for writing our programs.

I have written multiple Java programs in Notepad++ and Notepad and compiled and ran them in the Cmd prompt with minimal issue.

I am having an issue creating the same projects in Eclipse.

When I open the program, it loads to a main menu where I click on "Create New Java Project"
It then asks me to title the project and my professor wants every project titled "LastnameFirstIntial_projectname"
So, I do that for the real basic "Hello World" program we have to write.

Eclipse throws an error that says "modules shouldn't start with capital letters" or something to that effect.
I tell Eclipse to create it anyway, and it does. It loads some code in a screen like this:

I click on File, New, Java Project and type in my Hello World program code.

I click run and I get: "Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: C:\Users\crobi\eclipse-workspace\RobinsonC_ModuleTestIssue\bin
Caused by: java.lang.module.InvalidModuleDescriptorException: RobinsonC_ModuleTestIssue.class found in top-level directory (unnamed package not allowed in module)"

I've heard that when learning using an IDE is not preferable, but I have to submit assignments with this program in this class.

I googled the error and couldn't really make heads or tails of it. You all have been super responsive and I've learned things I didnt even know I didnt know every time I've asked a question, so I figured I'd give this a shot.

I would ask the professor, but it's a weekend and he likely won't answer.

Thanks for the help!
1 year ago

I'm currently working through Chapter 3 in "Head First Java" which is about variables.

I read the section on numeric primitive variable types and wanted to try a test to see what the compiler returned if I tried to "trick" it into performing multiplication and returning a product as a short that does not fit into a short.

I wrote the following:

Which the compiler returned as: "error: incompatible types: possible lossy conversion from int to short"

Which was a result! However, reviewing my code, I realized I had mistakenly typed "x + y" rather than "x * y", so now I was confused. 7 + 4858 = 4865 so it SHOULD return a value for that.

My mistake in trying to satisfy my curiosity only broadened my curiosity.....

So I adjusted "x + y" to "x * y" and the compiler returned the same error. Which I figured.

Next, I tried changing variable "z" to an "int" variable type and it returned my expected answer "34006".

Now I wanted to see if I changed the operator ( * ) of the equation back to addition, and the compiler completed the equation, identifying the sum as a number that could be considered a short, would it identify it as such. So, I adjusted my code to:

And I got the familiar "error: incompatible types: possible lossy conversion from int to short".

My question is: The compiler seems to be just identifying that the result of my equation is labeled as an int type and returning an error based on the possibility that that result COULD be a number (even though it's had the opportunity to determine it's not) that wont be able to be identified as a short.

I've played around with Python and Javascript (not very much) prior to settling on learning Java first because my university teaches this language first. If I remember properly, the behavior of Python and JS are different in that, in this circumstance, they would perform this calculation, determine that the int variable CAN be reassigned to a short variable, and run the equation where the Java compiler seems to be more stubborn? and if it has the potential to not fit in the declared variable type, even if it does fit in that variable type, it will only return an error.

Restated, it seems the Java compiler is not performing the processes while compiling. It would seem it's just checking for potential problems rather than performing the process and determining if there actually is a problem?

Any perspectives, opinions, observations are welcome; especially where I have mis-termed something as I am trying to get all pertinent language down (the CS Dept at my school just simply will not answer an email if something is not termed correctly)

Thanks for the help.
1 year ago

That is also a great tip that I have not learned yet.

Thanks for the reply!
1 year ago