System.out.print prints two lines / questions at the same time. What am I doing wrong?

So I am working on assignment for a class, and after I have gone through and fixed all the errors the compiler has found, I get this when I run the program:

  J:\Java Class>java AddressDemo

   What is your Street name?     Unknown RD

   What is your zip code?  1234565

   What is your State?
   What is your house number?

      

It asks both questions at the same time and waits for input.

Here is my code:

import java.util.Scanner;

public class AddressDemo
{
    public static void main(String[] args)
    {
        Address address = new Address();
        Scanner input = new Scanner(System.in);

        String Street;
        int Zip;
        String State;
        String City;
        int house;

        // Get the student input

        System.out.println();
        System.out.print("What is your Street name?\t");
        Street = input.nextLine();

        System.out.println();
        System.out.print("What is your zip code?\t");
        Zip = input.nextInt();

        System.out.println();
        System.out.print("What is your State?\t");
        State = input.nextLine();

        System.out.println();
        System.out.print("What is your house number?\t");
        house = input.nextInt();

        System.out.println();
        System.out.print(" What is your city?\t");
        City = input.nextLine();


        // Processing
        address.setStName(Street);
        address.setZNum(Zip);
        address.setSName(State);
        address.setCName(City);
        address.setHNum(house);


        //Output isn't finished
        System.out.println("\nYour address is:" + address.getHNum() + address.getStName() + address.getCName() +address.getSName()
                                        + address.getZNum() + " !");
        }
}

      

and here is my code for the other required file:

public class Address
{
    private int HNum;
    private String StName;
    private String CName;
    private String SName;
    private int ZNum;
    private String LNum;

    public boolean setHNum (int hn)
    {
        HNum = hn;
        return true;
    }

    public String getHNum()
    {
        return String.format("%08d", HNum);
    }

    public boolean setStName(String str)
    {
        StName = str;
        return true;
    }

    public String getStName()
    {
        return StName;
    }

    public boolean setCName(String City)
    {
        CName = City;
        return true;
    }

    public String getCName()
    {
        return CName;
    }

    public boolean setSName(String st)
    {
        SName = st;
        return true;
    }

    public String getSName()
    {
        return SName;
    }

    public boolean setZNum ( int zip)
    {
        ZNum = zip;
        return true;
    }

    public String getZNum()
    {
        return String.format("%08d", ZNum);
    }

    public boolean setLNum (String l2)
    {
        LNum = l2;
        return true;
    }

    public String getLNum()
    {
        return String.format("%08d", LNum);
    }

}

      

+3


source to share


4 answers


When you use input.nextInt()

it does not use the newline character that ends the input. As a result, when you ask for the next input (state), it immediately reads the remaining newline, assigns an empty string, State

and asks for the house number. It should be used instead input.nextLine()

to read the postal code (and then the house number) and parse the string like int

. Alternatively, call nextLine()

and ignore the return value after the call nextInt()

.



I would like to assume that the zip code should probably just be a string, so that you keep the leading zeros and be able to handle zip + 4 addresses. Likewise, the house number must be a string to handle addresses such as 221b Baker Street. This will, of course, require changing the class definition Address

as well as changing the way the input is read.

+3


source


You must use input.nextLine()

after use input.nextInt()

to use the remaining newline character in the buffer. If you use input.nextLine()

and try to parse it directly to int, you might get an exception if the string is not an integer, so you will need to catch the exception. You can do this instead:



System.out.println();
System.out.print("What is your zip code?\t");
Zip = input.nextInt();
input.nextLine();

      

+1


source


When you enter a zip code, say 12345, you also enter a carriage return. input.nextInt (); eats 12345, next input is .nextLine (); eats a carriage return, so your program jumps to the house number.

Here's the solution:

        System.out.print("What is your zip code?\t");
        Zip = input.nextInt();
        input.next line();

        System.out.println();
        System.out.print("What is your State?\t");
        State = input.nextLine();

        System.out.println();
        System.out.print("What is your house number?\t");

      

+1


source


First:

in your Address class:

public boolean setStName(String str)
{
    StName = str;
    return true;
}

      

there is no need for a boolean return since you are not using that return value anywhere. Therefore your functions should look like this: (void means there is no return value).

public void setStName(String str)
{
    StName = str;

      

}

Second:

System.out.println () will print what you give it and then add a line break at the end and System.out.println () will just print what you give it ... Consider this:

    System.out.println("Hello1");
    System.out.print("Hello2");
    System.out.print("Hello3");
    System.out.print("Hello4");
    System.out.println("Hello5");
    System.out.println("Hello6");
    System.out.print("Hello7");
    System.out.println("Hello8");

      

The output will be:

    Hello1
    Hello2Hello3Hello4Hello5
    Hello6
    Hello7Hello8

      

-

Thirdly:

And that's why I really hate the fact that Java is taught this way to novice programmers, is that reading input from the console is not that easy because Scanner.nextInt () is not that easy.

Okay, so ...

Scanner.nextLine () reads a line from the input console until it hits a newline character (otherwise, you press enter), and it eats that new line character in the process. But Scanner.nextInt () only tries to read a number and stops when it encounters something that is not a number (like a newline character) and leaves it in the input buffer.

your call to Scanner.nextInt () works, but your next call to nextLine () never receives any input. BECAUSE the nextInt () call took all the numbers from System.in, but did not output a new line. Your next call to nextLine () only looks for the next new line character and finds it right away ...

Here's a quick and super dirty solution:

After each Scanner.nextInt () add an additional Scanner.nextLine () to unload a new line.

Less fast, but slightly less messy solution:

System.in consists of strings of characters as text. so don't jump the gun, don't immediately accept the text you get is that number ... first treat it as text and then check if it's a number ...

In your example:

    System.out.println();
    System.out.print("What is your house number?\t");
    house = input.nextInt();

      

Do it:

    System.out.println("What is your house number?");
    String houseNumberString = input.nextLine();
    house = Integer.parseInt(houseNumberString);

      

input.nextLine () will give you keyboard input as a string before the user hits Enter.

Integer.parseInt () will try to decode the String text and convert it to an integer.


courage:)

+1


source







All Articles