-2

I'm trying to return the string "otheruser" from static void Answer2 to my main method.

public static void main(String[] args) {
    System.out.println("is there another person with you?");

    Scanner Answer2 = new Scanner(System.in);
    String answer2 = Answer2.nextLine();
    if (answer2.equals("yes")) {
        System.out.println("ok then type there name in please");
        Answer2();
    } else if (answer2.equals("no")) {
        System.out.println("ok then good day");
    }

    System.out.println("how old are you " + Answer2());
}

static void Answer2() {
    Scanner otheruser = new Scanner(System.in);
    String Otheruser = otheruser.nextLine();
    System.out.println("hi " + Otheruser);
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
Jimmy
  • 9
  • 1
  • 2
    Use return in the Answer2() method instead of returning void – jeprubio Mar 14 '20 at 10:27
  • 2
    Consider indenting your code readably if you would like people to try and read it. – khelwood Mar 14 '20 at 10:29
  • thanks @jeprubio and thanks for the tip khelwood im new to programming and want to be great at it, so please cut me some slack on my sloppiness sorry – Jimmy Mar 14 '20 at 10:39
  • You should avoid creating multiple scanners tied to the same resource. They can interfere with each other which can result in bad behavior (due to internal caching). Create one scanner and share it across the program. – Zabuzard Mar 14 '20 at 10:44
  • how exactly does using one scanner work? wouldnt it get confusing if i only use one? please explain further and thank you for your feedback – Jimmy Mar 15 '20 at 00:40

2 Answers2

1

Instead of printing the string you got from the Scanner, you could return it, so it's available to the rest of the program:

static String answer2() { 
    Scanner otheruser = new Scanner(System.in);
    String otherUser = otheruser.nextLine(); 
    return otherUser;
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
1

In Java it is common practice to name methods and variables using camel case naming convention

You can not get a return value from the function static void Answer2() because the return type is void, which means it doesn't return anything. You need to change the return type to String to get the value.

As mentioned by Zabuza in the comments, you should not create multiple scanners on the same stream.

Additionally, if you really want to use methods for this you can make them more generic, and use them to get any input.

import java.util.Scanner;

public class Main {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("is there another person with you?");
        String answer = getString();
        if (answer.equalsIgnoreCase("yes")) {
            System.out.println("ok then type their name in please");
            String otherUser = getString();
            System.out.printf("hi %s. how old are you?%n", otherUser);
            int age = getNumber();
            System.out.printf("%s is %d years old%n", otherUser, age);
        } else {
            System.out.println("ok then good day");
        }
    }

    static String getString() {
        return scanner.nextLine();
    }

    static int getNumber() {
        return scanner.nextInt();
    }
}
HomeIsWhereThePcIs
  • 869
  • 10
  • 28