-6

today I've a simple question for you. Please Solve this problem:

List<String> split1 = Arrays.asList(str.split(" "));
String split1.get(0) = split1.get(1);

It returns an error:

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

Please help me with the little Java Problem.

  • 5
    `String split1.get(0)` is sure not a valid variable declaration/name - not very sure what is being tried here: assigning the 2nd value to the 1st?? – user85421 Oct 01 '19 at 19:23
  • 2
    Are you trying to give your variable the name from the list element? – ernest_k Oct 01 '19 at 19:25
  • @ernest_k correct – KiwiGaming YT Oct 01 '19 at 19:32
  • 2
    Then related: [Creating a variable name using a String value](//stackoverflow.com/q/8631935) – Tom Oct 01 '19 at 19:35
  • 1
    Say you set the variable name to that value, how do you plan on using that variable name in the code to access it? You won't know what to type (unless you just access it every time). – Nexevis Oct 01 '19 at 19:39
  • @Tom no there's dog but I need String, also I need the String name to be a variable of the arraylist ( String arraylist.get(0) = "Hello" ) par example – KiwiGaming YT Oct 01 '19 at 19:42
  • @Nexevis the programm reads a file in which the String is often used, but only one time declared. If I have a String called like in the File, I could access it for every time – KiwiGaming YT Oct 01 '19 at 19:43
  • @KiwiGaming YT So why would you need the _variable_ name to be the `String`? Just name it anything and set it the value. e.g. `String yourValue = split1.get(0);` – Nexevis Oct 01 '19 at 19:45
  • @KiwiGamingYT As the answers there tell you: that's not possible, so don't stick to `dog`, but read what actually matters. – Tom Oct 01 '19 at 19:48
  • 3
    This sounds like an [XY problem](http://xyproblem.info/). There's some deeper issue here that's making you want to use dynamic variable names in the first place. Why do you feel like the variable names need to be dynamic? What issue would that solve? – Jordan Oct 01 '19 at 19:49
  • @Jordan because for example if in line 3 of the file the string name appears, i can write System.out.println(String defined in file) – KiwiGaming YT Oct 01 '19 at 19:49
  • @KiwiGaming YT Or you can just use an `if (yourLine.contains(yourString))`. What you are trying to do is not how Java works, please do some research to understand your options, as this is not required to do what you want. – Nexevis Oct 01 '19 at 19:50
  • @KiwiGamingYT It sounds like what you want is a `Map`. You can map keys to associated values. In this case, you'd use `split1.get(0)` as the key in the Map, and `split1.get(1)` as the value. Later, if you get a String that's a key in the map, you can use it to look up the associate value. – Jordan Oct 01 '19 at 19:52
  • @KiwiGaming YT Your Python edit has nothing to do with _variable names_, you are just setting the _value_ at the index to the other one and accessing it again. You can literally do that in Java as well, but that is not what you asked. Try typing `print(hello)` in the Python code and you can see you _changed_ the array. – Nexevis Oct 01 '19 at 19:57
  • @Nexevis you are correct – KiwiGaming YT Oct 01 '19 at 20:00

4 Answers4

0

Maybe this example can help you:

        String str= "Stack overflow";

        List<String> split1 = Arrays.asList(str.split(" "));
        String textSplited = split1.get(0);
        String textSplited2 = split1.get(1);

        System.out.println(textSplited+" "+textSplited2);
Scarabelo
  • 163
  • 6
0

You cannot assign name to a variable at runtime.

Tom
  • 16,842
  • 17
  • 45
  • 54
0

You are trying to assign the first element form the list to 0th position

    String str = "Stack Over Flow";
    String[] split1 = (str.split(" "));
    System.out.println(split1[0]);
    System.out.println(split1[1]);
    split1[0] = split1[1];
    System.out.println(split1[0]);
Sekhar
  • 61
  • 3
0

You can not create variable name in this way, read Naming section from https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

If you want to create variable dynamically then read -

Assigning variables with dynamic names in Java

Atul
  • 28
  • 6