Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:04:51+00:00 2026-05-27T23:04:51+00:00

I have a search function within my program that looks for the name or

  • 0

I have a search function within my program that looks for the name or a person within my arraylist. This can find the name of the person, but I am unable to get the other information I need. I’m guessing it’s because I have not declared something. I am uncertain of how to proceed.

First class which holds my methods.

public String searchName(String guest)
{
  for (Booking s : bookings)
  {
      if (s.getGuest().equals(guest))
        return guest + " is in room " + roomID + "\n"; 
  }
  return guest + " is not booking in at the hostel";
}

My Second class is my GUI that calls the methods

else if (item.equals("Find Room"))
{
  String name = JOptionPane.showInputDialog(this, 
                    "Enter Guests Name",
                    "Find Room",
                    JOptionPane.QUESTION_MESSAGE);   
  output.setText( hostel.searchName(name));
}

I am able to search for a persons name and if it’s correct it will be found and draw the message into my JTextArea successfully. It will not display the roomID though, it just comes up with null. I’m confused I get no error messages and null is normaly associated with ints not Strings?

Full Code if needed GUI CLASS

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JFrame implements ActionListener 
{
 private HostelFile hostel;
 private JPanel textOutput;
 private JScrollPane scroller;
 private JButton listFreeButton = new JButton("List Free Rooms");
 private JButton newBookingButton = new JButton("New Booking");
 private JButton cancelBookingButton = new JButton("Cancel Booking");
 private JButton listAllButton = new JButton("List All Bookings");   
 private JButton allRoomsButton = new JButton("List All Room Details");
 private JButton findRoomButton = new JButton("Find Room");
 private JButton exitButton = new JButton("Exit System");
 private JTextArea output = new JTextArea(30,30);

 public GUI(String hostelName)
 {
     super(hostelName);
     hostel = new HostelFile(hostelName);
     makeFrame();
     showFrame();
 }

 public void showFrame()    
 {   
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    setSize(800,600);
    setVisible(true);
 }

 public void makeFrame()    
 {
    JPanel buttonPanel = new JPanel();               
    buttonPanel.setLayout(new GridLayout(10,10,10,10));
    buttonPanel.setBorder(BorderFactory.createEtchedBorder());        
    buttonPanel.add(listFreeButton);
    buttonPanel.add(newBookingButton);
    buttonPanel.add(cancelBookingButton);
    buttonPanel.add(listAllButton);
    buttonPanel.add(allRoomsButton);
    buttonPanel.add(findRoomButton);
    buttonPanel.add(exitButton);

    textOutput = new JPanel();
    textOutput.setLayout(new BorderLayout());
    textOutput.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

    output.setEditable(false);
    scroller = new JScrollPane(output);     
    textOutput.add(scroller);

    add(buttonPanel,BorderLayout.WEST);
    add(textOutput,BorderLayout.CENTER);

    listFreeButton.addActionListener(this);
    newBookingButton.addActionListener(this);
    cancelBookingButton.addActionListener(this);
    listAllButton.addActionListener(this);
    allRoomsButton.addActionListener(this);
    findRoomButton.addActionListener(this);
    exitButton.addActionListener(this);
 }

 public void actionPerformed( ActionEvent ae)    
 {
    String item = ae.getActionCommand(); 

    if (item.equals("List Free Rooms"))
    {
    }
    else if (item.equals("New Booking"))
    {
        AddBooking bookingGUI = new AddBooking(this);
    }
    else if (item.equals("Cancel Booking"))
    {
    }
    else if (item.equals("List All Bookings"))
    {
        output.setText(hostel.getAllBookings());
    }
    else if (item.equals("List All Room Details"))
    {
    }
    else if (item.equals("Find Room"))
    {
        String name = JOptionPane.showInputDialog(this, 
                                  "Enter Guests Name",
                                  "Find Room",
                                  JOptionPane.QUESTION_MESSAGE);   
        output.setText( hostel.searchName(name));
    }
    else if (item.equals("Exit System"))
    {
        hostel.saveListBookings();
        System.exit(0);
    }
 }


 public void addBooking(String roomID, String roomType, String guest)
 {
    output.setText(hostel.addBooking(roomID,roomType,guest));
 }
}

