Win a copy of Java by Comparison (eBook) this week in the Java in General forum!

Brian Winkler

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

Recent posts by Brian Winkler

Steve Luke wrote:We aren't being thick here, we just want you to work it out yourself.

So, I guess the question is how do I tell my program that each time it runs through the loop to use a new instance of the String Array?


How do you make an instance of an array? Note, you already do it once in your code. How do you think you could do the same thing: but once for each iteration of the loop?



Sorry, I knew you were trying to get me to help myself, but my head really hurts at this point (not kidding, I have a terrible headache). I did figure out that if I moved the line 18 into the loop it solved the issue. So thank you so much for all the direction.
One more question... And I am pretty sure you can't make me learn this on my own but is this an okay way to be doing this? or is there a better type of collection or some other method I should look into? I figure this can only be answered by experience.

Thanks Again. This site never lets me down.
5 years ago

Henry Wong wrote:

Brian Winkler wrote:
Yes this...
So, how do I do that? I thought once I store the info into the List, I can reuse the String Array to create the next item. Is there a better way to do this instead of String array inside of list array?

Thank you for all your help I have a headache at this point from over thinking this.



You are definitely over-thinking this.... basically, you have a loop that puts the same array object in the collection many many times. So, shouldn't you expect a collection with many many references to the same array?

Henry



Yeah, I think I understand that part, so adding the String Array to the List Array is just a reference. So, I guess the question is how do I tell my program that each time it runs through the loop to use a new instance of the String Array? Or is there a way to add the actual data into the List Array instead of a reference? Or is all of this a really poor way of doing this, and I should be doing this a completely different way?
5 years ago

Steve Luke wrote:

Paul Clapham wrote:I'm not sure that I understand that, so correct me if I'm wrong... Do you mean to say that in line 46, debug[0].toString() and debug[1].toString() and debug[2].toString() all return the same string?


I think what he is saying is that no matter what index he uses on line 45, the only output would be for the input set in the last run of the loop. And this makes sense. You use a single array over and over again, in the loop you are simply changing the values in the single array then putting that array into the list again and again. Since you only have one array, all the indexes in the list point to the same array instance and the data is the same (whatever happens to be put in last).

If you want to store different values you need to use a different array for each iteration through the loop.



Yes this...
So, how do I do that? I thought once I store the info into the List, I can reuse the String Array to create the next item. Is there a better way to do this instead of String array inside of list array?

Thank you for all your help I have a headache at this point from over thinking this.
5 years ago

Paul Clapham wrote:I'm not sure that I understand that, so correct me if I'm wrong... Do you mean to say that in line 46, debug[0].toString() and debug[1].toString() and debug[2].toString() all return the same string?



so, I collect information from an API that gives me three items per player. I collect those in a String Array called nameOreStoneString in locations 0, 1, 2. (lines 32 - 34)
I then take that string array and add it to a list of arrays called namesAndOresList. (line 40)
I then output a count of the total lines added from line 40. (line 43)
Line 45 and 46 are the problem... I want to be able to output a row from the list, except when I do no matter what line I choose, I always get the same output.

Sample output:


My output here should be:


Hope this help to clarify my issue. Thanks for such a quick response.
5 years ago
Hello all...
Please look at the following snippet of code.



In the first debug section everything works as expected, and the output is consecutive strings of the results of the query.
In the second debug statement, the count is correct, but no matter which index I choose, the output is always the last entry that was added. If I put this in a looped output, I will get the total counts worth of the same thing line after line. It's always whatever the value of the last entry was. Can anyone please tell me what I am missing? I have been beating my head on a wall for a few days now, and I just can't see why it's not adding the lines consecutively.
5 years ago

Steve Luke wrote:

Bear Bibeault wrote:Is your IDE perhaps using an older JDK? I know with IntelliJ (the IDE I use) you can select among a number of Java versions.



I believe the problem is reversed - if you compile on an old JDK and run on a new JRE you are ok because the new version knows about the old, it is only the reverse which is bad.

And this seems to be the problem the OP is seeing. The error message says "Unsupported major.minor version 51.0." The class version for Java 7 is 51.0, so it means whatever version of Java the user is trying to run on is prior to version 7.

