【问题标题】:Java converting upper to lower sortingJava将上排序转换为下排序
【发布时间】:2013-05-16 11:09:53
【问题描述】:

我有一个程序可以对文本文件中的行进行排序并按字母顺序排列,但它无法对第一个单词进行排序,因为第一个单词的第一个字母是大写的,并且该单词的第一个字母必须大写,我不知道该怎么做。

这是文本文件:

圣诞老人,你最好小心你的脚步!

橱窗里的那只小狗多少钱?

敏捷的棕色狐狸跳过了懒惰的狗。

你读过 Khuth 的编程系列吗?

没有比这更好的了!

这是我的代码:

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

public class wordSorter {
public static void main(String[] args) throws Exception {
String firstTextFile = "prob10.in.txt";
String secondTextFile = "prob10.out.txt";
Scanner Document = null;
PrintWriter NewFile = null;
String inputFile = "";
String outputFile = "";

try{
    Document = new Scanner(new File(firstTextFile));
}
catch(Exception e){
    System.out.println("Could not find " + firstTextFile);
    System.exit(0);
}

try{
    NewFile = new PrintWriter(new FileOutputStream(secondTextFile, true));
}
catch(Exception f){
    System.out.println("Could not find " + secondTextFile);
    System.exit(0);
}

while (Document.hasNextLine()){
inputFile = Document.nextLine();
String line = inputFile;
line = line.toLowerCase();
String[] words = line.split(" ");

Arrays.sort(words);
NewFile.println(Arrays.toString(words));
}
Document.close();
NewFile.close();

}

}

【问题讨论】:

  • 把它放在 Q 中可能会更好。否则就支持我见过的最奇怪的评论。

标签: sorting uppercase lowercase


【解决方案1】:

看看Java's Collator ...既然您是Java新手,请查看Java CodeRanch上的示例

【讨论】:

  • 把它扔出去我是java新手,你刚刚发送的内容对我来说毫无意义。
  • 对不起,我不知道人们知道什么程度。这是一个示例链接:coderanch.com/t/492333/java/java/…
猜你喜欢
  • 2021-10-11
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多