Java——如何将字符串扫描到数组中?

时间:2022-09-06 13:10:00

I'm having trouble with putting scanned information into array in Java.

我在用Java将扫描信息放入数组时遇到了麻烦。

private void initializeFriends()
{
    //initialize the size of array:
    friends = new Friend[200];
    //initialize the first 5 objects(Friends):
    friends[0] = new Friend("Rodney Jessep", "rj2013", "Brisbane", "hi!");
    friends[1] = new Friend("Jaime Smiths", "abcd5432", "Sydney", "how's going");
    friends[2] = new Friend("William Arnold", "william1994", "Brisbane", "boom");
    friends[3] = new Friend("James Keating", "qwerty52", "Newcastle", "Hey");
    friends[4] = new Friend("Amy Richington", "IAmRichAmy", "Perth", "Yo");
}

After the procedure above is run, a method called addFriends() is run where a scanner comes in to put data into friend structure.

在运行上述过程之后,将运行一个名为addFriends()的方法,其中有一个扫描器将数据放入friend结构中。

What is the best way to scan in information into an array? Would using the Scanner in = new Scanner(System.in); feature be wise since there are so many different elements in 'Friend': (String name, String username, String city, String message)?

将信息扫描到数组的最佳方式是什么?在= new Scanner(system .)中使用扫描器;特性是明智的,因为在“Friend”中有很多不同的元素:(String name, String username, String city, String message)?

2 个解决方案

#1


5  

If you're receiving input from the console and can safely assume that it is not going to be formatted improperly you can use:

如果您正在从控制台接收输入,并且可以安全地假设它不会被不正确地格式化,您可以使用:

Scanner in = new Scanner(System.in):  //receives input from the console
String line = in.nextLine(); //receives everything they type until they hit enter

That would take in all the information they enter. If their input is:

这将包含他们输入的所有信息。如果他们的输入:

"Jaime Smiths, abcd5432, Sydney, how's going"

You can then split the values by comma:

然后可以用逗号分隔值:

String[] values = line.split(",");
for(String s: values)
    System.out.println(s);

Gives the following output:

给下面的输出:

"Jaime Smiths"
" abcd5432"
" Sydney"
" how's going"

You'll want to remove trailing and or leading space characters, but that will give you the values you want, and you can then concatenate them and add them to the existing array:

您将希望删除尾随和或前导空格字符,但这将为您提供所需的值,然后您可以将它们串联起来,并将它们添加到现有的数组中:

friends[5] = new Friend(values[0], values[1], values[2], values[3]);

#2


0  

If you want to print Arraylist in string by scanner

如果你想用扫描器在字符串中打印Arraylist

Scanner scan=new Scanner(System.in);
ArrayList<String> list=new Arraylist<String>();
list.add(Scan.nextline);
System.out.println(list);

#1


5  

If you're receiving input from the console and can safely assume that it is not going to be formatted improperly you can use:

如果您正在从控制台接收输入,并且可以安全地假设它不会被不正确地格式化,您可以使用:

Scanner in = new Scanner(System.in):  //receives input from the console
String line = in.nextLine(); //receives everything they type until they hit enter

That would take in all the information they enter. If their input is:

这将包含他们输入的所有信息。如果他们的输入:

"Jaime Smiths, abcd5432, Sydney, how's going"

You can then split the values by comma:

然后可以用逗号分隔值:

String[] values = line.split(",");
for(String s: values)
    System.out.println(s);

Gives the following output:

给下面的输出:

"Jaime Smiths"
" abcd5432"
" Sydney"
" how's going"

You'll want to remove trailing and or leading space characters, but that will give you the values you want, and you can then concatenate them and add them to the existing array:

您将希望删除尾随和或前导空格字符,但这将为您提供所需的值,然后您可以将它们串联起来,并将它们添加到现有的数组中:

friends[5] = new Friend(values[0], values[1], values[2], values[3]);

#2


0  

If you want to print Arraylist in string by scanner

如果你想用扫描器在字符串中打印Arraylist

Scanner scan=new Scanner(System.in);
ArrayList<String> list=new Arraylist<String>();
list.add(Scan.nextline);
System.out.println(list);