0

enter image description here

I'm not looking for a complete answers just help on how to start it or maybe some references I could look at that may help me with this. Ok so I have to populate the JComboBox (accountnumber) from a text file. The txt files reads as:

1231<>Jack Williams<>2015/1/21<>463.02
1232<>Jane Brown<>2015/1/21<>13510.54
1233<>Paul Gonzales<>2015/1/22<>680.17
1234<>Jian Chen<>2015/1/22<>1117.54
1235<>Lily Makki<>2015/1/22<>1124.89
1236<>Michael Lopez<>2015/1/23<>800.0
1237<>Jose Alvarez<>2015/1/23<>607.21
1238<>Tina Lin<>2015/1/24<>11077.0

It reads as acctNumber<>CustomerName<>openDate<>balance

How would I go about starting this? Which would be easiest to split the 4 variables. array/arraylist/hashmap etc.?

I'm not familiar with file I/O. and trouble with collections so this is the only part I'm stuck on.

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AccountUtility {
List<String> accountsInfo = new ArrayList<>();
BufferedReader in;
File file  = new File("accounts.txt");
public AccountUtility(){
    ReadFile();
}
public void ReadFile(){
    String nxtLine = " ";

    try{
    in = new BufferedReader(new FileReader(file));

    while(nxtLine != null){
    nxtLine = in.readLine();
    accountsInfo.add(nxtLine);
    }
    for(String items : accountsInfo)
    System.out.println(items);
    in.close();
    }catch(IOException ex){
    }

}
public static void main(String[] args) {
    AccountUtility ut = new AccountUtility();

}

}

so i decided to use a list , this is just my accountutility class I just added a mainmethod so i can test just this class and the result when i Run it comes to

1231<>Jack Williams<>2015/1/21<>463.02
1232<>Jane Brown<>2015/1/21<>13510.54
1233<>Paul Gonzales<>2015/1/22<>680.17
1234<>Jian Chen<>2015/1/22<>1117.54
1235<>Lily Makki<>2015/1/22<>1124.89
1236<>Michael Lopez<>2015/1/23<>800.0
1237<>Jose Alvarez<>2015/1/23<>607.21
1238<>Tina Lin<>2015/1/24<>11077.0
null

How do i split an list using a delimiter?

3 Answers3

1

You're primary identifier is the account number, from this you need to be able to ascertain the account details.

This would lead me to use some kind of Map.

I would then create an Account class which held all the information in a simple, easy to use class, which provided appropriate setters and getters.

This would then lean me to the fact that I wouldn't actually need the Map, because all the information I need was in the Account class, so instead, I would simply create a ListCellRenderer for the combo box that would be capable of taking the account number from an instance of the Account class and display it appropriately...

This would mean I'd only need a List or a ComboBoxModel to hold the account details

Take a closer look at How to Use Combo Boxes for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @JustinPancakeRazon, `I would simply create a ListCellRenderer for` - and just using a custom renderer will break the default functionality of the combo box since you can no longer use the keyboard to select items in the combo box. See [Combo Box With Custom Renderer](http://tips4java.wordpress.com/2013/11/17/combo-box-with-custom-renderer/) for more information and a possible solution. – camickr Jun 06 '14 at 02:22
  • @camickr You know, in 15 years, that's never been a problem, not saying it's not a nice solution ;) – MadProgrammer Jun 06 '14 at 02:24
  • 1
    `that's never been a problem,` - what do you mean it has never been a problem? Are you saying the users have never noticed/complained that the functionality has been broken? I find that strange because advance users prefer to keep their hands on the keyboard (when possible) instead of switching from keyboard to mouse back to keyboard to enter data on a form. – camickr Jun 06 '14 at 03:10
  • @camickr Nope, never really had some whinge about that, most people want auto complete, which `JComboBox` doesn't really do, which ends up in a different component - that's just what I've experienced ;) – MadProgrammer Jun 06 '14 at 03:12
1

To display an Object, Swing components will use the toString() method the Object placed in it.

One approach is to create a Data class that holds the name, ID, etc., implement toString() to display what you want (in your case, the Account Number), and then put a list of these objects in your JComboBox.

Then on change of selection in the combo, get the selected item, cast it to the data class, and then call getDate(), getName(), etc. to populate the textfields.

If you want to actually show the extra details of the Customer in the combo (after all, who really knows the person by account number?), then take a look at one approach here: DetailedComboBox

Community
  • 1
  • 1
splungebob
  • 5,357
  • 2
  • 22
  • 45
  • `toString` - IMHO, should be used to display diagnostic and debug information, take a look at `java.util.Date` for example. The information provided by `toString` will either be too complex or too simple for every case. It is the responsibility of the renderer for the Swing component to decide how best to display the information presented by the Object, IMHO – MadProgrammer Jun 06 '14 at 00:52
-1

Justin, I'm in the same boat and created a similar program that reads from text file. There was an excellent given for cells (e.g. Excel or Sql), but need to have it read from a text file. I'm thinking:

String tmp [] = line.split ("<>");

this will output data from each break.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72