将全名拆分为名字和姓氏,然后将代码组合以在程序中使用。

huangapple go评论83阅读模式
英文:

Split full name into first and sur name and then combine code to use with the program

问题

我是一名大一学生,对Java一窍不通,现在我在我的练习中遇到了麻烦。

以下是部分CSV文件内容:

1,Jay Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
2,Mel Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
3,Hugh Manatee,32 Edgecliff Road,REDFERN,NSW,2016
4,Elizabeth Turner,93 Webb Road,MOUNT HUTTON,NSW,2290

这是我的Client类(具有构造函数):

public class Client {
    private int clientID;
    private String firstName;
    private String surName;
    private String street;
    private String suburb;
    private String state;
    private int postcode;

    // 构造函数
    public Client(int ID, String fName, String sName, String str, String sb, String sta, int pCode) {
        clientID = ID;
        firstName = fName;
        surName = sName;
        street = str;
        suburb = sb;
        state = sta;
        postcode = pCode;
    }

这是我用于创建对象并从CSV文件中读取数据的代码:

File inFile = new File("clients.txt");
Scanner inputFile = new Scanner(inFile);
String str;
String[] tokens;
ArrayList<Client> cList = new ArrayList<>();   
while (inputFile.hasNext()) {
    str = inputFile.nextLine();          
    tokens = str.split(",");             
    
    int clientID = Integer.parseInt(tokens[0]);
    String[] nameTokens = tokens[1].split(" ");
    String firstName = nameTokens[0];
    String surName = nameTokens[1];
    String street = tokens[2];
    String suburb = tokens[3];
    String state = tokens[4];
    int postcode = Integer.parseInt(tokens[5]);
    
    Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
    cList.add(client);
    System.out.println(client + "\n");
} 

因此,我想以每个客户的格式打印出结果:clientID,firstName,surName,street,suburb,state,postcode。但是在txt文件中只有全名,我不知道如何将其拆分为名字和姓氏。

我也不知道如何将拆分名称的代码与此程序组合起来以匹配。

当我运行上述代码(我没有拆分名称)时,它总是出错并显示错误:“索引6超出长度6”。

由于我必须以特定格式显示结果,因为这是老师的要求,我不能将名字更改为客户名。

英文:

i'm a first year student and I'm a complete beginner to Java, now i am in trouble with my exercises

Here is part of the CSV file:

<!-- language: lang-none -->
1,Jay Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
2,Mel Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
3,Hugh Manatee,32 Edgecliff Road,REDFERN,NSW,2016
4,Elizabeth Turner,93 Webb Road,MOUNT HUTTON,NSW,2290

This is my Client class (has constructor):

public class Client {
	private int clientID;
	private String firstName;
	private String surName;
	private String street;
	private String suburb;
	private String state;
	private int postcode;
	
	// constructor
	public Client (int ID, String fName, String sName, String str, String sb, String sta, int pCode) {
		
		clientID = ID;
		firstName = fName;
		surName = sName;
		street = str;
		suburb = sb;
		state = sta;
		postcode = pCode;
	}

and this is my code to create object and read data from csv file:

File inFile = new File(&quot;clients.txt&quot;);
		Scanner inputFile = new Scanner(inFile);
		String str;
		String[] tokens;
		ArrayList&lt;Client&gt; cList = new ArrayList&lt;&gt; ();   
		while (inputFile.hasNext()) {
			
			str = inputFile.nextLine();          
			tokens = str.split(&quot;,&quot;);             
			
			int clientID = Integer.parseInt(tokens[0]);
			String firstName = tokens[1];
			String surName = tokens[2];
			String street = tokens[3];
			String suburb = tokens[4];
			String state = tokens[5];
			int postcode = Integer.parseInt(tokens[6]);	
			
			Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);

			cList.add (client);
		    System.out.println(client + &quot;\n&quot;);
				
		} // end while

So i want to print out the result with the format clientID, firstName, surName, street, suburb, state, postcode perpestively. However in the txt file there are just full names and i dont know how to split it into first name and sur name.

And i also could not know how to combine the code to split the name into this program to match

As i run the code above (which i did not split the name), it always wrong and has error :"index 6 out of bound for length 6".

As i must display the result with the format because it is a requirement from the teacher and i could not change first name and sur name into Client Name

答案1

得分: 0

有2种可能的解决方案(依我所见):

  1. 在文件中用逗号(,)将名字和姓氏之间隔开,像这样写客户端:
1,Jay,Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
2,Mel,Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
3,Hugh,Manatee,32 Edgecliff Road,REDFERN,NSW,2016
4,Elizabeth,Turner,93 Webb Road,MOUNT HUTTON,NSW,2290
  1. 再次通过空格从数组中拆分字符串,像这样:
File inFile = new File("clients.txt");
Scanner inputFile = new Scanner(inFile);
String str;
String[] tokens;
ArrayList<Client> cList = new ArrayList<>();   
while (inputFile.hasNext()) {
    str = inputFile.nextLine();          
    tokens = str.split(",");
    
    int clientID = Integer.parseInt(tokens[0]);
    String firstName = tokens[1].split(" ")[0];
    String surName = tokens[1].split(" ")[1];
    String street = tokens[2];
    String suburb = tokens[3];
    String state = tokens[4];
    int postcode = Integer.parseInt(tokens[5]); 
    
    Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
    cList.add(client);
    System.out.println(client + "\n");
} // end while
英文:

There are 2 possible solution (as I can see):

  1. write the Client in the file with down comma (,) between the first and sur name, like this:
1,Jay,Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
2,Mel,Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
3,Hugh,Manatee,32 Edgecliff Road,REDFERN,NSW,2016
4,Elizabeth,Turner,93 Webb Road,MOUNT HUTTON,NSW,2290
  1. split the String from the array again by space, like this:
File inFile = new File(&quot;clients.txt&quot;);
        Scanner inputFile = new Scanner(inFile);
        String str;
        String[] tokens;
        ArrayList&lt;Client&gt; cList = new ArrayList&lt;&gt; ();   
        while (inputFile.hasNext()) {
            
            str = inputFile.nextLine();          
            tokens = str.split(&quot;,&quot;);             
            
            int clientID = Integer.parseInt(tokens[0]);
            String firstName = tokens[1].split(&quot; &quot;)[0];
            String surName = tokens[1].split(&quot; &quot;)[1];
            String street = tokens[2];
            String suburb = tokens[3];
            String state = tokens[4];
            int postcode = Integer.parseInt(tokens[5]); 
            
            Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);

            cList.add (client);
            System.out.println(client + &quot;\n&quot;);
                
        } // end while

huangapple
  • 本文由 发表于 2020年10月23日 13:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64494327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定