我需要帮助在for循环中创建单词UpperCase,这个单词不是用eclipse编写的,而是从txt文件导入的

时间:2013-09-23 19:34:34

标签: java eclipse

import java.util.*;
import java.io.*;

public class Cascader {
    public static void main(String args[]) throws IOException {
        Scanner inputfile = new Scanner(new File("name.txt"));
        String name = inputfile.nextLine();
        System.out.println("Reading the name...");
        for(int i = 0; i < name.length(); i++) {
            System.out.println(name.charAt(i));
        }
        inputfile.close();
    }
}

1 个答案:

答案 0 :(得分:1)

回答你的问题:你可能没有“保存”新的字符串。 toUpperCase()方法返回一个新的String,其中每个字符都已更改为大写。

name.toUpperCase(); // does not do anything to the String in the variable 'name'
String newName = name.toUpperCase(); // if name was 'john', newName will be 'JOHN'
name = name.toUpperCase(); // if name was 'john', name will now be 'JOHN'