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"));
}
}
-------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.
Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...
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));...
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 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 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 { // 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 = ""; ...
Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...
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...
Trying to of-else statement to sort 3 strings . import java.util.*; public class test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String w1 = "", w2 ="", w3 = ""; String top = null, mid = null, bot = null; System.out.println("Please enter the first word"); w1 = scan.next(); System.out.println("Please enter the Second word"); w2 = scan.next(); System.out.println("Please enter...
Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 { public static void main(String[] args)throws IOException{ File prg8 = new File("program8.txt"); Scanner reader = new Scanner(prg8); String cName = ""; int cID = 0; double bill = 0.0; String email = ""; double nExempt = 0.0; String tExempt = ""; int x = 0; int j = 1; while(reader.hasNextInt()) { x = reader.nextInt();} Customers c1 [] = new Customers [x]; for (int...
Rewrite following code down below using Factory Pattern. -------------------------Staff.java--------------------------- import java.util.*; public class Staff extends Employee { private int HourlyRate; /**Constructor Staff which initiates the values*/ public Staff() { super(); HourlyRate=0; } /**Overloaded constructor method * @param ln last name * @param fn first name * @param ID Employee ID * @param sex Sex * @param hireDate Hired Date * @param hourlyRate Hourly rate for the work...