Skip to content
Advertisement

Java Question on how to cancel system input

I want to print last message when user inputs q but it gives an error.


Language Translator Program

Please enter the input sentence (press q to exit):

Robin came to Montreal, Canada in 2009.

Robin stays in Montreal for 11 years. Montreal is in Canada.

Please enter the input sentence (press q to exit):

Lucy came to Berlin, Germany in 2000.

Lucy stay in Berlin for 20 years. Berlin is in Germany.

Please enter the numbers along operation (press q to exit):

q

Thanks for using translator program.

import java.util.Scanner;

public class MiniTranslator {
    public static void main(String[] args) {
        String message ="";
        do {
            //Declaring scanner
            Scanner keyboard = new Scanner(System.in);

            //Printing greeting message

            System.out.println("---------------------------------------------------------n" 
                                 +"tLanguage Translator Programn"
                                +"---------------------------------------------------------");

            System.out.println("Please enter the input sentence (press q to exit): n");
            message = keyboard.nextLine();
            String[] arr = message.split("[ ,.]+",8);
            String name = arr[0];
            String city = arr[3];
            String country = arr[4];
            String year = arr[6];

            int noOfYears =2020 - (Integer.parseInt(year));

            System.out.println("n" + name +" stay in " + city +" for " + noOfYears +" years. " 
                                + city +" is in " + country +".");
            System.exit('q');
            keyboard.close();
        }
        while(message!="q");
        if(message =="q") {
            System.out.println("Thanks for using translator program");
        }
    }
}

Advertisement

Answer

Ok, you have a number of immediate issues

message!="q" is not how you compare Strings in Java, you should be using !"q".equals(message) instead (same with ==, which would become "q".equals(message) for example)

But, your main problem is here …

System.out.println("Please enter the input sentence (press q to exit): n");
message = keyboard.nextLine();
String[] arr = message.split("[ ,.]+",8);
String name = arr[0];
String city = arr[3];
String country = arr[4];
String year = arr[6];

int noOfYears =2020 - (Integer.parseInt(year));

You’ve made an assumption on what message contains, instead of taking the time to ensure if contains what you think.

So, when message is q, your array will contain only one element, so as soon as you do String city = arr[3]; you have an array index out of bounds issue.

A “simple” solution might be to check if message is equal to q before you split, but a better solution would be to check the number of elements in array…

if (arr.length == 7) {
    // Process the result
} else if (!"q".equals(message)) {
    // Invalid input
}

Also, as has been stated, you should create your Scanner once, outside of the loop and NOT close it – as you’d terminate the undying system input stream as well, which isn’t pretty

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement