0

I was trying to store input as

5 
3DRP 3QEW
8AQW 9ADA

I want to read that input in as a copy paste and put it. I've tried this:

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    String userNumber;
    userNumber = scan.nextLine();
    String[] tokens = userNumber.split("[ ]");
    System.out.println(tokens[1]);
    for(int i = 0; i < tokens.length;i++) {
        System.out.println(tokens[i]);
    }
    scan.close();
}

My goal is to basically read that input in as a copy paste into the IDE or through a file a .txt and then store every single character besides whitespaces into a char array that is 1d or 2d.

3
  • 2
    and what problem are you facing? apart from split("[ ]"); doesn't seem to be required for your input. Commented Sep 23, 2017 at 9:06
  • Apart from that: Don't use scanner.close(), as it will close the System.in stream. Commented Sep 23, 2017 at 9:15
  • @nullpointer I want to read the entire line of code but it only reads the first line when I copy paste it. I want to only read the actual ASCII not the white spaces, in that case the white spaces are included Commented Sep 23, 2017 at 9:22

1 Answer 1

1

From what I understand, you want to read 3 lines from the console, remove all the white spaces from that text and store it as a char array.

If that is the case, here is how you could do that:

    int numberOfLinesToRead = 3;
    try(Scanner sc = new Scanner(System.in)){
        StringBuilder buff = new StringBuilder();
        while(numberOfLinesToRead-- > 0){
            String line = sc.nextLine();
            String noSpaces = line.replaceAll("\\s", "");
            buff.append(noSpaces);
        }
        char[] characters = buff.toString().toCharArray();
        System.out.println(Arrays.toString(characters));
    }catch (Exception e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

5 Comments

any specific reason to use the try with resources other than to close the stream automatically ? Seems a bit overkill to me, since Scanner doesn't throw any checked exceptions
@ChandlerBing Just a habit, comes in handy when changing the stream. There are no modifications required if I change System.in to a FileInputStream.
@Titus what if I don't know the number of lines I want to read?
@Peerless You'll have to find a way of stopping and set that as the condition of the loop. For example, you can stop reading lines when you enter something like stop into the console. If you what to do that, you can change the loop's condition to this while(!line.equals("stop")) and define the line variable outside of the loop
@Titus You shouldn't close the scanner, as it will also close System.in, which shouldn't be closed at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.