3

So i am working on an assignment for class and after i went through and fixed all of the errors the compiler found i get this when running the program:

  J:\Java Class>java AddressDemo

   What is your Street's name?     Unknown RD

   What is your zip code?  1234565

   What is your State?
   What is your house number?

It is asking both questions at the same time and waiting 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's input

        System.out.println();
        System.out.print("What is your Street's 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 file required:

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);
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
EpOcH91
  • 47
  • 2
  • 5

4 Answers4

3

When you use input.nextInt(), it does not consume the newline character that ends the input. The result is that when you prompt for the next input (the state), it immediately reads the leftover newline, assigns the empty string to State, and prompts for the house number. You should instead use input.nextLine() to read the zip code (and, later, the house number) and parse the line as an int. Alternatively, call nextLine() and ignore the return value after calling nextInt().

I'd like to suggest that the zip code should probably be just a string anyway so that you preserve leading zeros and can handle zip+4 addresses. Likewise the house number should be a string to handle addresses like 221b Baker Street. That will, of course, require changing your Address class definition as well as changing how you read the input.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

You should use input.nextLine() after using input.nextInt() in order to consume the remaining newline character in the buffer. If you use input.nextLine() and try to parse it directly to int, you may get an exception if the line is not an integer, so you would need to catch an exception. You can do this instead:

System.out.println();
System.out.print("What is your zip code?\t");
Zip = input.nextInt();
input.nextLine();
Gabriel Espinel
  • 358
  • 2
  • 9
1

When you type the zip code, let's say 12345, you also enter a carriage return. input.nextInt(); eats 12345, the following input.nextLine(); eats the carriage return, that's why your program jumps to the house number.

Here is 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");
ToYonos
  • 16,469
  • 2
  • 54
  • 70
1

First:

in your Address class:

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

there no need for the boolean return, since you're not using that return value anywhere. So your functions should look like this instead: (void means there is no return value).

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

}

Second:

System.out.println() will print out what you give it, then add a line break at the end, while System.out.print() 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");

Will output:

    Hello1
    Hello2Hello3Hello4Hello5
    Hello6
    Hello7Hello8

--

Third:

And this is why I really hate the fact that Java is taught in this fashion to beginner programmers, is that reading input from the console isn't that simple, because Scanner.nextInt() isn't that simple.

Ok, so...

Scanner.nextLine() reads a string from the input console, until it hits a "new line" symbol (aka, you hitting the enter key), and it eats up that new line symbol in the process. But Scanner.nextInt() only tries to read a number, and stops when it encounters something that's not a number (like a "new line" character), and leaves it in the input buffer.

your Scanner.nextInt() call works, but your next nextLine() call never gets any input BECAUSE the nextInt() call took all the numbers from the System.in , but it didn't take out the new line. Your following nextLine() call only looks for the next new line character, and it finds it right away...

Here's the quick and super filthy solution:

After every Scanner.nextInt() add an extra Scanner.nextLine() to dump out the new line.

The less quick but slightly less dirty solution:

System.in consists of Strings of characters as text. so don't jump the gun, don't immediately assume the text you're receiving is a number... treat it as text first, then check to see if it's a number...

In your example:

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

Let's do this:

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

input.nextLine() will give you the keyboard input as a String up to when the user pressed Enter.

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


bon courage :)

schuttek
  • 156
  • 1
  • 10