0

I want to split the input string by "\n", here is my code:

import java.util.ArrayList;
import java.util.Scanner;

public class toy_interpreter {

    public static String program;

    public static void getToyProgram() {

        Scanner prog1 = new Scanner(System.in);
        System.out.println("Please enter the toy program");
        program = prog1.nextLine();

    }

    public static String[] splitList() {

        return program.split("\n");
    }

    public static void main(String[] args) {

        getToyProgram();

        String[] parts = splitList();
        String part1 = parts[1];
        System.out.println(part1);      
    }
}

But the output shows that: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at toy_interpreter.main(toy_interpreter.java:36)

could you help me?

Henrique Sabino
  • 546
  • 3
  • 18
  • 4
    You are only reading one line `program = prog1.nextLine();` - why do you think you can split? – Scary Wombat Dec 11 '19 at 23:51
  • You are reading only one input line on your program, so there'll be only one `\n` in your input, thus one position on your array. if you want to receive multiple `Strings` from the user you should use a loop – Henrique Sabino Dec 11 '19 at 23:55
  • You are reading only one input line on your program, so there'll be only one `\n` in your input, thus one position on your array. if you want to receive multiple `Strings` from the user you should use a loop – Henrique Sabino Dec 11 '19 at 23:55
  • When you [debug](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) it you can see what causes the issue and why. Beware of Scanner, though, it is [tricky](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo?noredirect=1&lq=1). – Nikolai Dmitriev Dec 11 '19 at 23:56
  • I can add multiple line and output it, but i can not output the result with index = 1 or 2 3 after program be splited. – Henry li Dec 11 '19 at 23:56
  • @HenriqueSabino, nextLine consumes `\n` but [doesn't include](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine%28%29) it in its return value. Scanner is tricky and nasty. It is **evil**. – Nikolai Dmitriev Dec 11 '19 at 23:58
  • but if i input “A = 2 \n B = 8 \n C = A + B \n C” , the output will do the same. – Henry li Dec 12 '19 at 00:00
  • @gurioso That's right, forgot about that one, anyways, because they're reading one line only there'll be only 1 position in the array – Henrique Sabino Dec 12 '19 at 00:00
  • 1
    @Henryli that's an interesting way of inputing the string, lol. Have you tried changing the split character? Like inputing "A = 2;B = 8;C = A + B;C" and splitting it by the semicolon – Henrique Sabino Dec 12 '19 at 00:02
  • yes, i try to change" \n" to "n" , it can output a perfect result. – Henry li Dec 12 '19 at 00:17
  • 1
    Maybe this can clarify something: usually when people talk about the character `'\n'` (decimal 10 on the [ascii table](http://www.asciitable.com/), meaning **new line**), which is one character, rather than the string `"\n"` (ascii 92 110), which are two characters. Note: `'` vs `"`. Nobody uses "\n" as a record separator btw. If you do perhaps you arrive at @sidgate's approach, but I guess, you don't want to get there anyway. – Nikolai Dmitriev Dec 12 '19 at 01:47

2 Answers2

1

You are reading a singular line at prog1.nextLine(), so I am not sure how many objects you expect in the array, but I feel from your naming convention and the error code that you might not realize that arrays start at [0], not [1].

In your case split() will save your entire line to [0]. Since it ends on the lineend, the length of the array is 1, as split() will not create another String for an empty element. You are trying to access [1], which is the second element of the array, which does not exist. Hence the ArrayOutOfBoundsException!

If you wanted to save multiple lines to program, make sure not to just use the = sign. It'll overwrite your previous String with the new one. Trying using program += for repeated uses, or use the .concat() method. Java will handle both the same during compile anyhow. For that matter, you might want to look into the StringBuilder class if you plan to build a multi-line string from user input.

LlamaSage
  • 69
  • 6
0
public static String[] splitList() {
    return program.split("\\\\n");
}

split function takes a regex. Your input has a backslash that needs to be escaped in the input. And this backslash again has to be escaped for regex

sidgate
  • 11,396
  • 8
  • 53
  • 100