1

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 JTextField which i am able to figure out.

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

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.

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?

Thanks in Advance

Rockwire
  • 127
  • 1
  • 2
  • 9
  • To populate JComboBox: http://stackoverflow.com/questions/1291704/how-do-i-populate-jcombobox-with-arraylist – Patrick Oct 28 '11 at 09:52
  • First of all, welcome to Stackoverflow. Second, if you have two questions, like "How do I get first token of a line", and "How do I populate a JComboBox", consider making them two different questions instead of packing them together. It will make future searches for someone with either problem easier. – Patrick Oct 28 '11 at 16:12

3 Answers3

1

If you know how to read lines from a text file you can split each line by a delimiter, using the String.split function. 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 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.

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.

Patrick
  • 17,669
  • 6
  • 70
  • 85
1

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

  • 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 0 to get the information you need.
  • Then it's just a case of adding split_string[0] to the JComboBox.

The documents are a great help:

String JComboBox

Carl Winder
  • 938
  • 8
  • 18
1

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

There is a tutorial about JComboBox. The Java Swing classes are based on Model/View, so you have to fill the strings into the Model of the 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.

  • Thanks for your answer. Do you any related code for doing this? – Rockwire Oct 28 '11 at 11:08
  • You'll have to write the code yourself. But if something is not yet clear in my answer, feel free to ask. – S.L. Barth is on codidact.com Oct 28 '11 at 11:10
  • Do you have any link which i can refer, which would help me accomplish above task. Thanks for help. – Rockwire Oct 28 '11 at 12:52
  • The tutorial that I linked to has some demos. Look at the constructor for [CustomComboBox](http://download.oracle.com/javase/tutorial/displayCode.html?code=http://download.oracle.com/javase/tutorial/uiswing/examples/components/CustomComboBoxDemoProject/src/components/CustomComboBoxDemo.java), for example. I just realized that you don't have to work with the ComboBoxModel directly, in a straightforward case like yours you can pass the values directly to the [constructor](http://download.oracle.com/javase/6/docs/api/javax/swing/JComboBox.html#JComboBox%28java.util.Vector%29). – S.L. Barth is on codidact.com Oct 28 '11 at 12:59