• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Incompatible type issue

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All you want to do (I guess) is this..

 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the contents of your phone.txt again?
 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not test the code but it should work.

If you are having problems following just ask.




 
Mike Osterhout
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic