Bug 753

Summary: The program skips a line of code about scanner input and take "" as default value
Product: coworking space management system Reporter: yongjiawu5-c
Component: testsystemAssignee: yongjiawu5-c
Status: RESOLVED FIXED    
Severity: normal CC: takhchau2-c, yongjiawu5-c
Priority: ---    
Version: release2   
Hardware: PC   
OS: Windows   

Description yongjiawu5-c 2022-12-05 01:42:36 HKT
The program output message:
Please choose the property you want to change: (type number to choose)
1. Username;
2. password.
1
New username: Changed successfully.

expected output: 
Please choose the property you want to change: (type number to choose)
1. Username;
2. password.
1
New username: NEW_VALUE
Changed successfully.

IDE: Eclipse IDE 2021-06 (4.20.0)

Java Version: JDK 17

Related Code:
package System;

import java.util.Scanner;
import java.util.UUID;

public class CmdChangeGlobalUserInfo implements Command {
	public String[] getParamList() {
		Scanner in = new Scanner(System.in);
		String[] paramList = new String[3];
		System.out.print("Please input user id to select user: ");
		try {
			paramList[0] = in.next();
			UUID test = UUID.fromString(paramList[0]);
		}
		catch(Exception e) {
			System.out.println("Please input id with correct format.");
			return null;
		}
		System.out.print("Please choose the property you want to change: (type number to choose)\n"
				+ "1. Username;\n"
				+ "2. password.\n");
		String packageIndex = in.next();
		switch(packageIndex) {
			case "1":
				paramList[1] = packageIndex;
				System.out.print("New username: ");
				paramList[2] = in.nextLine();
				break;
			case "2":
				paramList[1] = packageIndex;
				System.out.print("New password: ");
				paramList[2] = in.nextLine();
				break;
			default:
				System.out.print("Cannot recognize the number you typed.\n");
				return null;
		}
		return paramList;
	}
	public void execute(Role role, String[] paramList) {
		if(paramList!=null&&role.canChangeGlobalUserInfo()) {
			ManagementPortal managementPortal = ManagementPortal.getInstance();
			managementPortal.changeUserInfo(paramList);
		}
		else if(paramList==null) {}
		else {
			System.out.print(role.toString()+" role cannot check global user info.\n");
		}
	}

}
package System;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.UUID;


public class ManagementPortal {
	
	private static final ManagementPortal instance = new ManagementPortal();
	private User currentUser;
	private boolean isLoggedIn;
	private ArrayList<Customer> customerList = new ArrayList<Customer>();
	private ArrayList<Space> spaceList = new ArrayList<Space>();
	private ArrayList<Payment> paymentList = new ArrayList<Payment>();
	private ArrayList<ReserveRecord> reserveRecordList = new ArrayList<ReserveRecord>();
	
	private ManagementPortal() {
		this.currentUser = new User();
		this.isLoggedIn = false;
		initSpace();
	}
	
	// for CmdChangeGlobalUserInfo class
	public void changeUserInfo(String[] paramList) {
		UUID id = UUID.fromString(paramList[0]);
		Customer customer = searchCustomerById(id);
		if(customer!=null) {
			String property = paramList[1];
			String newValue = paramList[2];
			switch(property) {
			case "1":
				if(isExist(newValue)!=null) {
					System.out.print("The new username has already existed.\n");
					return;
				}
				customer.setUsername(newValue);
				break;
			case "2":
				customer.setPassword(newValue);
				break;
			};
			System.out.print("Changed successfully.\n");
		}
		else {
			System.out.print("Cannot find the customer by the id provided.\n");
		}
	}

}
Comment 1 yongjiawu5-c 2022-12-05 02:22:38 HKT
FIXED

Change the in.next() to in.nextLine in CmdChangeGlobalUserInfo class's getParamList method.

The problem is caused by incorrect usage of next() method. I use two next() and the second next() may receive "\n" from the end of the first next()'s input which will give it a "" value.