Java错误 - 我认为问题可能是Nextint()之后Nextint()但不确定.. [英] Java Error - I Think Problem May Be The Nextline() After Nextint() But Not Sure..

查看:55
本文介绍了Java错误 - 我认为问题可能是Nextint()之后Nextint()但不确定..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码。

代码在保存和搜索菜单中工作正常。

然而,我删除后'一个人并选择下一个菜单作为搜索,程序会跳过用户输入名称的部分,从而显示找不到结果。



我认为\ n可能是问题,并尝试了parseInt(),System.out.flush(),并再次使用nextLine(),但它们似乎都不起作用。



另外,我很困惑为什么在某些情况下,即使缓冲区中还有\ n,我也可以输入'name'而不会跳过程序。



如果不是问题的原因,请告诉我。

感谢您的时间。

The following is my code.
The code works fine with the 'save' and 'search' menus.
However, after I 'delete' a person and select the next menu as 'search', the program skips the part where the user enters a name, thus showing "no results found" as the result.

I thought that the \n may be the problem and tried parseInt(), System.out.flush(), and using nextLine() again but they all seemed not to work.

Also, I am confused why in some cases, even though there is a \n left in the buffer, I can enter the 'name' without the program skipping.

Please let me know if the \n is not the cause of the problem.
Thank you for your time.

//Phonebook Project - ver3

import java.util.Scanner;

class PhoneInfo
{
	String name;
	private String phoneNumber;
	private String birthDay;
	private String birthYear;
	private int choice;
	
	public PhoneInfo(String name, String phoneNumber, String birthDay)
	{
		this.name = name;
		this.phoneNumber = phoneNumber;
		this.birthDay = birthDay;
	}
	
	public PhoneInfo(String name, String phoneNumber, String birthDay, String birthYear)
	{
		this(name, phoneNumber, birthDay);
		this.birthYear = birthYear;
	}
	
	public void showPhoneInfo()
	{
		System.out.println("name: " + name);
		System.out.println("phone no.: " + phoneNumber);
		System.out.println("birthday: " + birthDay);
		if(birthYear == null)
			System.out.println("birth year: no record");
		else
			System.out.println("birth year: " + birthYear);
			
		System.out.println("-----------------------------");
	}
} 

class Manager //save, search, delete data
{
	PhoneInfo[] pBook = new PhoneInfo[100];
	Scanner input = new Scanner(System.in);
	
	public PhoneInfo inputData()
	{	
		System.out.print("name: ");
		String name = input.nextLine();
		
		System.out.print("phone no.: ");
		String phoneNumber = input.nextLine();
		
		System.out.print("birthday: ");
		String birthDay = input.nextLine();
		
		System.out.print("birth year: ");
		String birthYear = input.nextLine();
		
		PhoneInfo pData = new PhoneInfo(name, phoneNumber, birthDay, birthYear);
		
		System.out.println("-----------------------------");
		
		return pData;
	}
	
	public void saveData()
	{	
		for(int i=0; i<100; i++)
		{
			if(pBook[i] == null)
			{	
				pBook[i] = inputData(); //new entry
				break;
			}
			else
			{
				if(i == 99)
					System.out.println("database full");
				else
					continue;
			}
		}
	}
	
	public void searchData()
	{
		System.out.print("name: ");
		//tried putting System.out.flush(); here as well... didn't work
		String name = input.nextLine(); // do not want to use input.next() if possible
		System.out.println("\nSearching data...\n");
		for(int i=0; i<100; i++)
		{
			if(pBook[i] != null)
			{
				if(pBook[i].name.equals(name))
				{
					pBook[i].showPhoneInfo();
						break;
				}
				else
					continue;
			}
			else
			{
				System.out.println("no results found");
				break;
			}
		}
	}
	
	public void deleteData()
	{
		int idx;
		System.out.print("name: ");
		String name = input.nextLine();
		
		idx = getIndex(name);
		if(idx == -1)
			System.out.println("no matching name");
		
		System.out.println("Are you sure you want to delete " + name + "?");
		System.out.print("1. yes\n2. no\n> ");
		int choice = input.nextInt();
		if(choice == 1)
		{	
			for(int i=idx; i<99; i++)
				pBook[i] = pBook[i+1];
			System.out.println(name + " deleted...");
			System.out.println("-----------------------------");
		}
		if(choice == 2)
			return;
	}
	
	public int getIndex(String name)
	{
		int index = -1;
		for(int i=0; i<100; i++)
		{	
			if(pBook[i] != null)		
				if(pBook[i].name.equals(name)) //error --> worked when I added if(pBook[i] != null). why?? what difference did it make?
					index = i;
				else
					continue;
			else
				continue;
		}
		return index;
	}
}

class PhoneBookVer3
{
	static Scanner input = new Scanner(System.in);
	
	public static void showMenu()
	{
		System.out.println("[MENU]");
		System.out.println("choose...");
		System.out.println("1. save data");
		System.out.println("2. search data");
		System.out.println("3. delete data");
		System.out.println("4. exit program");
		System.out.print("> ");
	}
	
	public static void main(String[] args)
	{
		PhoneBookVer3 pb = new PhoneBookVer3();
		int choice;
		Manager mng = new Manager();
		while(true)
		{
			pb.showMenu();
			//choice = Integer.parseInt(input.nextLine()); // why this doesn't work either..
			choice = input.nextInt();
			//input.nextLine(); //why this doensn't consume the \n???
	//		if(input.hasNextInt()) //exception
	//		{
	//			while(!(choice>=1 && choice<=4))
	//			{
	//				System.out.print("enter again\n> ");
	//				choice = input.nextInt();
	//			}
	//		}
	//		else
	//		{
	//			input.next();
	//		}
			switch(choice)
			{
				case 1:
					System.out.println("-----------------------------");
					mng.saveData();
					break;
				case 2:
					System.out.println("-----------------------------");
					mng.searchData();
					break;
				case 3:
					System.out.println("-----------------------------");
					mng.deleteData();
					break;
				case 4:
					System.out.println("-----------------------------");
					System.out.println("end of program");
					return;
			}
		}
	}
}

推荐答案

nextInt()方法不读取输入的最后一个换行符,因此在下次调用nextLine()时消耗它。



The nextInt() method does not read the last newline character of your input, and hence its consumed in the next call to nextLine().

deleteData() {
instead 
int choice = input.nextInt();

Try this

int choice = Integer.parseInt(input.nextLine());
}


这篇关于Java错误 - 我认为问题可能是Nextint()之后Nextint()但不确定..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