This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Incompatible type issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incompatible type issue" Watch "Incompatible type issue" New topic
Author

Incompatible type issue

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
HI.

I am using the following code to read the contents of a file into an array


Then I am testing the code in another file like this


I am hoping that once I figure out how to access this array in the read method I can begin to perform tasks on it such as split, search and delete. The problem is I am hung up on the incompatible types. I dont understand why I am able to get the length of readwrite.Read() but I cant set it to an array...

Thanks for any insights on this one.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

It looks like the Read() methods is an attempt to read the whole file in. Why are you calling it so many times in a loop?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

But to answer your question....



The Read() method returns a string array, which you are trying to assign to a string reference. A string reference is for strings -- not string arrays.

Henry
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Yes it reads each line of the file into the line array. I am doing it that way so I have

line[1] = "some string"
line[2] = "some other string"
etc...
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Well, you are assigned the return of Read (which is String[]) to and index of your String array.

If you have



and code



you are trying to assign and Array to a String.

Your code is a little cumbersome too.

The conditional part of a for loop is checked with every cycle. This means you invoke Read() with every loop cycle, which is a major performance hit and can produce weird results.
You are always better off assigning the result to a local variable before the loop, and then check on the local variable.


JDBCSupport - An easy to use, light-weight JDBC framework -
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Henry Wong wrote:But to answer your question....



The Read() method returns a string array, which you are trying to assign to a string reference. A string reference is for strings -- not string arrays.



I am still not sure how I might remedy this
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
All you want to do (I guess) is this..

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok I see that I am assigning an array to an index of an array, which in my mind should create a multidimensional array. But even if it did that is not going to solve my problem.

I guess I am wondering how I might be able to use my Read class to create an array of the file contents and then be able to work with that array in other files.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Ok.

Let's start from scratch.

Explain what exactly you want to do with the Array that is being returned from the Read() method.

Also, why are you using an Array in the first place and don't return the plain file contents?
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
The requirements are too search the file, add to the file and delete from the file. I figured the only one out of those that an array list would be a major benefit is deleting.

I am trying to create a method that will read my file into an array, where each element will be a line in the file.

Then I can split the strings/elements to allow for search. Add an element to the end of the array to write to the file, and delete an element to delete a line.

At this point I have an array of strings(lines/records in the file) which I figured was a good start. Now I just need to be able to manipulate each one of those elements/records/lines.

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You are better off using a List or Set over an Array.

Lists/Sets give you the possibility to remove and insert, since they can dynamically grow/shrink (which Arrays cannot) ...
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I realize an array list would be better however I am not sure how to properly create that. I think you showed me somethign about a personbean, but I tried that to no avail.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
What are the contents of your phone.txt again?
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Smith;John;332-5467
Jones;Mary;456-2312
Sam;Henry;345-0987
Henry;Ed;768-0934
Jones;Paula;222-8787
Smith;Karen;343-0223


Also maybe you have a good resource on array list, My text is terrible and I find finding good examples online is tough. Also the javadoc is still hard for me to read.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I did not test the code but it should work.

If you are having problems following just ask.




Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Um. Im lost. Its bed time, back at it when I get some free time. Good thing I have a couple weekends to hash this out.

Thanks for your help Sebastian, maybe you can comment that code. You introduced a lot of stuff with no explanation... Gotta remember I am a complete NOOB. Only 8 weeks into an introductory course on java.

Right now I am just trying to figure out how I can work with this file after reading it, I am not looking for the entire solution. I am still thinking a good solution would be too read each file into an element of either an array or array list and work with that array to do the mods to the file...

PS. To anyone who is following along below is the code that I think is going to hopefully get me to the promise land.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
You may need to read the file twice.
  • Read the file, one line at a time. Don't record any lines.
  • Count how many lines there are.
  • Set up an array with that number of elements.
  • Back to the beginning of the file.
  • Iterate through the array; for each member read a line into that element.

  • You will need to know this idiomWhen you read through a file, you have n lines, so you read the lines. When you get to n + 1, your reader reads nothing and returns null. Then your loop terminates.
    If you have an empty line (eg enter key and nothing else) your reader returns "" (the empty String), so you might want to do something different if you get "".
    You need the additional () around the assignment bit so as to raise the precedence of the = operator above the != operator. Otherwise the compiler thinks you are trying to assign the result of != null (a boolean) to the String.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Incompatible type issue
     
    Similar Threads
    Getting the MAC address and using that for licensing
    Issue with reading from a file
    Reading File contents to a String
    Strange(?) behaviour
    declaring array of vectors