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

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

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

问题

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

以下是部分CSV文件内容:

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

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

  1. public class Client {
  2. private int clientID;
  3. private String firstName;
  4. private String surName;
  5. private String street;
  6. private String suburb;
  7. private String state;
  8. private int postcode;
  9. // 构造函数
  10. public Client(int ID, String fName, String sName, String str, String sb, String sta, int pCode) {
  11. clientID = ID;
  12. firstName = fName;
  13. surName = sName;
  14. street = str;
  15. suburb = sb;
  16. state = sta;
  17. postcode = pCode;
  18. }

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

  1. File inFile = new File("clients.txt");
  2. Scanner inputFile = new Scanner(inFile);
  3. String str;
  4. String[] tokens;
  5. ArrayList<Client> cList = new ArrayList<>();
  6. while (inputFile.hasNext()) {
  7. str = inputFile.nextLine();
  8. tokens = str.split(",");
  9. int clientID = Integer.parseInt(tokens[0]);
  10. String[] nameTokens = tokens[1].split(" ");
  11. String firstName = nameTokens[0];
  12. String surName = nameTokens[1];
  13. String street = tokens[2];
  14. String suburb = tokens[3];
  15. String state = tokens[4];
  16. int postcode = Integer.parseInt(tokens[5]);
  17. Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
  18. cList.add(client);
  19. System.out.println(client + "\n");
  20. }

因此,我想以每个客户的格式打印出结果: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):

  1. public class Client {
  2. private int clientID;
  3. private String firstName;
  4. private String surName;
  5. private String street;
  6. private String suburb;
  7. private String state;
  8. private int postcode;
  9. // constructor
  10. public Client (int ID, String fName, String sName, String str, String sb, String sta, int pCode) {
  11. clientID = ID;
  12. firstName = fName;
  13. surName = sName;
  14. street = str;
  15. suburb = sb;
  16. state = sta;
  17. postcode = pCode;
  18. }

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

  1. File inFile = new File(&quot;clients.txt&quot;);
  2. Scanner inputFile = new Scanner(inFile);
  3. String str;
  4. String[] tokens;
  5. ArrayList&lt;Client&gt; cList = new ArrayList&lt;&gt; ();
  6. while (inputFile.hasNext()) {
  7. str = inputFile.nextLine();
  8. tokens = str.split(&quot;,&quot;);
  9. int clientID = Integer.parseInt(tokens[0]);
  10. String firstName = tokens[1];
  11. String surName = tokens[2];
  12. String street = tokens[3];
  13. String suburb = tokens[4];
  14. String state = tokens[5];
  15. int postcode = Integer.parseInt(tokens[6]);
  16. Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
  17. cList.add (client);
  18. System.out.println(client + &quot;\n&quot;);
  19. } // 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. 1,Jay,Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
  2. 2,Mel,Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
  3. 3,Hugh,Manatee,32 Edgecliff Road,REDFERN,NSW,2016
  4. 4,Elizabeth,Turner,93 Webb Road,MOUNT HUTTON,NSW,2290
  1. 再次通过空格从数组中拆分字符串,像这样:
  1. File inFile = new File("clients.txt");
  2. Scanner inputFile = new Scanner(inFile);
  3. String str;
  4. String[] tokens;
  5. ArrayList<Client> cList = new ArrayList<>();
  6. while (inputFile.hasNext()) {
  7. str = inputFile.nextLine();
  8. tokens = str.split(",");
  9. int clientID = Integer.parseInt(tokens[0]);
  10. String firstName = tokens[1].split(" ")[0];
  11. String surName = tokens[1].split(" ")[1];
  12. String street = tokens[2];
  13. String suburb = tokens[3];
  14. String state = tokens[4];
  15. int postcode = Integer.parseInt(tokens[5]);
  16. Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
  17. cList.add(client);
  18. System.out.println(client + "\n");
  19. } // 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. 1,Jay,Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
  2. 2,Mel,Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
  3. 3,Hugh,Manatee,32 Edgecliff Road,REDFERN,NSW,2016
  4. 4,Elizabeth,Turner,93 Webb Road,MOUNT HUTTON,NSW,2290
  1. split the String from the array again by space, like this:
  1. File inFile = new File(&quot;clients.txt&quot;);
  2. Scanner inputFile = new Scanner(inFile);
  3. String str;
  4. String[] tokens;
  5. ArrayList&lt;Client&gt; cList = new ArrayList&lt;&gt; ();
  6. while (inputFile.hasNext()) {
  7. str = inputFile.nextLine();
  8. tokens = str.split(&quot;,&quot;);
  9. int clientID = Integer.parseInt(tokens[0]);
  10. String firstName = tokens[1].split(&quot; &quot;)[0];
  11. String surName = tokens[1].split(&quot; &quot;)[1];
  12. String street = tokens[2];
  13. String suburb = tokens[3];
  14. String state = tokens[4];
  15. int postcode = Integer.parseInt(tokens[5]);
  16. Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
  17. cList.add (client);
  18. System.out.println(client + &quot;\n&quot;);
  19. } // 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:

确定