java 从文本文件中读取每行的第一个单词,然后填充到组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7927558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:03:15  来源:igfitidea点击:

Read the first word of each line from a text file and then populate to Combo box

javaswing

提问by Rockwire

I have college project where i have to read the first word of every line from text file which looks as follow:

我有一个大学项目,我必须从文本文件中读取每一行的第一个单词,如下所示:

23123123213 Samuel classA
23423423423 Gina classC
23423423423 John classD

The text file will be updated with through 3 JTextFieldwhich i am able to figure out.

文本文件将通过JTextField我能够弄清楚的3 进行更新。

but now i have to populate the JComboboxwith first word(23123123213,23423423423 and 23423423423) of all the lines.

但现在我必须填充JCombobox所有行的第一个单词(23123123213,23423423423 和 23423423423)。

I am new to java, i dont even have hint of how about doing it. I know how to read and write to text files.

我是 Java 新手,我什至不知道该怎么做。我知道如何读写文本文件。

Please could someone help me do this?

请问有人可以帮我做这个吗?

The code so far i came up with is as follows:

到目前为止,我想出的代码如下:

import java.io.*;
public class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("RokFile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {

      String[] delims = strLine.split(" ");
      String first = delims[0];
      System.out.println("First word: "+first);

  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

With u guys help I was successfully able to extract the first string from each line but now how could i populate it in Jcombobox, I mean should i save it somewhere first?

在你们的帮助下,我成功地从每一行中提取了第一个字符串,但现在我如何在 Jcombobox 中填充它,我的意思是我应该先将它保存在某个地方吗?

Thanks in Advance

提前致谢

回答by Patrick

If you know how to read lines from a text file you can split each line by a delimiter, using the String.splitfunction. In that case you get an array, with which you can get the first string by a normal array indexer, the [] operator that is.

如果您知道如何从文本文件中读取行,您可以使用String.split函数通过分隔符分割每一行。在这种情况下,您将获得一个数组,您可以使用该数组通过普通数组索引器(即 [] 运算符)获取第一个字符串。

String hello = "Hello world";
String[] delims = hello.split(" ");
String first = delims[0];


To answer your edit, you populate the JComboBox using one of its constructors, for instance the one that takes an Object array, or using the JComboBox.addItem(Object)function.

要回答您的编辑,请使用其构造函数之一填充 JComboBox,例如采用 Object 数组的构造函数,或使用JComboBox.addItem(Object)函数。

The latter has an example. Regarding the one with the constructor you can either build an array of objects yourself, or use an arraylist to which you add all your elements and then get an array using the ArrayList.toArray()function.

后者有一个例子。关于带有构造函数的那个​​,您可以自己构建一个对象数组,也可以使用一个数组列表,将所有元素添加到其中,然后使用ArrayList.toArray()函数获取一个数组。

回答by Carl Winder

I'm not "down" with Java, however I can give you a few pointers:

我并没有对 Java“失望”,但是我可以给你一些建议:

  • You can read files, and presumably can read a line.
  • Each line is (presumably) separated with spaces so what you need to look up is a String.split function
  • Once you've split a string you will be able to use array index 0to get the information you need.
  • Then it's just a case of adding split_string[0] to the JComboBox.
  • 您可以读取文件,并且大概可以读取一行。
  • 每行(大概)用空格分隔,因此您需要查找的是 String.split 函数
  • 拆分字符串后,您将能够使用数组索引0来获取所需的信息。
  • 那么这只是将 split_string[0] 添加到 JComboBox 的一种情况。

The documents are a great help:

这些文件很有帮助:

StringJComboBox

字符串JComboBox

回答by S.L. Barth - Reinstate Monica

You can get the first word using String.split(), or by using indexOfand substring.

您可以使用String.split(), 或使用indexOfand获取第一个单词substring

There is a tutorialabout JComboBox. The Java Swing classes are based on Model/View, so you have to fill the strings into the Model of the JCombobox.

有一个关于 JComboBox的教程。Java Swing 类基于模型/视图,因此您必须将字符串填充到 JCombobox 的模型中。

EDIT: In response to your edit, suppose you have retrieved the values. Then you can indeed save them to a specific data structure. It would be preferable to make the code that retrieves those values into a separate method. The values returned from that method (in, for example, a List<String>) can then be put into the JComboBox.

编辑:为了响应您的编辑,假设您已检索到值。然后你确实可以将它们保存到特定的数据结构中。最好将检索这些值的代码放入单独的方法中。然后可以将从该方法返回的值(例如,在 List<String> 中)放入 JComboBox。