0

I am calling the method findRoom() which is in the class myClass from the main method:

int room[]= {1,2,3,4,5,6,7,8,9,10};
String customer[] = {"","Shay","","Yan","Pan","","","Xiao","Ali",""};
    myClass m = new myClass();
    m.findRoom(customer, room);         

The class myClass is as follows:

class myClass {

int count = 0;

public void findRoom(String customerName[], int roomNo[]) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter Customer's Name");

    String name = sc.next();

    for (int i = 0; i < 10; i++) {

        if (customerName[i].equalsIgnoreCase(name)) {
            System.out.println(roomNo[i]);
            count++;
            break;
        } else {
            count++;

        }
    }
    myMethod(customerName, roomNo);
}

public void myMethod(String cusName[], int rooms[]) {
    myClass m = new myClass();
    if (m.count == 10) {
        System.out.println("Please check the name again");
        m.count = 0;
        m.findRoom(cusName, rooms);
    }
}
}

I want the program to prompt the user to enter the customer's name again, if the name entered by the user is not found in the array customer[]. So I created the method myMethod() which will ask the user to re-enter customer's name.

The program runs fine if user enters a name that is already in the array, but doesn't work the other way around when user enters a name that is not found in the array. The method myMethod() is not being called. What could be the possible reason for this? Is it a problem with the parameter passing? Any help is appreciated. =)

Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
Dinz_N
  • 23
  • 1
  • 2
  • 9
  • 3
    Always always always, your class name should start with a CAPITAL LETTER :) – Jude Niroshan Jul 16 '16 at 14:48
  • Each time you are creating a new MyClass Object by calling myMethod. You are accessing the instance variable (count) of that newly created object which is not satisfying the if condition in your method. – Sagar Pudi Jul 16 '16 at 14:54

3 Answers3

2

Your mistake is that, when you go into myMethod, you create new myClass object and this object has count field, but value of this filed is zero, because this is new object. But all your work and changing count field going in another object that you create in main method:

myClass m = new myClass();
m.findRoom(customer, room); 

If you need so simple example, try to use static modifier on field count:

static int count = 0;

Edit findRoom method:

    myClass.count++;
    break;
} else {
    myClass.count++;

Edit myMethod method:

if (myClass.count == 10) {
    System.out.println("Please check the name again");
    myClass.count = 0;
    m.findRoom(cusName, rooms);
}
Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
1

First of all I suggest you to learn more about objects and classes. In your code in method myMethod() first statement is for creating a new object of myClass. When you create that it's like you are taking a fresh copy of your class's attributes. (If they are not static) So always it will give you variable count with the value you gave it, that is 0.
If your code has to remember the values given to class variables not depending on the objects you create you have to make them static. Then you can call those variables without creating objects.

myClass.count;

So what you have to do is just replace int count=0; with static int count=0; and few more things to improve your coding.

  • Start class names with capital letters (this is not a rule, but a good practice)
  • Learn more about difference between static and non-static methods/variables.
  • Normally class variables are used with private access modifier. (for encapsulation)
Lasitha Yapa
  • 4,309
  • 8
  • 38
  • 57
0

Something like this should do the trick:

import java.util.Scanner;
public class MyClass {


    static int[] room;
    static String[] customer;



    public static void main(String[] args) {

        room = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        customer = new String[]{"", "Shay", "", "Yan", "Pan", "", "", "Xiao", "Ali", ""};

        MyClass mc = new MyClass();
        mc.findRoom(customer, room);

    }



    public void findRoom(String customerName[], int roomNo[]){

        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the Customer's name.");
        String name = sc.next();

        int count = 0;


        for(int i = 0; i < customerName.length; i++){
            if(customerName[i].equalsIgnoreCase(name)){

                System.out.println(name + " is in room number " + roomNo[i]);
                break;
            }
            else{
                count++;
            }
        }

        //##### RECURSION STARTS HERE #####
        if(count == customerName.length){

            count = 0;
            System.out.println("Name not found, please try again.\n");
            findRoom(customerName, roomNo);
        }

    }

}

Recursion (method calling on itself), saves you from having to create two methods, also using arrayName.length within the 'if statement' will avoid you having to hard code the condition if you decide to have a bigger set of arrays.

Dinz_N
  • 23
  • 1
  • 2
  • 9