Artifacialic
Artifacialic

Reputation: 51

user input name times age

so I need to make the following: when a user types their name in the console then gets prompted for filling in their age, after that it needs to display the name the amount if times their age is. So for example User input is Mikey age input is: 4 then console prints:
Mikey
Mikey
Mikey
Mikey

So far I made the following code:

 public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String invoer;
        String invoer2;
        System.out.print("Fill in your name:");
        invoer = br.readLine();
        System.out.print("Fill in your age:");
        invoer2 = br.readLine();
        System.out.print("" + invoer);
        System.out.print(" " + invoer2);
    }

I'm very new to java so I'm not sure what the problem is nor what the fix could be. I have been searching for such code to see what I'm doing wrong but I can't seem to find any.

Upvotes: 1

Views: 390

Answers (2)

xenteros
xenteros

Reputation: 15842

 public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String invoer;
    String invoer2;
    System.out.print("Fill in your name:");
    invoer = br.readLine();
    System.out.print("Fill in your age:");
    invoer2 = br.readLine();
    try {
        for (int i = 0; i < Integer.parseInt(invoer2); i++) {
            System.out.println(invoer);
        }
    } catch (NumberFormatException e) {
        System.out.println("Age should be a number");
        e.printStackTrace();
    }
    System.out.print("" + invoer);
    System.out.print(" " + invoer2);
}

You have get number from user's input. This can be done with Integer.parseInt(invoer2). However it can throw NumberFormatException if the input isn't a valid number.

However I would recommend using Scanner.

 public static void main(String[] args) throws IOException {
    Scanner s = new Scanner(System.in);
    String invoer;
    String invoer2;
    System.out.print("Fill in your name:");
    invoer = s.next();
    System.out.print("Fill in your age:");
    try {
        invoer2 = s.nextInt();
        for (int i = 0; i < invoer2; i++) {
            System.out.println(invoer);
        }
    } catch (InputMismatchException e) {
        System.out.println("It wasn't valid age");
    }
    System.out.print("" + invoer);
    System.out.print(" " + invoer2);
}

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

You need to use a loop for printing the name once for each year of age. But first, convert the string input for age into an int which can then be used as a boundary for the loop.

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String invoer;
    int invoer2;
    System.out.print("Fill in your name:");
    invoer = br.readLine();
    System.out.print("Fill in your age:");
    invoer2 = Integer.parseInt(br.readLine());
    for (int i=0; i < invoer2; ++i) {
        System.out.print(invoer);
    }
}

Upvotes: 0

Related Questions