0

I was following the Moocfi exercise 8 (java online course), and I'm unable to debug my programming code. https://materiaalit.github.io/2013-oo-programming/part2/week-7/ (The question I am asking is on the very bottom of the page, named "airport")

Airport.java

import java.util.Scanner;
import java.util.HashMap;
import java.util.ArrayList;

public class Airport { 

    private HashMap<Plane, Flight> num;
    private Scanner read;
    private ArrayList<Plane> planes;

    public Airport(){
    read = new Scanner(System.in);
    this.num = new HashMap<Plane, Flight>();
    planes = new ArrayList<Plane>();

    }


    public void start(){

        while(true){
        System.out.print("Choose operation:\n[1] Add airplane" + "\n[2] Add flight\n[x] Exit\n> ");
        String ans = read.nextLine();
        if (ans.equals("1")){
            num1();

        } else if (ans.equals("2")){
            num2();

        } else if (ans.equals("x")){
            break;
        } else continue;
        }

        System.out.println("\nFlight service\n------------\n");

        while(true){

        System.out.print("Choose operation:\n[1] Print planes\n"
                + "[2] Print flights\n[3] Print plane info\n[x] Quit\n> ");
        String ans2 = read.nextLine();
        if (ans2.equals("1")){
            nus1();

        }
        else if (ans2.equals("2")){
            nus2();

        }
        else if (ans2.equals("3")){
            nus3();

        } 
        else if (ans2.equals("x")){
            break;
        }
        else continue;
        }
    }

    public void num1(){
        System.out.print("Give plane ID: ");
        String pi = read.nextLine();
        System.out.print("Give plane capacity: ");
        int pin = read.nextInt();
        planes.add(new Plane(pi, pin));
        System.out.println("");
    }

    public void num2(){
        System.out.print("Give plane ID: ");
        String pi = read.nextLine();
        System.out.print("Give departure airport code: ");
        String pin = read.nextLine();
        System.out.print("Give destination airport code: ");
        String pim = read.nextLine();
        for (Plane a:planes){
            if (a.getName()==pi){
                Plane b = new Plane(a.getName(), a.getCap());
                num.put(b, new Flight(pin, pim));
            }
        }
        System.out.println("");
    }

    public void nus1(){
        for (Plane p: planes){
            System.out.println(p.getName() + " (" + p.getCap() + " ppl)");            
        }
        System.out.println("");
    }

    public void nus2(){
        for (Plane p : num.keySet()){
            System.out.println(p.getName() + " (" + 
                    p.getCap() + " ppl) " + num.get(p));
        }
        System.out.println("");
    }

    public void nus3(){
        System.out.print("Give plane ID: ");
        String b = read.nextLine();
        for (Plane a: planes){
            if (a.getName().equals(b)){
                System.out.println(a.getName() + " (" + a.getCap() + " ppl)");
            }
            System.out.println("");
        }
    }


}

Plane.java

public class Plane {
    private String name;
    private int capacity;

    public Plane(String name, int capacity){
        this.name = name;
        this.capacity = capacity;
    }

    public String getName(){
       return name;
    }

    public int getCap(){
       return capacity;
    }
}

Flight.java

public class Flight {
    private String departureCode;
    private String destinationCode;

    public Flight(String departureCode, String destinationCode){
        this.departureCode = departureCode;
        this.destinationCode = destinationCode;
    }

    public String toString(){
        return "(" + departureCode + "-" + destinationCode + ")";
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        Airport a = new Airport();
        a.start();
        // Write your main program here. Implementing your own classes will be useful.
    }
}

There are two problems I want to figure out:

  1. When I run the program and press 1 and save the plane information, it then prints out the following statement twice, which should have been just once.
System.out.print("Choose operation:\n[1] Add airplane" + "\n[2] Add flight\n[x] Exit\n> ");
  1. Also, on Airport.java, I saved the flight information in a HashMap variable to print out later, but it doesn't seem to work when I run the program.
Draken
  • 3,049
  • 13
  • 32
  • 49
able20
  • 157
  • 1
  • 1
  • 7
  • The programming is intended to break out of the loop only when "x" is entered, so I wanted it to continue asking until "x" is entered. – able20 Dec 30 '19 at 17:34

1 Answers1

1

It is the issue with nextInt(), it wouldn't consider the newline when you press enter. Use nextLine() and cast it to int

In num1() method, replace

int pin = read.nextInt();

with

int pin = Integer.parseInt(read.nextLine());

Coming to your HashMap problem, you are comparing strings using ==. Use equals

In num2() method, replace

if (a.getName() == pi)

with

if (a.getName().equals(pi))
Sunil Dabburi
  • 1,394
  • 10
  • 15
  • Thank you so much. If you don' mind, can you please explain what is the fundamental difference between using nextInt and casting nextLine into integer? – able20 Dec 30 '19 at 17:50
  • 1
    Refer this answer: https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo – Sunil Dabburi Dec 30 '19 at 17:51