我需要通过使用scanner类来通过用户输入访问名称和ID [英] I need to access name and id through the user input by using scanner class

查看:58
本文介绍了我需要通过使用scanner类来通过用户输入访问名称和ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package customer_details;
import java.util.*;
public class customer_deatils
{
	
	public String name;
	public int id;
	
	public customer_deatils(String name,int id)
	{
		this.name=name;
		this.id=id;
	}
	public static void    customer_deatils(customer_deatils[] c)
	{
		for(customer_deatils d:c)
		{
			System.out.println(d);
		}
		
	}
	public String toString()
	{
		return name+" "+id;
	}
	public static void main(String args[])
	
	{
		Scanner s1=new Scanner(System.in);
		customer_deatils []acc1=new customer_deatils[3];
	//	customer_deatils obj=new customer_deatils(s1.nextLine(),s1.nextInt());
		
		 for(int i=0;i<acc1.length;i++)
		 {
			 
			 acc1[i]=new customer_deatils(s1.nextLine(),s1.nextInt());
			 
			 
		 }
		customer_deatils(acc1);
		
		
		
		
		
	}
	
	
}





我尝试了什么:



i尝试了但它显示错误输入不匹配,但我是根据构造函数中的数据类型给出输入



What I have tried:

i tried but it shows error input mismatch, but i am giving input based on the datatype in constructor

推荐答案

在调用 s1.nextInt 后,您有一个尾随换行符扫描仪输入流中剩余的字符。您需要在下次通话之前使用它。如果在创建类对象之前将每个项读入局部变量,它也会使调试变得更容易。
After your call to s1.nextInt you have a trailing newline character remaining in the scanner's input stream. You need to consume that before the next call. It would also make debugging far easier if you read each item into a local variable before creating the class object.


这篇关于我需要通过使用scanner类来通过用户输入访问名称和ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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