JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can use System.setOut(PrintStream out) to send all your console output to a file instead of the console, without having to change any other code.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here at DaniWeb we try to help people learn Java and develop their Java skills. We do NOT do people's homework for them. Your post explains and teaches nothing, it just gives the OP a chance to cheat. In future please help by pointing people in the right direction - eg tell them which classes and methods they should read about, or give them some sample code that they will have to understand and adapt to their needs. If you feel you should correct someone's code then that's useless if you don't explain what you changed and why you changed it as you did. If you need to explain with actual code then explain why you coded it the way you did. Don't just spoon-feed them a solution to copy and paste.

castajiz_2 commented: you are right +4
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The missing methods are because the class is declared to implement an Iterator, but has an empty body that (obviously) doesn't implement the methods declared in the Iterator interface ( http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html ).

ps Speaking technically, T and L are type variables, not necessarily classes. A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable ( http://docs.oracle.com/javase/tutorial/java/generics/types.html )

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I could create a GUI using Swing and add by using Robot class to imitate mouseactions and keyactions and by using screen capture and Compression and TCP/socket programming I could send the images.

There was a long thread on this subject a couple of years ago here. We did exactly that.
We tried standard compression methods, but finally got much better performance using a custom algorithm that just transmitted changed pixels using a form of RLE.
I'm still using the resulting code daily.

http://www.daniweb.com/software-development/java/threads/254810/find-the-differences-between-two-images-and-the-locations-of-the-differences/

There are, of course, commercial and open source products that do that. Windows has one built in, and VNC is open source, TeamViewer has a very good version free for non-commercial use. If you just want the functionality, then use one of these or one like them.
But if you want a really good Java learning project, this one is excellent!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

stultuske was right until last month.
The latest version of Java (Java 8) allows static methods and "default" instance methods to be defined in interfaces, which are inherited by any class that implements the interface.

stultuske commented: when you're right, you're right +13
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

yes, it goes like this (pseudo code)

input = ""
while (input is not "exit")
   prompt user with menu choices
   get user input
   if (input is abc) do task abc
   else if (input is def) do task def
   (etc)
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

Start a loop
Print the menu using System.out.println
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)

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

(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% …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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.

Raymond_3 commented: haha! wow thanks james can't believe it was just me needing to put an asterisk! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

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 4,733 Most Valuable Poster Team Colleague Featured Poster

It's nothing to do with imports - the classes are defined in your source file.
@hannahaddad told you what to do a day ago. Until you follow that advice you will remain stuck.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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);
   ...
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread".
That means that once your paintComponent method starts NOTHING else will happen in Swing, including no mouse or keyboard events, until your method finishes. You can update stuff and loop and sleep as much as you like, but none of that will affect what's on the screen until your method has terminated.

If you want to do stuff in steps over a period of time, here is the right way:
Start a javax.swing.Timer, and return. Every time the timer fires you can update whatever needs updating and return. Inbetween the timer firings Swing will be free to update the screen. Swing will call paintComponent when needed, but you can call repaint() if you want Swing to schedule a call soon.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

AFter entering 23 do you enter a space or a carriage return? (If you just type 23 the it will be waiting for the next character in case that will be a digit.)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

... further to the JavaFX suggestion...
just for fun I tried it. It's really easy, apart from one gotcha that took a little research. Anyway, here's the result

You create a JavaFX MediaPlayer, passing it the URI of the mp3 file, and call its play() method. The following code gets the URI string from an ordinary file path/name string...

String uriString = new File(fileName).toURI().toString();

then this creates the player

MediaPlayer player = new MediaPlayer(new Media(uriString));
player.play(); // or stop() or pause() etc etc

yes, it's really that easy, and is 100% mainstream Oracle-supported Java.

Now the "gotcha". If you run this as part of a non-JavaFX application then the JavaFX environment won't have been initialised properly. However, you can force JavaFX to initialise as part of an ordinary application by simply creating a Swing JavaFX panel, as in

new javafx.embed.swing.JFXPanel(); // forces JavaFX init

so that's it. In summary, a trivial player looks like:

void playMP3(String fileName) {
    new javafx.embed.swing.JFXPanel();
    String uriString = new File(fileName).toURI().toString();
    new MediaPlayer(new Media(uriString)).play();
}

... plus the imports of course.

import java.io.File;
import javafx.scene.media.MediaPlayer;

Best of all it's fully supported and is included in the standard Java SE JRE from Java SE 7 update 6 onwards.