full code for methods

import java.util.*;
public class ListBookings
{
 private String hostelName;
 private ArrayList<Booking> bookings;
 private String roomID;
 private String roomType;
 private boolean ensuite;
 private String guest;
 private String nights;
 private String booked;
 /**
  * constructs objects for GUI
  */

 public ListBookings(String hostelName)
 {
    this.hostelName = hostelName;
    bookings = new ArrayList<Booking>();
 }

 public String getHostelName()
 {
    return hostelName;
 }

 public String addBooking(String roomID, String roomType, String guest)
 {
    if (roomID.equals(""))
        return "Error Please Entre Room ID";

    else if (roomType.equals(""))
        return "Error Please Entre Room Type";

    else if (guest.equals(""))
        return "Error Please Entre Guest Name";

    bookings.add(new Booking(roomID,roomType,guest));
    return "Room " + roomID + " " + roomType + " Has Been Booked For " + guest;
 }

 public String getAllBookings()
 {
    if (bookings.size() > 0)
    {
        String allBookings = "";
        for (Booking s : bookings)
        {
            allBookings = allBookings + s.getRoomID() + " " + s.getRoomType() + " " + s.getGuest() + "\n";
        }
        return allBookings;
    }
    else 
    {
        return "Bookings are Empty";
    }
 }

 public String searchName(String guest)
 {
    for (Booking s : bookings)
    {
        if (s.getGuest().equals(guest))
        return guest + " is in room " + roomID + "\n"; 
    }
    return guest + " is not booking in at the hostel";
 }

 public String deleteBooking(String roomID)
 {
    int index = 0;
    for ( Booking s : bookings )
    {
        if ( s.getRoomID().equals(roomID))
        {
            return "Room ID: " + roomID + " Room Type: " + roomType + " Guest: " + guest;
            //students.remove(index);
            //return name + " removed from  module " + moduleName + "\n";
        }
        index++;
    }
    return  "  Cannot find " + roomID + "\n";
 }
}

HostelFile

import java.util.*;
import java.io.*;
public class HostelFile extends ListBookings
{
 public HostelFile(String hostelName)
 {
    super(hostelName);
    readListBookings(hostelName);
 }

 private void readListBookings(String fileName)
 {
    String roomID;
    String roomType;
    String guest;
    try
    {
        Scanner fileScanner = new Scanner( new File ( fileName + ".txt"));
        while (fileScanner.hasNext())
        {
            roomID = fileScanner.next();
            roomType = fileScanner.next();
            guest = fileScanner.next();
            addBooking(roomID,roomType,guest);
        }
        fileScanner.close();
    }
    catch (IOException e)
    {
        System.out.println("File not found");
    }
 }


 public void saveListBookings()
 {
    String fileName = getHostelName() +".txt" ;  
    try { 
      PrintWriter print = new PrintWriter( 
                  new BufferedWriter( 
                             new FileWriter( fileName ) ) ); 

      print.println(getAllBookings());
      print.close();
    } 
    catch ( IOException iox ) {
       System.out.println("Problem writing " + fileName ); 
    }
 }             
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:04:52+00:00Added an answer on May 27, 2026 at 11:04 pm

    It doesn’t make sense that the var roomID can be used for this. There must be some property in class Booking that holds the room. Something like “s.getRoomID()”.

    Edit: OK. Seeing all the classes, it is clear that Booking has a getRoomID() method.

    Change your function to this:

    public String searchName(String guest)
    {
        for (Booking s : bookings)
        {
            if (s.getGuest().equals(guest))
            return guest + " is in room " + s.getRoomId() + "\n"; 
        }
        return guest + " is not booking in at the hostel";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search function that returns an arraylist of objects to my form.
How can i have slide downeffect? The effect will be similar with search function
hai I have a magento web site. In this the search function compare with
I have a function that uses Pattern#compile and a Matcher to search a list
I have search through the web to figure this out but no luck. I
I tried to search the site for this question but didn't find this exactly,
Say I have a jquery function within JavaScript and I have this code so
I've look at the questions on here but can't find an answer that specifically
if i have a search function for my site and i want the user's
I have an active directory search function: Function GetAdInfo(ByVal ADDN As String, ByVal ADCommonName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.