The options are to either ask the user to install Java 7 on CentOS, or to recompile targeting a previous version of Java - assuming you don't use any of the new Java 7 stuff.



This is kind of what I was thinking too. I did some searching on google, and and couldn't find an exact answer... A.) do I have to install the JDK for say java 6, or is it included in the java 7 JDK? and B.) how in Eclipse do I tell it which version of java to compile with?
5 years ago

Bear Bibeault wrote:At the command prompt, what does each respective machine show for:



on the MBP:
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

on the Mac Mini:
java version "1.7.0_09"
Java(TM) SE Runtime Enviroment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)

I dont have the info on the CentOS box, but I will ask him for it. Also, the Mac Mini is working fine now... Ever since I installed the JDK on it. So, I am not sure what the results of java -version were prior to installing it.
5 years ago

Bear Bibeault wrote:That error almost always means that the class is trying to be executed in a JRE of an earlier version than the JDK that created it. Are the CentOS and Mac Mini running earlier version of Java than your Pro?


Thanks for your reply...

The CentOS I don't know about... The Mac Mini is mine, and only about two weeks old. I installed the JRE on the Mac Mini right after I purchased it, and the Java control panel showed only one instance and it was version 1.7.0_09... same as the JDK I later installed. Is there any chance I am missing anything?
5 years ago

Campbell Ritchie wrote:Is this error when you run the app, or when you load Eclipse?
In case it is an Eclipse‑related question, I shall try (I have never done this before) duplicating the discussion in our IDEs forum, and maybe you will get more attention there.



The .jar is a plugin for a game. the error happens when the game starts. So I guess you would say when you run the app.
5 years ago
Sorry, here you go, hope this helps.

5 years ago
Sorry if this is in the wrong section, but I am guessing it is a beginner mistake on my part.

I wrote a small app today in Eclipse that ran fine on my local machine (Mac Book Pro). I then sent it to a friend who is on CentOS and the app errored out for him. I told him he need to run Java7 as that was how it was developed, but it still would not run. I then installed that app on a Mac Mini of mine, and I was getting the same error. I went to Java.com and made sure to download and install the latest JRE and still got errors. On a whim, I downloaded and installed the JDK, and now the program runs fine. Is there a preference I need to select when creating a .jar or in my properties of the app itself that allows it to run in the JRE?

Just some additional information in case it is relevant, I used fatjar to make the jar file as I had some dependencies i needed to keep.

Thank you in advance for any help.
5 years ago

Michael Dunn wrote:because 'panel' is a null layout it has no
preferred size i.e. a preferred size (0,0)

BorderLayout uses the preferred size to
layout the components, so add
panel.setPreferredSize(new Dimension(400,100));
where 400,100 can be anything you want.
then use
frame.pack();
instead of
frame.setSize();



I love this site. This worked perfectly. Thanks again for your help!
5 years ago

Michael Dunn wrote:panel.setLayout(null);
panel = new JPanel();

reverse the lines



ha, I just figured it out at the same time you answers that. Thank you.

One other question, I am trying to take that panel and insert it into a JFrame with a border layout as the SOUTH panel, but when I run the code, nothing shows. Should I be able to do this? or am I doing something wrong?

5 years ago
I have this test code I am trying with a panel that uses absolute positioning, but when I run the code, it doesn't like the setLayout(null) line. Can one of you please look at it and let me know what I have wrong? Thanks.

5 years ago

fred rosenberger wrote:You don't need to pull the dimensions from the RoomDimension object. To calculate the cost, you don't care if it's 10x12 or if it's 120x1. All you want is the area of the room. So, you can call the RoomDimension's getArea() method, and multiply that by the cost.



the way it was assigned, was like this...

RoomCarpet
- size : RoomDimension
- carperCost : double
+ RoomCarpet(dim: RoomDimension, cost : double)
+ getTotalCost() : double
+ toString() : String
<>
|
|
RoomDimension
- length : double
- width : double
+ RoomDimension(len : double, w : double)
+ getArea() : double
+ toString() : String


So according to the diagram, I think I do have to call the two doubles from RoomDimension. Or am I understanding this incorrectly? I am sorry, this is all very new to me.

Thanks for your help.


5 years ago