stultuske commented: nice. might take a look myself whenever I write me a new one. +13
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your earlier version was almost there

    while ((line = reader.readLine()) != null) {
      String[] name = line.split(" ");
      //Fullname = name[0];
      for(int i = 0; i < name.length(); ++i){
        Initials += Fullname.charAt(0);
      }

It just needs you to use name[i] to refer to each name inside the inner loop, and you will be heading in hthe right direction...

       Initials += name[i].charAt(0);

ps Java 8 uses :: to refer to methods, eg this::doIt means "run the method doIt() for the current object", someObject::print means "run someObject's print() method". Used a lot to replace anonymous inner classes, eg for Runnables or Swing event listeners, eg

saveButton.addActionListener(this::save); 
// calls this object's save(ActionEvent e) when button clicked
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your code is almost right - you have the right pieces but the order is a bit messed up.
Read each line of the file,
split each line into words (ie use split with the default separator of a space),
then use chatAt to get the first letter of each word.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Try
TimeUnit.SECONDS
or chnage the import to
import static java.util.concurrent.TimeUnit.*;

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java will autobox/unbox int<->Integer, double<->Double,etc, so milil's example is OK.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
  1. To prevent the application for terminating when yuo close the window, use WindowConstants.DISPOSE_ON_CLOSE (releases all the window's resources, leaves the program running) or WindowConstants.HIDE_ON_CLOSE (just makes the window invisible).

  2. For your Thread exercise I suggest you forget SwingWorker for the moment. SwingWorker does all the thread-related stuff for you, so it's not helpful for learning.
    All you need to do is to call invokeLater (ie what you do now) to run the method that will create the window on the Swing EDT thread - no other threads should be involved in that. Then create and start an ordinary thread that prints to the console in a loop. You may want to think about how to terminate that loop...

If after that you want to have some kind of interaction between the console loop and the window, that gets a bit more tedious, and you start to see why SwingWorker was invented.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You coded
54. setDefaultCloseOperation(EXIT_ON_CLOSE);
and Java is doing what you told it! That option instructs Java to exit the whole program when the JFrame is closed.

I realise that you are practicing with Threads here, but t4 just executes an addActionListener, which isn't enough for you to be able to observe any multi-threaded behaviour.
Also t1 and t2 are missing the whole point of SwingWorker, because Swing runs the doInBackground and process methods in suitable Threads anyway. Indeed by using your own thread for the process method you are breaking the program - process is where you updade the GUI so it MUST be executed on the Swing EDT thread (which is where SwingWorker calls it)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, Eclipse is trhe other. I can't comment on the quality of either sources because I haven't read them myself. I would expect them both to be of very high quality.
I suggested NetBeans because that's closer in its roots to Sun/Oracle Java. More seriously, Eclipse uses IBM's SWT GUI library rather than Java's Swing or JavaFX. Although SWT is used a lot by IBM, Swing is far more common in general Java usage.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The answer. as always, is in the doc, the Java Language Spec in this case. Section 15.12.2.5.

15.12.2.5 Choosing the Most Specific Method
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one ... The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
<followed by pages of really difficult stuff>

The first call goes to sifter(Object) because only A[]... and Object are applicable, and A[]... is considered less specific than Object.
The second call goes to sifter(B[]) because B[]..., B[], and Object are applicable, and B[] is nore specific than Object

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The usual GUI lib for Java is its javax.swing - part of the standard Java API. However, starting with Java 8 this month Oracle are pushing JavaFX as a superior replacement for Swing with lots of animations and clever effects.

I'm hoping to get time to post some simple "why are lambdas a good thing" tutorials here when they are released. I'm a big fan.

As for projects - have a look at NetBeans - it's one of the two all-singing all-dancing IDEs for Java, so it's a vast GUI project with massive technical content under the hood. It's written in Java, and you can join the project at https://netbeans.org/community/index.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't you think that the point of your project is to do it yourself?
Nobody here will help you cheat.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I didn't say "read the API", I suggested you read the source code of the Java classes that implement the API. That satisfies everything in your first post except the use of build tools.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

After the for loop (Lines 34-38) i will be >= CurrentMonth.length, so that's why you have the error - array index out of bounds on line 45

stultuske commented: indeed +13
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Obviously you were able to work out that 1500 can use 7 200's, then that the remainer was 100, which is one 100 note, so just code the method/formula you used to do that in your head!
If that's not clear, just create a few more sample cases like that and work them out with paper and pencil, wrting down all the calculations and intermediate results. Once you've done that you will know exactly what the required logic is - converting that to code is completely trivial, in any language.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

One simple way is the use as many 200 as possible, then as many as 100 etc until what's left can be done in 10s. If you get this version working then it should be easy to upgrade to more complex "A.I." algorithms to give "desirable" mixes of notes

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In my opinion it's reasonably good code. So although there are quite a few points raised in this post, don't be discouraged.
The variables names are very clear, so it doesn't need a lot of comments. Many of the comments you do have explain what you are trying to achieve - excellent.
The unneccessary coments are lines like 12, 15, 24. Anyone reading this code should know enough Java to understand public static void main, or new Scanner(System.in)
The else clause on 45/46 is redundant.
Java naming conventions are to use "camel case" not underlines for variable names, eg milesPerGallon
You should be very careful to keep you indentation and brackets lined up - errors in structure are very hard to see otherwize. A programmer's editor is useful here.
YOu may want to deal with the user entering 0 for the gallons - right now that will trigger a horrible crash.
I don't know why you have the if test online 50.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

miles_per_gallon = miles / gallons;

Because miles and gallons are both ints this division is done in int arithmetic. After that, the result is converted to double to match the target variable miles_per_gallon, but by then it's too late.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 10 you calculate Math.pow(x, i); but you don't do anything with the result.

Tarek_2 commented: This answer resolves the problem. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think you have confused us with people who will do your homeowrk for you for nothing?

If you have specific questions or problems then post them here and someone will help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

When you print an object, the print or println method calls that object's toString() method to get a printable representation of that object.
toString() is defined in the Object class, so it is inherited by every other Java class. The version in Object just returns a code representing the type of object, and its hash code - technically correct info, but mostly useless. That's why you should override the inherited version, and supply your own toString() method in any new class that you create. You can ensure that your toString() returns something useful.

The @Override annotation on a method tells the compiler (and anyone reading the code) that the following method definition overrides an inherited one. Thus the compiler can check that you have overridden it correctly, and everyone else can understand what you intended.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Using a null layout manager is a really bad idea unless you are coding for some very specific hardware configuration and the code will never run run on (eg) a Mac retina display. Pixel sizes are very different from one machine ot the next, so they are a near-usless way of specifying layout that has any text anywhere in it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes.