简体   繁体   English

使用扫描器读取文本,存储在数组中,并为每个循环使用大写输出Java

[英]Reading text using scanner, storing in array and outputting uppercase using for each loop Java

I have to write a program to read in 10 names from a user (keyboard scanner) and store them in an array. 我必须编写一个程序来从用户(键盘扫描仪)中读取10个名称并将它们存储在数组中。 Then loop through the array and display the names in upper case (using a for each loop to display them all). 然后遍历数组并以大写形式显示名称(每个循环使用a来显示所有名称)。 How do I do it? 我该怎么做? At the moment I have: 目前,我有:

import java.util.Scanner;

public class Names
{
    public static void main(String[] args) throws Exception
    {
        Scanner nameScan = new Scanner(System.in);
        String[] names = new String[10];
        String[] namesUpper = new String[10];

        System.out.print("Enter a name : ");
        names=nameScan.next(); 


        namesUpper=names.toUpperCase();
        System.out.println("Names in upper case: "+namesUpper);
    } 
}

So far I have this, but still not working. 到目前为止,我有这个,但仍然无法正常工作。 Where am I going wrong please? 请问我哪里出问题了? Thanks 谢谢

import java.util.Scanner;

    public class NamesReAD
    {
        public static void main(String[] args) throws Exception
        {
            Scanner nameScan = new Scanner(System.in);
            String[] names = new String[10];


            for (int i = 0 ; i < names.length ; i++) {
                System.out.print("Enter a name: ");
                names[i] = nameScan.nextLine().toUpperCase();
            }
            nameScan.close();
            System.out.println("Here is name"+names);
        }
    }
for (int i = 0 ; i < names.length ; i++) {
    System.out.print("Enter a name: ");
    names[i] = nameScan.nextLine().toUpperCase();
}

This section would work except the .toUpperCase(); 除了.toUpperCase();以外,本节将起作用。 It will not change the names to uppercase. 不会将名称更改为大写。

You'll need a for -loop to traverse the array(s) and put the appropriate data at each index. 您需要一个for -loop遍历数组并将适当的数据放在每个索引处。 For instance, 例如,

for (int i = 0 ; i < names.length ; i++) {
    System.out.print("Enter a name: ");
    names[i] = nameScan.nextLine();
}

This will ask the user for a name 10 times, and will fill the names array with whatever the user inputs. 这将要求用户输入名称10次,并使用用户输入的内容填充names数组。 Now, you don't really have to create a second array ( namesUpper ); 现在,您实际上不必创建第二个数组( namesUpper ); you can just loop over names again and print names[i].toUpperCase() (or use a for -each loop as you suggested, but the idea is the same). 您可以再次循环遍历names并打印names[i].toUpperCase() (或按照您的建议使用for -each循环,但是想法是一样的)。


As you have it now, these two lines won't compile: 现在,这两行将无法编译:

names = nameScan.next(); 
namesUpper = names.toUpperCase();

since 1) names is an array and nameScan.next() returns a string and 2) names has no method called toUpperCase . 因为1) names是一个数组,并且nameScan.next()返回一个字符串,并且2) names没有名为toUpperCase方法。


EDIT : Or, you could just store the names as upper-case when you add them to the names array: 编辑 :或者,当您将names添加到names数组中时,可以将它们存储为大写形式:

for (int i = 0 ; i < names.length ; i++) {
    System.out.print("Enter a name: ");
    names[i] = nameScan.nextLine().toUpperCase();
}

Then you wouldn't have to worry about anything when you print the contents of the array. 这样,当您打印数组的内容时,您将不必担心任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM