tags:

views:

42

answers:

2

I am a first year student and learning setter and getter at school. When I run it, it is ignoring the following statement:

String Origin = scan.nextLine();

and then it goes to the next line.

Here is the main:

import java.util.Scanner;

public class FlightTest
{
    public static void main (String [] args)
    {
        Scanner scan = new Scanner (System.in);
        Flight Flight1 = new Flight ();

        System.out.print ("Airline Name: ");
        String AirlineName = scan.nextLine ();

        System.out.print ("Flight Number: ");
        int FlightNumber = scan.nextInt ();

        System.out.print ("Origin: ");
        String Origin = scan.nextLine(); // There is a problem here

        System.out.print ("Destination: ");
        String Destination = scan.nextLine ();
    }   
}

This is the class

public class Flight
{
    private String AirlineName;

    private int FlightNumber;

    private String Origin;

    private String Destination;

    public String setAirlineName()
    {
        String Name = AirlineName;
        return Name;
    }

    public String getAirlineName()
    {
        return AirlineName;
    }

    public int setFlightNumber ()
    {
        int Number = FlightNumber;
        return Number;
    }

    public int getFlightNumber ()
    {
        return FlightNumber;
    }

    public String setOrigin ()
    {
        String Orig = Origin;
        return Orig;
    }

    public String getOrigin ()
    {
        return Origin;
    }

    public String setDestination ()
    {
        String Desti = Destination;
        return Desti;
    }

    public String getDestination ()
    {
        return Destination;
    }

    public String toString ()
    {
        String result = AirlineName + " flight number " 
                 + FlightNumber + " leaves from " + Origin + " to "
                 + Destination + ".";
        return result;
    }
}
+2  A: 

because scan.nextInt() doesn't use the whole line, the next call to scan.nextLine() is returning the end of the line that the flight number was on, which is probably just a newline.

Add another call to scan.nextLine()

System.out.print ("Flight Number: ");
int FlightNumber = scan.nextInt ();
scan.nextLine(); // get rid of rest of line

System.out.print ("Origin: ");
String Origin = scan.nextLine();

and I think things will work.

bemace
Thank you so much!! it works! :)
Woong-Sup Jung
bemace
How do I do it? Sorry, it's my first time using this website.
Woong-Sup Jung
@Woong-Sup Jung: The triangles above the number '2' (currently) vote up and down, clicking on the outline of the checkmark will accept this answer
bemace
A: 

and your setter is wrong, you should pass a parameter.

private String airlineName;
public String getAirlineName() {
    return airlineName;
}
public void setAirlineName(String airlineName) {
    this.airlineName = airlineName;
}

you can use setter like this

Flight f = new Flight ();

System.out.print ("Airline Name: ");
f.setAirlineName(scan.nextLine ());
icyfeel