Skip to content
Advertisement

How do i pass variables into methods – java [closed]

I need to use a variable from one method in another. Here is the code:

public static void secondMain() {
        BufferedReader reader;
        var lines = new ArrayList<String>();
        var rooms = new ArrayList<Room>();

    try {
        reader = new BufferedReader(new FileReader("rooms.txt"));
        String line = reader.readLine();
        lines.add(line);
        while (line != null) {
            line = reader.readLine();
            lines.add(line);

        }
        
        reader.close();
        
        
        
        for (int i = 0; i < lines.size() - 1; i++){

            String[] words = lines.get(i).split(" ");
            var room = new Room();
            room.roomNum = Integer.parseInt(words[0]);
            room.roomType = (words[1]);
            room.roomPrice = Double.parseDouble(words[2]);
            room.hasBalcony = Boolean.parseBoolean(words[3]);
            room.hasLounge = Boolean.parseBoolean(words[4]);
            room.eMail = (words[5]);
            rooms.add(room);    
                       
            }

Here is where it needs to be used:

public static void reserveRoom() {
        // TODO Auto-generated method stub
    
    String choice = "";
    
    do {
secondMain();
        System.out.println("n-- ROOM RESERVATION --");
        System.out.println("Please enter the room number you wish to reserve");
        System.out.println("Q - Quit");
        Room selectedRoom = null;
         var searchRoomNum = input.nextInt();
        for(int i = 0; i < rooms.size(); i++){                 
              if(rooms.get(i).roomNum == searchRoomNum){
                  selectedRoom = rooms.get(i);            
                    System.out.println("Room Number: " + selectedRoom.roomNum);
                    System.out.println("Room Type: " + selectedRoom.roomType);
                    System.out.println("Room Price: " + selectedRoom.roomPrice);
                    System.out.println("Balcony: " + selectedRoom.hasBalcony);
                    System.out.println("Lounge: " + selectedRoom.hasLounge);
                    System.out.println("Email: " + selectedRoom.eMail);
                    System.out.println("-------------------");
              }
         }
        
    } 

my current error is on the for loop where im testing the conditions against the array. “rooms cannot be resolved”.

Advertisement

Answer

If you want to access rooms from the secondMain() method in the reserveRoom(), you should return it from secondMain() when you make the call. This can be done by changing the return type of the method public static void secondMain() to become public static ArrayList<Room> secondMain()

Then after the for loop in secondMain() when you have finished adding all the rooms to the array list, you should return rooms to the caller. This can be added as one statement after your try catch block.

return rooms;

This way you can access this property from reserveRoom(). The line where the call is made

do {
secondMain();

should become:

do{
var rooms = secondMain();

This should allow you to access rooms in reserveRoom()

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement