Home > Software design >  When having String and int as an Output - how do I print string first?
When having String and int as an Output - how do I print string first?

Time:01-03

this code doesn't work, whenever I as an user input String data first and then int data, it just accepts input and doesnt print out the data. If I change String name = input.nextLine(); int age = input.nextInt(); position of this two code blocks, and enter int first and String as second value after then it happily prints first int number and then String. Please help out how to solve it. I want to be able Name and Family name first and then I want to have age. Thank you, regards Aka

package package1;

import java.util.Scanner;

public class experiment {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            System.out.println("Please enter your age and name: ");

            String name = input.nextLine();
            int age = input.nextInt();

            System.out.println("Your age is: "   age);
            System.out.println("Your name is: "   name);
        } finally {
            input.close();
        }
    }

}

CodePudding user response:

Did you tried using input.next() instead of input.nextLine();

CodePudding user response:

First things first: welcome to StackOverflow :D

Your Problem

It seems like you're providing your input the wrong way. I tested it locally and it works just fine, but you have to be careful about the way you input data. nextLine() is going to read from the StandardOutput until it reaches a \n aka new-line character. On the other hand nextInt() is going to read the next Integer until it reaches a space. So what happens is: you input your data all in one line (f.e. stackoverflow 2022) sepparated by a space and hit enter. This will get saved in your variable name. Then, the console waits for your integer input. If you input some number, it will successfully print out the name (stackoverflow 2022) and age.

Solution

If you just want your code to work:

Hit enter after you input the name and then type the number: your code works!

If you 100% must provide the input in a single line:

  1. Use next() instead of nextLine(). That will read until a space, but be careful though: if the names you input contain spaces, the rest will be interpreted as integer and you will probably run into InputMismatchException-s

General Recommendations:

You could try making your programm more interactive by asking the user to type their name and age specifically.

Another important change would be some better Exception-Handling, for example when the user is prompted to type an integer, but he/she provides a String.

Feel free to discuss your changes/ problems/ ideas/ thoughts in the comments :D

CodePudding user response:

separate name and age declaration, to more understandably. and you can write try with resourses

    try (Scanner input = new Scanner(System.in)) {
        
        System.out.println("Please enter your name: ");
        String name = input.nextLine();

        System.out.println("Please enter your age: ");
        int age = input.nextInt();

        System.out.println("Your age is: "   age);
        System.out.println("Your name is: "   name);
    }
  • Related