- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我想做的是从文本文件中获取信息,
For example, 134567H;Gabriel;24/12/1994;67;78;89
然后我在下拉列表中仅显示第一个管理员号码,而不是整行。这是我的代码:
public static String[] readFile(){
String file = "output.txt";
ArrayList <String> studentList = new ArrayList <String> ();
try{
FileReader fr = new FileReader(file);
Scanner sc = new Scanner(fr);
sc.useDelimiter(";");
while(sc.hasNextLine()){
studentList.add(sc.nextLine());
}
fr.close();
}catch(FileNotFoundException exception){
System.out.println("File " + file + " was not found");
}catch(IOException exception){
System.out.println(exception);
}
return studentList.toArray(new String[studentList.size()]);
}
这就是我填充下拉列表的方式:
public void populate() {
String [] studentList ;
studentList = Question3ReadFile.readFile();
jComboBox_adminNo.removeAllItems();
for (String str : studentList) {
jComboBox_adminNo.addItem(str);
}
}
但是,我现在的问题是下拉列表中的选项显示文本文件中的整行。它不只显示管理员号码。我已经尝试使用 useDelimiter 了。我应该用那个吗?
如有任何帮助,我们将不胜感激。提前致谢。
Rince帮助检查。
public class Question3ReadFile extends Question3 {
private String adminNo;
public Question3ReadFile(String data) {
String[] tokens = data.split(";");
this.adminNo = tokens[0];
}
public static String[] readFile(){
String file = "output.txt";
ArrayList <String> studentList = new ArrayList <String> ();
try{
FileReader fr = new FileReader(file);
Scanner sc = new Scanner(fr);
while(sc.hasNextLine()){
studentList.add(new Question3ReadFile(sc.nextLine()));
}
fr.close();
}catch(FileNotFoundException exception){
System.out.println("File " + file + " was not found");
}catch(IOException exception){
System.out.println(exception);
}
return studentList.toArray(new String[studentList.size()]);
}
最佳答案
hasNext 和 next 而不是 hasNextLine 和 nextLine
public static void main(String[] args) {
String input = " For example, 134567H;Gabriel;24/12/1994;67;78;89";
Scanner scanner = new Scanner(input);
scanner.useDelimiter(";");
String firstPart = null;
while(scanner.hasNext()){
firstPart = scanner.next();
break;
}
String secondPart = input.split(firstPart)[1].substring(1);
System.out.println(firstPart);
System.out.println(secondPart);
scanner.close();
}
关于java - 如何使用 useDelimiter 过滤信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16082667/
根据java: public Scanner useDelimiter(String pattern) Sets this scanner's delimiting pattern to a patt
给出下面的代码,它输出: Feed a chunk of data here: I have found: 0 words; 0 ints; 0 booleans; 如果我键入
我在从文本文件读取数据时遇到问题。我正在使用 useDelimiter 将字符串的一部分存储到映射中。我使用的模式稍后将在文件中重复,并且扫描仪将不会进一步读取。 示例文本, a:1、(值)、b:2、
所以我查看了几个相关的问题,但似乎仍然找不到我的答案(我猜是因为它很具体)。我正在尝试在 Java 中使用 Scanner.useDelimiter 方法,但无法让它正常工作...这是我的困境...
我正在尝试使用 useDelimiters() 按特定分隔符而不是按每个新行分割文本文件。例如,在下面的代码中,我有一个文本文件输入,我想在句号处将其分成两个: hellothere stop.her
useDelimiter 无法识别竖线,但可以识别其他字符。 这不起作用: scan.useDelimiter("|"); 这确实有效: scan.useDelimiter(","); 其余代码: S
我有以下代码,请记住我刚刚开始学习一门语言,并且一直在寻找相当简单的练习。欢迎编码礼仪和批评。 import java.util.*; import java.io.*; public class T
所以,我想做的是从文本文件中获取信息, For example, 134567H;Gabriel;24/12/1994;67;78;89 然后我在下拉列表中仅显示第一个管理员号码,而不是整
我有一串 float ,例如 1.0000\0.0000\0.0000\0.00000\1.0000\0.0000\0.0000 我需要解析它。我使用 Java 扫描仪并使用反斜杠作为分隔符。它已经以
例如我有这个代码 String input = "1 fish 2 fish red fish blue fish"; Scanner s = new Scanner(input).useDel
我正在尝试创建一个程序,使用分隔符分隔字符串输入(Windows 路径)。然而我的程序似乎忽略了分隔符。 我期待的结果: Skriv in sökvägen: C://Windows/System/
这个问题已经有答案了: Read CSV with Scanner() (8 个回答) 已关闭10 年前。 我找不到解决此问题的正确方法: 我有一个 .csv 文件,其中包含最后一个值以逗号分隔的值列
我想计算两个日期之间的天数,但我在解析输入流时遇到问题...... 如果我输入“22 1 2013”这样的日期,效果很好,但我遇到了困难: 2013年1月22日 2013年1月22日 2013年1
我需要将特定 Scanner 对象的分隔符设置为:换行符、任意数量的连续空格、逗号字符和美元符号字符。 我正在使用此代码: Scanner s = new Scanner("just\ndo
我环顾四周,但找不到任何有用的东西。基本上我正在编写一段代码来获取数字列表作为输入。但是我希望用逗号作为分隔符输入数字。 这是我的代码片段。 import java.util.Scanner; pub
这个问题在这里已经有了答案: How do I use a delimiter with Scanner.useDelimiter in Java? (3 个答案) 关闭 5 年前。 这就是我到目前
我正在开发一个Java项目,我试图让useDelimiter删除除“=”和“,”之间的文本之外的所有内容。例如,在文件的第一行我想保留“ThermostatNight”。 文本文件如下所示: E
我有一个如下所示的文本文件: Event=ThermostatNight,time=0 Event=LightOn,time=2000 Event=WaterOff,time=10000 Event=
当我们像这样将字符串传递给扫描器时,我们得到“ab”作为控制台输出: public static void main(String []args){ Scanner sc = new Sca
希望解析以下文本文件: 示例文本文件: text entered by useradditional text entered by user 我想解析上面的文本,这样我就可以拥有三个变量: v1 =
我是一名优秀的程序员,十分优秀!