Question

Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

Why does my program generate [] when viewing all guests names?


-----------------------------------------------
import java.util.*;

public class Delete extends Info {
    String name;
    int Id;
    int del;

    public Delete() {
    }
    public void display() {
        Scanner key = new Scanner(System.in);
        System.out.println("Input Customer Name: ");
        name = key.nextLine();
        System.out.println("Enter ID Number: ");
        Id = key.nextInt();
        System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. ");
        del = key.nextInt();
        if (del == 1) {
            int flag = 0;     //learned that exceptions were the best way to do this. Learned from book
            try {
                for (Object nameString : getList()) {

                    if (name.equalsIgnoreCase(name))// from array in info class, have to figure out how to get array from info
                    // class into this one.
                    {
                        flag = 1;
                        getList().remove(name);// also from the array in the info class

                    }
                }

                if (flag == 0) {
                    System.out.println("Incorrect name.");

                }
            } catch (Exception e) {
                System.out.println("List is empty");
            }
        } else {
            System.out.println("You have chosen not to delete.");
        }

    }
}

-------------------------------------------------

public class View extends Info {

    public View() {
        String dash = ("--------------------------------");
    }

    public void display() {
        System.out.println("Customer Log\n" + dash);
        Info info = new Info();
        System.out.println(info.getList());


    }

}

-------------------------------

import java.util.Date;
import java.util.*;

public class Info {
    String name;
    String dateOfBirth;
    String password;
    int menu;
    String idnumber = "0000000000";
    String deletion;
    final int IDEN_LENGTH = 9;
    final String BREAK_CODE = "XXX";
    int total = 0;
    final int smallSuite = 25;
    final int mediumSuite = 50;
    final int largeSuite = 75;
    String dash = ("----------------------------------");
     ArrayList names = new ArrayList();


    public void display() {
        Scanner input = new Scanner(System.in);
        Date date = new Date();
        String dec;
        do {
            System.out.println("Please answer the following Questions to Check In: ");
            names.add("John");
            System.out.println("Enter your Full Name: ");

            name = input.nextLine();
            names.add(name);
            System.out.println("Date of Birth 8 digits in m/d/y format: ");
            dateOfBirth = input.nextLine();

            System.out.println("Enter your Identification Card Number: ");
            idnumber = input.nextLine();
            if (idnumber.length() != IDEN_LENGTH) {
                System.out.println("Invalid Length");
                display();
            } else {
                System.out.println("IdNumber is set.");
            }
            System.out.println("Enter your E-Signature: ");
            password = input.nextLine();
            if (password.equalsIgnoreCase(name)) {
                System.out.println("E signature accepted.");
                System.out.println(dash);
                String insert = dateOfBirth.substring(0, 2) + "/" + dateOfBirth.substring(2, 4) + "/"
                        + dateOfBirth.substring(4, 8);
                String idInsert = "(" + idnumber.substring(0, 3) + ")" + " " + idnumber.substring(3, 5) + "-"
                        + idnumber.substring(6, 9);
                // JOptionPane.showMessageDialog()
                System.out.println("Your information:");
                System.out.println("Name: " + name);
                System.out.println(" Date Of Birth: " + insert);
                System.out.println(" ID#: " + idInsert);
                System.out.println(" E Signature: " + password);
                System.out.println();
                System.out.println(dash);
                System.out.println(" Suite Package Options ");
                System.out.println(" #1 Small (1-2 people): $25 ");
                System.out.println(" #2 Medium (1-4 people): $50 ");
                System.out.println(" #3 Large: (1-8 people)($75 ");
                System.out.println(" Enter Option Number: ");
                int packageOption = input.nextInt();
                input.nextLine();
                if (packageOption != 0) {
                    if (packageOption == 1) {
                        total += smallSuite;
                        System.out.println("You have selected a small suite.");
                    }
                    if (packageOption == 2) {
                        total += mediumSuite;
                        System.out.println("You have selected a medium suite.");
                    }
                    if (packageOption == 3) {
                        total += largeSuite;
                        System.out.println("You have selected a large suite.");
                    }
                    System.out.println("Your current total is $" + total);
                } else if (packageOption != 1 | packageOption != 2 | packageOption != 3) {
                    System.out.println("Please choose a valid package number.");
                }
            } else {
                System.out.println("Signature does not match the name. Try again.");
                display();
            }
            System.out.println("Add another visitor? Y/N");
            dec = input.nextLine();
        }while(!dec.equalsIgnoreCase("N"));
    }

    public String returnArray() {
        return (name);
    }

    public ArrayList getList() {
         for(int x = 0; x < names.size(); ++x)
             System.out.println("position " + x + " Name: " + names.get(x));
        return names;
    }

}

------------------------------

import java.util.*;

public class Main {
    public static void main(String[] args) {
        String dash = ("----------------------------------");
        Scanner input = new Scanner(System.in);
        Date date = new Date();
        String option;
        System.out.println(date);
        do {
            System.out.println("Welcome to the Hotel\n" + dash);
            System.out.println("1. Add A Visitor\n2. View all Visitors\n3. Delete a Visitor");
            System.out.println("Choose option Number: ");
            int menu = input.nextInt();
            input.nextLine();
            if (menu == 1) {
                Info in = new Info();
                in.display();
            }
            if (menu == 2) {
                View v = new View();
                v.display();
            }
            if (menu == 3) {
                Delete del = new Delete();
                del.display();
            } else if (menu != 1 && menu != 2 && menu != 3) {
                System.out.println("Menu number does not exist.");
            }
            System.out.println("Do you want to continue!(Y/N)");
            option = input.nextLine();
        }while(!option.equalsIgnoreCase("N"));
    }
}



