Change string according to char array

I am very inexperienced in programming, so bear with me.

I need to write a class that mimics some attributes of the String class. In this particular method, I have to take a char array and get a string corresponding to this.

This is what I have so far:

1 char[] word = new char[80];
2 int wordLength = word.length;    
3
4 public String getString () {
5      String s;
6      s.length() = word.length;
7      for (int i = 0; i < word.length; i++) {
8           s.charAt(i) = word[i];
9      }
10     return s; 
11 }

It should be noted that the char array is null here, but in the main method, values ​​are assigned to it.

This is the error I get on lines 6 and 8:

The left-hand side of an assignment must be a variable

I have no idea why it does not recognize String s as a variable.

+4
source share
6 answers

I'm not sure that you are allowed to use this, but you can create a String from a char array as follows:

String s = new String(charArray);

Now to your code:

String s;
s.length() = word.length;

: s , - , :

String s = new String();

String, , chached:

String s = "";

Next String - , , - . Plus s.length() int NO pointer/reference, , .

s.charAt(i) = word[i];

.

String char char, - :

String s = "";
for (char c : charArray)
{
    s += c;
}

, s += c String.

StringBuilder:

StringBuilder s = new StringBuilder();
for (char c : charArray)
{
    s.append(c);
}
s.toString();

:

String s = new StringBuilder().append(charArray).toString();
+5

:

s.length() = word.length;
s.charAt(i) = word[i];    // same here

.

, String s .

getString() :

public String getString () {
  String s = "";
  for (int i = 0; i < word.length; i++) {
    s = s + word[i];
  }
  return s;
}

StringBuilder .

public String getString () {
  StringBuilder builder = new StringBuilder();
  for (int i = 0; i < word.length; i++) {
    builder.append(word[i]);
  }
  return builder.toString();
}
+4

- , . .

- , , . ,

int wordLength = word.length;

, wordLength, word.length. word.length - , .

,

s.length() = word.length;

, s.length() - , , , . .

+3

s.charAt(i) s.length() .

:

String s = String.valueOf(word);
+2

. , , , . -, StringBuilder char. , , word.length(), for, append, . , , toString().

public String getString () {
      StringBuilder sb = new StringBuilder(word.length);
      for (int i = 0; i < word.length; i++) {
           sb.append(word[i].toString());// you must give a string to the string builder
      }
     return sb.toString(); 
 }
+2
s.length() = word.length;

You cannot do this. A method length()is an access method that provides size String. This is not a variable. Note:

int variable; // the non-initialized variable
s.length(); // the method invoking
variable = s.length(); // the variable initializing

You can use the following snippet:

String s = "";
for (int i = 0; i < word.length; i++) {
    s += word[i]; // Both "+" and "+=" operators are a bad practice
}
return s;

Best practice is to use StringBuilderto combine characters into one String:

StringBuilder builder = new StringBuilder();
for (int i = 0; i < word.length; i++) {
    builder.append(word[i]);
}
return builder.toString();
+1
source

Source: https://habr.com/ru/post/1612081/


All Articles