简体   繁体   中英

I'm getting Resource Leaks

Whenever I run this code, it works pretty smoothly, until the while loop runs through once. It will go back and ask for the name again, and then skip String b = sc.nextLine();, and print the next line, instead.

static Scanner sc = new Scanner(System.in);

static public void main(String [] argv) {
    Name();
}

static public void Name() {

boolean again = false;
do
{
    System.out.println("What is your name?");

    String b = sc.nextLine();
    System.out.println("Ah, so your name is " + b +"?\n" +
            "(y//n)");
    int a = getYN();
    System.out.println(a + "! Good.");
    again = askQuestion();
} while(again);



}

static public boolean askQuestion() {
    System.out.println("Do you want to try again?");
    int answer = sc.nextInt();

    if (answer == 1) {
        return true;
    }
    else {
        return false;
    }

}

static int getYN() {
    switch (sc.nextLine().substring(0, 1).toLowerCase()) {
    case "y":
        return 1;
    case "n":
        return 0;
    default:
        return 2;
    }
}

}

Also, I'm trying to create this program in a way that I can ask three questions (like someone's Name, Gender, and Age, maybe more like race and whatnot), and then bring all of those answers back. Like, at the very end, say, "So, your name is + name +, you are + gender +, and you are + age + years old? Yes/No." Something along those lines. I know there's a way to do it, but I don't know how to save those responses anywhere, and I can't grab them since they only occur in the instance of the method.

Don't try to scan text with nextLine() AFTER using nextInt() with the same scanner! It may cause problems. Open a scanner method for ints only...it's recommended. You could always parse the String answer of the scanner.

Also, using scanner this way is not a good practice, you could organize questions in array an choose a loop reading for a unique scanner instantiation like this:

public class a {

    private static String InputName; 
    private static String Sex; 
    private static String Age; 
    private static String input; 
    static Scanner sc  ; 

    static public void main(String [] argv) {
        Name();
    } 

    static public void Name() {

      sc =  new Scanner(System.in);

       String[] questions = {"Name?","Age","Sex?"};//
       int a = 0; 
       System.out.println(questions[a]); 

      while (sc.hasNext()) {
          input = sc.next();
           setVariable(a, input);
            if(input.equalsIgnoreCase("no")){
                sc.close();
                break;
            }
            else if(a>questions.length -1)
            {
                a = 0;
            }
            else{
                a++;
            }
            if(a>questions.length -1){
                System.out.println("Fine " + InputName 
                        + " so you are " + Age + " years old and " + Sex + "." );
                Age = null;
                Sex = null;
                InputName = null;
                 System.out.println("Loop again?"); 

              }
               if(!input.equalsIgnoreCase("no") && a<questions.length){
              System.out.println(questions[a]);
              } 
        } 

    }


    static void setVariable(int a, String Field) {
        switch (a) {
        case 0:
            InputName = Field;
            return;
        case 1:
            Age = Field;
            return;
        case 2:
            Sex = Field; 
            return;
        }
    }
}

Pay attention on the global variables, wich stores your info until you set them null or empty...you could use them to the final affirmation.

Hope this helps! Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM