我正在开发一个支持MaterialFileChooser的Livestream应用程序,但是我正在努力检查所选文本文件中的一行是否以“ h”开头,这些行(以h开头)是否应该存储在字符串中。
我尝试过:
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String oldString = scanner.next();
String newString = changeVowels(oldString);
System.out.println(newString);
scanner.close();
}
private static String changeVowels(String oldString) {
char[] characters = oldString.toCharArray();
for (int i = 0; i < characters.length; i++) {
switch (characters[i]) {
case 'a':
characters[i] = 'e';
break;
case 'e':
characters[i] = 'i';
break;
case 'i':
characters[i] = 'o';
break;
case 'o':
characters[i] = 'u';
break;
case 'u':
characters[i] = 'a';
break;
default:
break;
}
}
return String.valueOf(characters);
}
}
答案 0 :(得分:0)
我不明白您的意思是“应该存储在字符串中”。如果您需要以“ h”开头的行,只需创建一个ArrayList
字符串并将其保存在那里。
// Declare an ArrayList first
private ArrayList<String> lineStore = new ArrayList<String>();
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1000 && resultCode == RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("h")) {
// Store the line in the ArrayList to be used later
lineStore.add(line); // That's what you meant?
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}