Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Incompatible types: String cannot be converted to String[]

Im trying to read a file and then pass the values through my variables

String name = in.next();
String age = in.nextInt();
String[] friends =in.next();

Person aP = new Person(name, age, friends);
print blah blah
return aP

Any help is appreciated
You can't declare an array of strings like that. You'll have to initialize the array first and then add the values you read in.

String[] friends = new String[20];
https://www.youtube.com/watch?v=nP_kYxxhaX4
You can't just assign string to an array of strings like that, you can either do this:

Code
String[] friends = new String[size_of_your_array];
friends[0] = in.next();

or do like you did but use ".split()" method on the user input as ".split()" will split the string by the delimiter you specify and return an array of strings.
[Image: 4f2d8e8a917f4be2a829a0405325e880.png]
How are you formatting your storage?
I solved it by created 3 sub friends and parsing each of what they read into it. probably an awful solution.
i did string friend1 = in.next; string friend2 = in.next; string friend3 = in.next;
String[] friends = {friend1,friend2,friend3};
(May 10, 2017 07:48 AM)pureacca Wrote:  I solved it by created 3 sub friends and parsing each of what they read into it. probably an awful solution.
i did string friend1 = in.next; string friend2 = in.next; string friend3 = in.next;
String[] friends = {friend1,friend2,friend3};

Easier with a loop and friends[i] = in.next()
(May 10, 2017 09:59 AM)Sheeple Wrote:  Easier with a loop and friends[i] = in.next()

Considering it's only 3 strings, it would be pretty much the same.
[Image: 4f2d8e8a917f4be2a829a0405325e880.png]
(May 9, 2017 08:00 AM)pureacca Wrote:  Im trying to read a file and then pass the values through my variables

String name = in.next();
String age = in.nextInt();
String[] friends =in.next();

Person aP = new Person(name, age, friends);
print blah blah
return aP

Any help is appreciated

1. - I am assuming this is not full code, but ensure that your 'Person' class has the proper constructor types.
2. - Arrays should be declared in this format:
Code
String[] myStringArr = new String[size_of_array];
So you would do something like:
Code
//Let's say you are taking in 3 friends...
String[] friends = new String[3];
for(int i = 0; i < 3; i++) {
     friends[i] = in.next(); // take in next String and put it in the Array
}
(May 10, 2017 02:51 PM)ComplexEternity Wrote:  1. - I am assuming this is not full code, but ensure that your 'Person' class has the proper constructor types.
2. - Arrays should be declared in this format:
Code
String[] myStringArr = new String[size_of_array];
So you would do something like:
Code
//Let's say you are taking in 3 friends...
String[] friends = new String[3];
for(int i = 0; i < 3; i++) {
     friends[i] = in.next(); // take in next String and put it in the Array
}

Uhm he said he is having trouble with the String array, not the constructor. 2, thats basically a repost of what everyone said. Three, Lists are amazing. (List<?>)
(May 10, 2017 10:00 PM)jp78 Wrote:  Uhm he said he is having trouble with the String array, not the constructor. 2, thats basically a repost of what everyone said. Three, Lists are amazing. (List<?>)

Excuse me, but I gave a solution to the String array problem. And no, I did not repost what everyone said, I gave my own solution (which is similar to everyone else's and there is no problem to that)


Possibly Related Threads...
Thread / Author Replies Views Last Post
by HackersApr 29, 2024 05:37 AM 27 467 Apr 29, 2024 05:37 AM
Last Post: nucleo_type09
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia
[C#] by passwordsMay 16, 2017 06:23 AM 7 753 May 16, 2017 06:23 AM
Last Post: darkdoctor