aspose file tools*
The moose likes Beginning Java and the fly likes file cannot be resolved to a type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "file cannot be resolved to a type" Watch "file cannot be resolved to a type" New topic
Author

file cannot be resolved to a type

enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
hi guys, I have a Problem with this class. Eclipse says:
Multiple markers at this line
- Return type for the method is
missing
- file cannot be resolved to a
type




the rest seems to be ok, as Eclipse shows no mistakes:


// Snip large piece of code


Can you give me please give me a hint so that I can run the program???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8


This is the signature of your method? Then just look at it. The name of the method is "File". What type does it return? You don't say. Perhaps you meant it should return a File? If so, then you need to give the method a name. Or if you don't want it to return anything, you should declare it as "public void".

And its parameter is of type "file". Where do you import that? It isn't part of the standard Java API. Perhaps you meant "File" instead of "file"? They aren't the same thing.

That was what the two error messages meant. You don't have to look at the other 300 lines of code for that.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I edited your post because there is no need to post that much code. The first example was plenty. You are defining a method without a return type which isn't possible unless it's an constructor.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

enrique garcia wrote:

If I read this one line, without anything else, I see this as:
- a constructor
- of a class called File
- with a parameter of type file
- called dir

Now I doubt this is what you want. First of all, you call methods on dir that I recognize from java.io.File. So that should be "public File(File dir)". The other error tells me that this is not a constructor at all. In other words, the class this code is in is not called File. So you must either make it a constructor of the class, or add a return type. In this case, "void" would seem to fit. You then get "public void File(File dir)". While this should compile I still recommend you rename that method, for two reasons:
1) method names should start with a lowercase letter.
2) it will cause confusion with the File class you are using.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
ok, so I changed the name into QuizKarte



unfortunately the Eclipse seems to complain now with this message:

QuizKarte cannot be resolved to a variable


but there is a class called QuizKarte with strings in it, at the beginning of the code?!
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
sourcecode
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

enrique garcia wrote:
QuizKarte cannot be resolved to a variable


but there is a class called QuizKarte with strings in it, at the beginning of the code?!


Eclipse is likely complaining about line 8. You are using a variable name QuizKarte, and you don't seem to have a declaration for it. And classes and variables are two different things -- just because you have a class name QuizKarte doesn't mean that there is an implicite instance of it somewhere.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
I guess this means I have to cast QuizKarte somehow, right?

Eclipse is likely complaining about line 8. You are using a variable name QuizKarte, and you don't seem to have a declaration for it. And classes and variables are two different things -- just because you have a class name QuizKarte doesn't mean that there is an implicite instance of it somewhere.


Now, the string in QuizKarte should be available - which I am quiet sure wasn't before:




So, when I set a cast for QuizKarte it works? Could it be something like this?

enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
omg I think I got it !

Now, the only thing I have done wrong is here

the compiler says
meineQuizkarte cannot be resolved to a type, objectOutputStream cannot be resolved, fileWriter cannot be resolved


How comes the ObjectOutputStream doesn't get the stuff?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Henry Wong wrote:You are using a variable name QuizKarte, and you don't seem to have a declaration for it. And classes and variables are two different things -- just because you have a class name QuizKarte doesn't mean that there is an implicite instance of it somewhere.


Just like before. Same error.
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
ok, I guess there are a lot of mistakes. I am reading the HeadFirst book from O'Reilly. This is also the very first time I am doing any programming. I am trying to fix this class for 3 days now. Can anyone please give me a link to the entire (working) code of quizcard from chapter 14, HeadFirst, O'Reilly?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You'll find all samples here.

From what I see, you are mixing variable names and class names. A variable has a type, a name and a value. For example, "String name = null", The variable called "name" is of type "String", and its value is null.

1. meineQuizkarte eins = (Quizkarte) zeile; -> Remember the thing above ? "meineQuizkarte eins", this can't be. The variable "eins" is of type "meineQuizkarte" ? Where is this type coming from ? Shouldn't that be "Quizkarte", as you are casting "zeile" to Quizkarte.

2. objectOutputStream.writeObject(QuizKarte); -> This cannot be. You have to pass a variable to the writeObject method. Maybe "eins" ? I don't know what you want to do, so I can't tell for sure.

3. You are mixing "QuizKarte" and "Quizkarte". Remember that Java is case-sensitive.


[My Blog]
All roads lead to JavaRanch
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
"eins" is the german word for "one", "zeile" means "line"

basically, what I want to do is put a string into a text file, store this file in a file object and then making it possible to read from this text file. This casting thing is really confusing. I think I will not change any code from now on until I get it clear which class is where and does what. Also your second point looks interessting, I will bear that in mind
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

enrique garcia wrote:This casting thing is really confusing. I think I will not change any code from now on until I get it clear which class is where and does what. Also your second point looks interessting, I will bear that in mind


These are not errors that are related to casting. What you are showing us are compile errors -- using classes that don't exist and using variables that you never declared.

Henry
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
Ok, what I need is some kind of text-string for that what is written on the cards. I already had a try with something like this:



Christophe Verré wrote:You'll find all samples here.


unfortunately this site doesn't give any hint regarding how to set up the text string for the quiz cards.



Edit: This is how my code looks like now
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
I put
into the quizcard file. The compiler says "Unreachable code". So I have to put this string somewhere else. But where and how?
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
hum.. When a class is never used locally (The type Textdatei is never used locally), what can I do? I tried to implement the class, but this doesn't work?
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
When I resolve one problem 10 new errors appear. Always

Edit: Now the compiler says 3 times

Illegal modifier for the local class NaechsteKarteListener; only abstract or final is permitted



And, what is very confusing: new mistakes appeared, which where no mistakes before:

The field kartenListe is defined in an inherited type and an enclosing
scope



The only thing I changed was to insert a class with a text file before the BufferedReader is declared (at least it looks that I did that ok)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
You are not allowed to use the keywords public private protected and static inside a method.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Maybe you need to do a step back and first play with some simpler examples. When you try to write a longer, more complicated program and you get lots of errors at once, and you don't understand some of the basic concepts very well, it will be very hard to fix all those problems.

I recommend that you first learn exactly how classes, objects, variables, methods etc. work in Java, by creating small and simple programs that focus on one concept at a time. When you understand all that, then you can try to make a longer program that uses all those concepts.

Have a look at Oracle's Java Tutorials, where all the concepts are very well explained. Especially look at the chapters in Learning the Java Language and after that at Essential Classes.

Don't try to learn too much at once.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
 
subject: file cannot be resolved to a type