Java questions

Aeren

n00b
Joined
Aug 29, 2005
Messages
37
Hi everyone! I encountered 2 problems when trying to make my program.

First, I wrote this as Main:
Code:
import java.io.*;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class Main extends Applet {
  public static void main (String args[]) {
//  new Graphics();
  System.out.println("Elindultunk!");
  NetMyThread progi = new NetMyThread( 1, 5000, "F:\1\1.txt", "F:\1\2.txt", "lines", "fast" );
  }
}
And it says: Can't find symbol:NetMyThread(it's another class i wrote)

My second problem is with NetMyThread:
It compiles, but says:
Note: F:\DIPLOM~3\NetMyThread.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I've checked it, and it's because i'm using Vectors. I make a new Vector, then try to add several Strings to it like this:
Code:
        String str;
        while( ( str = in.readLine( ) ) != null ) {
          sourceElements.add( new String( str ) );
        }
where in is an opened file.
Anyone knows why this is a problem?
Thanks!!!

Aeren
 
Not easy questions to answer.

1) Well I have three sugestions
-"Are you sure your class name is spelled correctly :p "
-It might probably be a problem with of namespace. Is your other class in the same package as this one?
-Or it could be a problem that you have never successfully compile the other class, so the compiler is a little borked.

Can't think of anything esle for question #1.

2) Now that one is a dilly of a pickle! I've personally never enconter that error message, probably because I've never used your particular compiler. Which, out of curiosity, what is it?

The only two things I can think of now is, when you add your String to your Vector, you don't need to create a new String with your String. Probably not your problem, but it's still good advice :D

Also, the way I read the error message, it might be that he is saying you are not handling exceptions correctly? Do you have the entire bit of code where you create/use/dispose of your input reader in a big try catch Exception. It might be worth your while to see check on that.

Cheers!
 
For the first problem:
- Yes it's written correctly :)
The 2 files are in the same directory. Should i include the other class in the Main file? If yes, how(what's the syntax)?

For 2:
This is the whole part:
Code:
      try {
        BufferedReader in = new BufferedReader( new FileReader( source ) );
        String str;
        while( ( str = in.readLine( ) ) != null ) {
          sourceElements.add( new String( str ) );
        }
        in = new BufferedReader( new FileReader( destination ) );
        while( ( str = in.readLine( ) ) != null ) {
          destElements.add( new String( str ) );
        }
        in.close( );
      } catch ( IOException e ) { }
I thought new String is needed, since i dispose of str in every cycle. Anyways, i tried with adding str only, and the same problem applies.
I'm using JEDPlus as editor as for java i'm using the newest Sun Java (J2EE 5.0 i think?)
 
You're using 1.5, aren't you? The "uses unchecked or unsafe operations" warning is printed when you use a container that can use generics but you have not specified the type of the contained object. Generics are new in 1.5.

Declare your Vector like this to get rid of the warning:

Code:
Vector<String> sourceElements = new Vector<String>();

Keep in mind this Vector can only contain Strings.

See http://forum.java.sun.com/thread.jspa?threadID=584311&tstart=-1
 
Thanks! That will only leave the first problem :)
Btw can i specify a self made class in the Vector? Will the
Vector return the specified Object, or still a simple
Object(and i'll have to do the conversion)?
 
Glad to hear you got rid of that nasty warning.

So to answer your first problem, you simply have to add an import at the start of your main class.

import NetMyThread;

That is if your NetMyThread class is the same folder and is in your current workspace.
 
I'm a big noob in project/workspace/etc stuff.
I wrote in:
Code:
import NetMyThread.java;
and it says: package NetMyThread does not exist
It misses a "." if i don't write in .java
What should i do?
 
Aeren said:
Thanks! That will only leave the first problem :)
Btw can i specify a self made class in the Vector? Will the
Vector return the specified Object, or still a simple
Object(and i'll have to do the conversion)?

Yes, you can declare a container of any class. Vector<MyCrazyClass> works if the compiler knows about MyCrazyClass.

If you declare a Vector<String>, it will return a String with no cast necessary. If you want the "old" behavior, you can always declare a Vector<Object>.
 
Aeren said:
I'm a big noob in project/workspace/etc stuff.
I wrote in:
Code:
import NetMyThread.java;
and it says: package NetMyThread does not exist
It misses a "." if i don't write in .java
What should i do?

You don't need the ".java". In fact, you shouldn't need the import if NetMyThread.java is in the same directory.

Seems like something else is going on here. Is NetMyThread.java in the classpath? Is NetMyThread part of a package? Are you sure NetMyThread has a constructor that matches what you're trying to call?

Can you post the beginning of NetMyThread.java up to the ctor you're trying to call?
 
No, you never write .java in your imports.

The thing is, if your two classes are in the default package then there is no reason to import them. If your compiler is unable to find the NetMyThread class, it's either that the NetMyThread class is in a different package (check at the start of the source file to see if there a "package com.org.net.something.or.other;" line. Or if there is not, then your compiler doesn't have your NetMyThread class in it's classpath.

Hope this helps!

EDIT: Darn you bassman, I will beat you next time Mouhahahah
 
Nemezer said:
No, you never write .java in your imports.

The thing is, if your two classes are in the default package then there is no reason to import them. If your compiler is unable to find the NetMyThread class, it's either that the NetMyThread class is in a different package (check at the start of the source file to see if there a "package com.org.net.something.or.other;" line. Or if there is not, then your compiler doesn't have your NetMyThread class in it's classpath.

Hope this helps!

EDIT: Darn you bassman, I will beat you next time Mouhahahah

Ahh... but your statement about the classes being in the default package is more accurate than mine about them being in the same directory. <yoda> A worthy adversary are you. </yoda> :)
 
As i said, i'm not very good with package and stuff. Since i'm not using any big editors and projects, i don't think i'm using any packages at all(self made packages that is).
My file is:
Code:
import java.util.*;
import java.util.Vector.*;
import java.io.*;

public class NetMyThread implements Runnable {
  private int id;                        //id of the thread, changes if other threads stop
  private int time;                      //how long it shall wait
  private String source;                 //where the saved file is(including name)
  private String destination;            //where the watched file is(including name)
  private String type;                   //what should be read in(lines or sentences)
  private String method;                 //SLOW but accurate or FAST but inaccurte
  private Vector<String> sourceElements; //storing the elements of the source
  private Vector<String> destElements;   //storing the elements of the destination
  private Vector matches;                //storing the matches 

  public NetMyThread( int id, int Time, String source, String destination, String type, String method ) {
    this.id          = id;
    this.time        = time;
    this.source      = new String( source );
    this.destination = new String( destination );
    this.type        = new String( type );
    this.method      = new String( method );
	init( );
  }
...
 
Let's check to see if it's a classpath problem. Are you compiling from JEDplus or the command line? Try running javac like this in the directory that contains your .java files:

javac -classpath . Main.java

(I'm assuming your file is named Main.java.)
 
bassman said:
Let's check to see if it's a classpath problem. Are you compiling from JEDplus or the command line? Try running javac like this in the directory that contains your .java files:

javac -classpath . Main.java

(I'm assuming your file is named Main.java.)

Cool! It's working this way. After a few corrections my proggy is alive!!! Only need the remaining 90% to go! :D :D :D
 
Aeren said:
Cool! It's working this way. After a few corrections my proggy is alive!!! Only need the remaining 90% to go! :D :D :D

Sweet. If you want to compile from inside JEDPlus, find where it sets the classpath. Or just stick with the command line.
 
Back
Top