Hack Forums
[Java] Incompatible types: String cannot be converted to String[] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Incompatible types: String cannot be converted to String[] (/showthread.php?tid=5618952)



Incompatible types: String cannot be converted to String[] - pureacca - May 9, 2017

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


* - StayTech666 - May 9, 2017

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];


* - TheNaked - May 9, 2017

https://www.youtube.com/watch?v=nP_kYxxhaX4


* - SIM-429621 - May 9, 2017

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.


* - Owl - May 10, 2017

How are you formatting your storage?


* - pureacca - May 10, 2017

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};


* - Sheeple - May 10, 2017

(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()


* - SIM-429621 - May 10, 2017

(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.


* - ComplexEternity - May 10, 2017

(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
}



* - jp78 - May 10, 2017

(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<?>)


* - ComplexEternity - May 12, 2017

(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)