简体   繁体   中英

Appending strings to an array in java

import javax.swing.*;

public class Condicional{
    public static String getNaipe(String carta){
        String[] partes = carta.split("");
        String[] nuevaCarta;
        switch (partes[0]){
            case "a": nuevaCarta.append("As");
        }
        return nuevCarta[0];
    }

    public static void main(String[] args){
        String notacion = JOptionPane.showInputDialog("Introduzca la notacion de la carta:");
        String significado = getNaipe(notacion);
        System.out.println(significado);
    }
}

I am trying to make an array where i will append 2 words strings but it marks an error. I try using StringBuffer but i don't quite understand how it works and apparently is not the same as an array.

It returns this error:

condicional.java:8: error: cannot find symbol
case "a": nuevaCarta.append("As");
^ symbol: method append(String)
location: variable nuevaCarta of type String[]
1 error

You should use a different data structure. Arrays have a fixed length when you create them, so you can't add more elements afterwards and make them longer. You need a mutable object so that you can continue to add new elements to it, like an ArrayList<String> .

Also, you may want to use es.stackoverflow.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM