0

I want my program to loop through the options (1,2,3) over and over again so I can perform the functions several times. However, after I type the input for one option, the program just terminates. How can I make it loop through multiple times without terminating?

This is sample input and output at the moment:

1 - Input

2 - Look up

3 - Blacklist

3

Enter Blacklisted IPA: 232

(Then terminates)

import java.util.*;

public class Main {
public static void main(String[] args) {

    DNS a = new DNS();

    String domain;
    String ipa;

    System.out.printf("1 - Input\n");
    System.out.printf("2 - Look up\n");
    System.out.printf("3 - Blacklist\n");

    Scanner scan = new Scanner(System.in);
    while ( scan.hasNextInt() ) {
        int option = scan.nextInt();

            if(option == 1){

                System.out.printf("Enter Domain & IPA: ");
                Scanner scan1 = new Scanner(System.in);
                    String theLine = scan1.nextLine();
                    String[] split = theLine.split(" ");
                    domain = split[0];
                    ipa = split[1];
                    a.input(domain, ipa);
                    scan1.close();

            } else if (option == 2){

                System.out.printf("Enter Domain: ");
                Scanner scan2 = new Scanner(System.in);
                    String theLine = scan2.nextLine();
                    domain = theLine;
                    ipa = a.lookup(domain);
                    if(ipa.equals("null")){
                        System.out.printf("There is no IPA for %s\n", domain);
                    } else {
                        System.out.printf("The IPA for %s is %s\n", domain, ipa);
                    }
                    scan2.close();

            } else if (option == 3){

                System.out.printf("Enter Blacklisted IPA: ");
                Scanner scan3 = new Scanner(System.in);
                    String theLine = scan3.nextLine();
                    ipa = theLine;
                    a.blacklist(ipa);
                    scan3.close();

            } else {
                System.out.printf("Incorrect command input\n");
            }
    }
    scan.close();
}
}       
  • Looks like once you get input, there is no more next integer... one solution would be to switch the structure to a while(true), and provide a "stop" facility? – D. Ben Knoble Oct 16 '16 at 03:52
  • Check - http://stackoverflow.com/a/19950797/3896066, it contains a similar code example. – saurav Oct 16 '16 at 05:42

3 Answers3

1

Don't use loop in reading options, use switch-case instead. you can't use loop to iterate the printf output :)

    Scanner scan = new Scanner(System.in);
    int option = scan.nextInt();
    switch(option){
        case 1:{//input your argument 1 here}break;
        case 2:{//input your argument 2 here}break;
        case 3:{//input your argument 3 here}break;
    }
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
1

Don't create more than one Scanner with System.in, since if you close the Scanner, you risk closing System.in itself and messing up all such Scanner function.

Instead use one and only one Scanner constructed with System.in, and share it in your program where needed, and close it only when the program is fully done with all use of it.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
-1

Try this instead :

import java.util.Scanner;
class Main {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
int option;
while(((option=s.nextInt())<4)&&(option>0))
{
if(option == 1){
System.out.printf("Enter Domain & IPA: ");
Scanner scan1 = new Scanner(System.in);
String theLine = scan1.nextLine();
String[] split = theLine.split(" ");
domain = split[0];
ipa = split[1];
a.input(domain, ipa);
scan1.close();
}
else if(option == 2){

System.out.printf("Enter Domain: ");
Scanner scan2 = new Scanner(System.in);
String theLine = scan2.nextLine();
domain = theLine;
ipa = a.lookup(domain);
if(ipa.equals("null")){
System.out.printf("There is no IPA for %s\n", domain);
} else {
System.out.printf("The IPA for %s is %s\n", domain, ipa);
}
scan2.close();
}
else if(option == 3){
System.out.printf("Enter Blacklisted IPA: ");
Scanner scan3 = new Scanner(System.in);
String theLine = scan3.nextLine();
ipa = theLine;
a.blacklist(ipa);
scan3.close();
}
else
System.out.printf("Incorrect command input\n");
}
}
}
Shree Naath
  • 397
  • 2
  • 3
  • 15