• Post Reply Bookmark Topic Watch Topic
  • New Topic

Incompatible types error  RSS feed

 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using netbeans and have an error that reads "incompatible types: String cannot be converted to List<String>"




I don't understand why I am receiving the error. I thought that .next() returned a String?

Thankyou for all your help
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nico Fish wrote:I am using netbeans and have an error that reads "incompatible types: String cannot be converted to List<String>"




I don't understand why I am receiving the error. I thought that .next() returned a String?

Thankyou for all your help


not sure i can be much help because I'm quite new to this, but I'd remove that line and output tempStringScanner.next() to the console to see what you get. might help you spot a problem.

 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I am very new what do you mean by output it to the next console?
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nico Fish wrote:Sorry, I am very new what do you mean by output it to the next console?


i think your problem is actually the declaration of the list, not the line where you add the string to the list
I'm just having a play with it now myself

anyway, here's what i meant about outputting to the console:

remove the line where you get the error, and type "System.out.println(tempStringScanner.next());"
instead of adding the String to the arrayList at position i, it should print that value to the screen.
i'm pretty sure that isn't your problem. but its worth checking to see if tempStringScanner.next() is working correctly. narrows down the problem i guess.
like i said, i'm new so probably not much help, but you might as well keep working on it while you wait for someone with a lot more knowledge!
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried just using a declaration like:

ArrayList<> fileDArr = new ArrayList<>();

?
 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did that prior to your post to check and it printed the text file so that is not the problem.

I'll wait for someone else. I already spent an embarrassing amount of time trying to figure it out myself.
Thanks, for the help
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nico Fish wrote:I did that prior to your post to check and it printed the text file so that is not the problem.

I'll wait for someone else. I already spent an embarrassing amount of time trying to figure it out myself.
Thanks, for the help



no problem buddy. one more try ;)

this creates a list containing a list of strings.

so fileDArr refers to a list of lists, so you can't add a string at position i, because it expects a list, not a string.

try
that's my guess anyway, and it seems to be what the compiler is telling you too!

 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm if that is the problem then how do I fix it?


I think I will use a HashMap instead



Im getting a different error now

This is my output:

"number of files: 6
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at rna.sort.RNASort.FileSearch(RNASort.java:50)
at rna.sort.RNASort.main(RNASort.java:67)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
"
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


remove this ^

and put: ArrayList<String> fileDArr = new ArrayList<String>();

OR

I guess you could try List<String>fileDArr; rather than List<List<String>>.

you see what i mean about a list of lists? whatever is in the angular brackets is what the compiler expects the list to contain, and you've written <List<String>>, so fileDArr is a list of lists, where each list contains a string.

so fileDArr cannot take a string at position i, because each element in the fileDArr list contains a list itself, whose elements contain strings


hope that was clear enough/correct!
 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you mean about list of list. However, I need a 2d list as I am dealing with multiple files. I think the HashMap could work as I can assign each file a key.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nico Fish wrote:I understand what you mean about list of list. However, I need a 2d list as I am dealing with multiple files. I think the HashMap could work as I can assign each file a key.


well as far as i can see in your original code you've created the list fileDArr, but you haven't populated the list itself with other lists.

you'd then have to add the strings to each element/list in the list. if it was a 2dArray it would be something like: fileDArr[x].add(i, tempStringScanner.next());
in an arraylist you'd use an iterator to help you achieve the same thing i guess.


sorry i couldn't be more help buddy.
 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel very incompetent now. I see what you mean I did not need a 2d list. I fixed that and took care of the other error by using a BufferedReader instead of a scanner (not sure why that fixed the problem but it did)
 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel very dumb.

Thank you very much Nick. I fixed the code and it runs perfectly now!
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nico Fish wrote:I feel very dumb.

Thank you very much Nick. I fixed the code and it runs perfectly now!


no worries, glad it helped!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!