1,105,380 Community Members

# Posts by JamesCherrill which have been Voted Up

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

yes, it goes like this (pseudo code)

``````input = ""
while (input is not "exit")
get user input
if (input is abc) do task abc
else if (input is def) do task def
(etc)
``````
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

OK. You don't need to use a switch, you can do the same thing with a lot of if tests. Similarly, you don't need break, you can use a while loop to keep looping as long as the input is not "exit".

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

Start a loop
Get the user's input using a Scanner
if the input was "exit" then break out of the loop
use a switch to invoke the appropriate action
(end loop)

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

Look at your initial value for minDistance. Do you think you will ever find a smaller value than that? So the if test on line 10 will always be false.

Start minDistance at some very large value (larger than any distance you will calulate) Integer.MAX_VALUE would be the obvious choice.

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

Centering things is a responsiblility of the layout manager. Have a look at this and chose a layout manager that handles sizing & centering the way you want.
Note also the the frame has a layout manager that positions the panels, but the panels have their own layout managers (not necessarily the same) that layout the labels etc in the panels.

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

Simply test for a -1 selected index before trying to use it!

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

Yes aFile could be either, but if on the first pass of the loop aFile is not a directory then the call to recursiveTravesrsal (line 6 above) is made using some random value of childFolder.
childFolder is only correctly initialised when the for loop processes a directory

Yes, NIO is so different that your would start again, but it does use better programming patterns that are used massively in Java 8

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

The problem starts at line 33 (the first line in your code that appears in the stack trace). Looking there. and just before it, you call findMatchingRightParen on line 31. That method returns -1 if there's an error (as well as displaying the error message in the first line you posted). Then at line 33 you use the returned value to index into a String without first checking for error (-1). Using the -1 causes the StringIndexOutOfBoundsException on line 33 (line 4 of the stack trace)

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

Browser is easy, especially in JavaFX, maybe too easy because it doesn't leave a lot of places for you to show your skills. Media player may be better (also in Java FX) because you can add lots of value around the metadata, media library management etc. There's a lot of tutorial and sample code on the web to get you started.

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

We are just volunteers here, so nobody will be in a position to give you extended one-on-one training. But if you have specific questions or errors we will be happy to help through these public forums, where everyone can contribute to the solution.

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

Excellent!
Please mark this "solved" for our knowledge base. You should start a new thread if you run into any new problems.
cheers
J

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

The only way to compare equality between objects of a class is to use the class's `equals` method - the very one that you are writing now!
But if you use getCels() to get the value of each Temperature in degrees Celcius then that value is a primitive double, so you can use binary operators eg `t1.getCels() > t2.getCels()` because both those values are just doubles.
(Hint: == is a problem with floating point values, so don't be surprised if it doesn't work exactly as you would hope)

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

(tempNum)==(testObj)

tempNum is a double primitive
testObj is an object of class Temperature

they are so different in kind that using == makes no sense - its like asking "is London equal to Tuesday?"

For `equals` you want to know if two Temperature objects are "equal", where it's up to you to decide what "equals" means for Temperatures. The obvious answer is that the value of both Temperatures expressed in the same units (C or F) should be the same, within the limits of their floating point precision. You already have the `get` methods you need to get the values to compare.

ps: good coding guide part XIV: <soapbox>
the names you use for your variables and methods tells everything about your code and you as a programmer. "tempNum" is a horrible name for a temperature value that isn't temporary or just a number.
When you try to think of a good name you get into things like "currentTemperatureInTheUnitsSpecifiedByTempUnit" - which is onbviously silly, but a clear sign that the variable itself is confusing.
Personally that signals to me that I should simplify my design to the point where its easy to explain what the variables are. Eg in this case I would chose a unit to hold the temperature in (eg degrees Kelvin or Absolute) , and always convert F or C into that unit. Now I can name the variable "degreesKelvin", get rid of the tempUnit variable altogether, and the code is instantly 100% clearer - easier to read, easier to understand, easier to debug, easier to fix, easier to avoid mistakes in the first place. </soapbox>

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

OK. got it
5(tempNum-32)/9
isn't a valid expression. You must specify the multiplication explicitly. It's just not one of the compiler's best error messages ;)
(ps no point having doubles for the results when the input (tempNum) is just a float.

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

That looks OK - maybe the parens are already out of step before that code?
Also, I don't know what the data type for tempUnit is, but it's unusual to see a char as parameter for equals. char is an integer primitive type, for which == is appropriate.

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

A JButton with an ImageIcon will automatically set its own preferred size to fit the icon. The problem comes when you use a layout manager that re-sizes the buttons to achieve the desired layout. You need to chose layout managers for your frame and panels that don't resize the buttons. Have a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

new ImageIcon(...) is famous for not throwing any exceptions when it fails to find the file; it's just null. JLabel is happy to accept null for its image icon, so the final result is no image and no error messages. We see that quite often in DaniWeb "help!" posts.
Use the exists() method of the File class with the path/name of the file to confirm that your program can find the file. Alternatively, check the width/height of the image to confirm they are >0

JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

It's nothing to do with imports - the classes are defined in your source file.

Re: confuse in Java
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured

That code is so nearly right!. It's just that for the length of an array you use `myArray.length` not `myArray.length()`
(yes, I know that's confusing - for a String it's `length()`, but for an array its just `length`)

Alternatively, make your code simpler by using an enhanced for/each loop, eg

``````String[] names = line.split(" ");
for (String w : words) {
initials += w.charAt(0);
...
``````