How to update a variable in a java object array

问题: I am creating a member application using the scanner to input data into an array called memberList. The data is going into an object and then the object is being stored in...

问题:

I am creating a member application using the scanner to input data into an array called memberList. The data is going into an object and then the object is being stored in the array. I am using string and integers in the object.

I am writing a method to change one of the string variables in the array from yes to no, it is whether the member has paid their fees or not.

I am trying to enter the members name by calling my getName method and using the setFeePaid to change the String to "yes" from "no".

There is something wrong with the loop, as it only lets me change the first member in the array. Could anyone help please.

public static void payMemberYearlyFees(){
        for (int i=0; i < memberCount; i++) {       
            Member member = memberList[i];

            System.out.println("Enter the name of the member who has to pay their fee:");

            if (input.nextLine().equals(member.getName())) {    
                member.setFeePaid("yes");
                System.out.println();
                System.out.println(member.getName() +" has paid their membership fee.n");
                System.out.println("********Returning to Main Menu.********n");                
            }
            else{
                System.out.println();
                break;
            }
        }   
    }

回答1:

In your for loop, you are first taking a member (the first one in the list) and then asking the user to input a name. It then checks if the input name is the same as the member that you've taken (remember, only the first of the list). If you input any other name, the if statement fails and you go to the else block, which stops the loop.

This should work:

public static void payMemberYearlyFees(){
    System.out.println("Enter the name of the member who has to pay their fee:");
    String inputName = input.nextLine();

    for (int i=0; i < memberCount; i++) {       
        Member member = memberList[i];            

        if (inputName.equals(member.getName())) {    
            member.setFeePaid("yes");
            System.out.println();
            System.out.println(member.getName() +" has paid their membership fee.n");
            System.out.println("********Returning to Main Menu.********n");   
            break;             
        }
    }   
}

Then call this method as many times as you want. If you want your code to be a bit cleaner, you can do the for loop this way:

for (Member member : memberList){
    if (inputName.equals(member.getName())) {    
        member.setFeePaid("yes");
        System.out.println();
        System.out.println(member.getName() +" has paid their membership fee.n");
        System.out.println("********Returning to Main Menu.********n");   
        break;             
    }
}

That way you just take the members of your list one by one without having to deal with the i variable and manually pick the i member of the list.

  • 发表于 2018-12-31 01:27
  • 阅读 ( 266 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除