0 0
Add a comment Improve this question Transcribed image text
Answer #1

-------Main.java-------------

import java.util.*;

public class Main {
   static int count=1;//required to create the ArrayList instance only once in Info class
public static void main(String[] args) {
String dash = ("----------------------------------");
Scanner input = new Scanner(System.in);
Date date = new Date();
String option;
  
System.out.println(date);
do {
System.out.println("Welcome to the Hotel\n" + dash);
System.out.println("1. Add A Visitor\n2. View all Visitors\n3. Delete a Visitor");
System.out.println("Choose option Number: ");
int menu = input.nextInt();
input.nextLine();
if (menu == 1) {
Info in = new Info();
in.display();
count--;
}
if (menu == 2) {
View v = new View();
v.display();
}
if (menu == 3) {
Delete del = new Delete();
del.display();
} else if (menu != 1 && menu != 2 && menu != 3) {
System.out.println("Menu number does not exist.");
}
System.out.println("Do you want to continue!(Y/N)");
option = input.nextLine();
}while(!option.equalsIgnoreCase("N"));
}
}

-------Info.java------------


import java.util.*;

public class Info {
String name;
String dateOfBirth;
String password;
int menu;
String idnumber = "0000000000";
String deletion;
final int IDEN_LENGTH = 9;
final String BREAK_CODE = "XXX";
int total = 0;
final int smallSuite = 25;
final int mediumSuite = 50;
final int largeSuite = 75;
String dash = ("----------------------------------");
static ArrayList<String> names;//made static to reference in any other java class

public Info() {

//initialize ArrayList only once
   if(Main.count==1)
   names=new ArrayList<String>();
}
public void display() {
Scanner input = new Scanner(System.in);
Date date = new Date();
String dec;
do {
System.out.println("Please answer the following Questions to Check In: ");
names.add("John");
System.out.println("Enter your Full Name: ");

name = input.nextLine();
names.add(name);
System.out.println("Date of Birth 8 digits in m/d/y format: ");
dateOfBirth = input.nextLine();

System.out.println("Enter your Identification Card Number: ");
idnumber = input.nextLine();
if (idnumber.length() != IDEN_LENGTH) {
System.out.println("Invalid Length");
display();
} else {
System.out.println("IdNumber is set.");
}
System.out.println("Enter your E-Signature: ");
password = input.nextLine();
if (password.equalsIgnoreCase(name)) {
System.out.println("E signature accepted.");
System.out.println(dash);
String insert = dateOfBirth.substring(0, 2) + "/" + dateOfBirth.substring(2, 4) + "/"
+ dateOfBirth.substring(4, 8);
String idInsert = "(" + idnumber.substring(0, 3) + ")" + " " + idnumber.substring(3, 5) + "-"
+ idnumber.substring(6, 9);
// JOptionPane.showMessageDialog()
System.out.println("Your information:");
System.out.println("Name: " + name);
System.out.println(" Date Of Birth: " + insert);
System.out.println(" ID#: " + idInsert);
System.out.println(" E Signature: " + password);
System.out.println();
System.out.println(dash);
System.out.println(" Suite Package Options ");
System.out.println(" #1 Small (1-2 people): $25 ");
System.out.println(" #2 Medium (1-4 people): $50 ");
System.out.println(" #3 Large: (1-8 people)($75 ");
System.out.println(" Enter Option Number: ");
int packageOption = input.nextInt();
input.nextLine();
if (packageOption != 0) {
if (packageOption == 1) {
total += smallSuite;
System.out.println("You have selected a small suite.");
}
if (packageOption == 2) {
total += mediumSuite;
System.out.println("You have selected a medium suite.");
}
if (packageOption == 3) {
total += largeSuite;
System.out.println("You have selected a large suite.");
}
System.out.println("Your current total is $" + total);
} else if (packageOption != 1 | packageOption != 2 | packageOption != 3) {
System.out.println("Please choose a valid package number.");
}
} else {
System.out.println("Signature does not match the name. Try again.");
display();
}
System.out.println("Add another visitor? Y/N");
dec = input.nextLine();
}while(!dec.equalsIgnoreCase("N"));
}

public String returnArray() {
return (name);
}

public static ArrayList getList() { //made static to directly reference in other classes
for(int x = 0; x < names.size(); ++x)
System.out.println("position " + x + " Name: " + names.get(x));
return names;
}

}

--------------------View.java----------------

public class View extends Info {

public View() {
String dash = ("--------------------------------");
}
@Override
public void display() {
System.out.println("Customer Log\n" + dash);
//Info info = new Info();
System.out.println(Info.getList());//directly calls the function with class name


}

}

Problem with the code is "Info info = new Info();" this particular statement. Since you was creating the new object of Info class even the ArrayList also used to be initialized to null, which means there is no data to print it. Even after making the suggested changes if you method with object reference it won't work, as it will assume it as different object itself.

Add a comment
Know the answer?
Add Answer to:
Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util....

    Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Role { String user, password, role; public Role(String user, String password, String role) { super(); this.user = user; this.password = password; this.role = role; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** *...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    //...

    Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    // checks customers in and assigns them a boarding pass    // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers    //    public void reserveSeats()    {       int counter = 0;       int section = 0;       int choice = 0;       String eatRest = ""; //to hold junk in input buffer       String inName = "";      ...

  • Identify a logical error in the following code and fix it. public class test1 {    ...

    Identify a logical error in the following code and fix it. public class test1 {     public static void main(String[] args){         int total;         float avg;         int a, b, c;         a=10;         b=5;         c=2;         total=a+b+c;         avg=total/3;         System.out.println("Average: "+avg);     } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT