0

I cannot type the TAB key into the console for 2 strings separated by the tab key, i used the split method but it didn't work. The Java console does not accept the TAB key.

Here's a snippet of my code

String[] data;
String description, amount;
    
Scanner scan = new Scanner(System.in);
    
System.out.println("Enter the description, followed by a [TAB] key, and then the amount: ");
data = scan.nextLine().split("\\t");
description = data[0];
amount = data[1];
    
    
System.out.println("You have entered: " + description + " and " + amount)
3
  • Please be more specific. Exactly what type of console are you using?
    – k314159
    Commented Jul 8 at 15:05
  • I use BlueJ for my code editor Commented Jul 8 at 15:33
  • 2
    You can't use a tab character in consoles usually. Choose a different one
    – g00se
    Commented Jul 8 at 15:52

1 Answer 1

-1

if this not working you can use BufferedReader to take the input

for example

        String[] data;
        String description = "", amount = "";

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            data = br.readLine().split("\\t");
            description = data[0];
            amount = data[1];
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }


        System.out.println("You have entered: " + description + " and " + amount);

this should work and do the required job

1
  • 3
    The problem is not what classes you use to read the input. The problem is consoles (rather like text fields) are not designed to hold tabs
    – g00se
    Commented Jul 8 at 16:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.