Ho bisogno di dividere una stringa di input in molte parti. Le suddivisioni dovrebbero avvenire in "\ n" (letteralmente backslash-n, non il carattere di nuova riga). esempio voglio trasformare questo:Problemi nell'utilizzo di myString.split (" n");
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw y #0000ff\ny = y & x\ndraw y #ff0000
in questo:
x = [2,0,5,5]
y = [0,2,4,4]
draw y #0000ff
y = y & x
draw y #ff0000
avrei pensato che stringArray = string.split("\n");
sarebbe stato sufficiente.
Ma mi dà lo stesso output come input nel codice seguente:
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Input\n");
String s = br.readLine();
NewInterpreter interpreter = new NewInterpreter(s);
interpreter.run();
}
public NewInterpreter(String input) {
this.input = input;
this.index = 0;
this.inputComponents = input.split("\n");
System.out.println("Output: ");
for(String s : inputComponents)
System.out.println(s);
}
Enter Input
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw x #00ff00\ndraw y #0000ff\ny = y & x\ndraw y #ff0000"
Output:
x = [2,0,5,5]\ny = [0,2,4,4]\ndraw x #00ff00\ndraw y #0000ff\ny = y & x\ndraw y #ff0000
Ogni aiuto è molto apprezzato, grazie!
se si definisce il membro dati in questo modo'private String [] inputComponents; 'Quindi la divisione usando' \ n' dovrebbe funzionare @Dylan: controllare il link qui https: //ideone.com/5pN6xX. dato che l'input ha due '\ n' questo è il motivo per cui la nuova linea in arrivo sta arrivando. –
@VinayakPingale Il tuo campione è sbagliato. OP riceve il suo input dall'utente, non un letterale stringa Java. In questo caso, ciò fa la differenza nel mondo. Per avere un input equivalente, dovresti sostituire tutti \ n' nella tua stringa di esempio con '\\ n'. –
Controllalo ora @RobbyCornelissen: https://ideone.com/5pN6xX –