Question

I am accessing Data from a .txt file via Scanner, the .txt file looks like:

001 City     5000.00 101
002 Capital  4000.00 101
003 Farm     1000.00 102

I need this to be reformatted like this:

Capital
City
Farm

101 9000.00
102 1000.00

My Code Sor Far:

try{
      Scanner scan = new Scanner(new File("C:\\Users\\Dustin\\Desktop\\test.txt"));
      String[] strArray = new String[3];
      TreeSet<String> ts = new TreeSet<String>(); 
      while(scan.hasNextLine()){
          ts.add(scan.nextLine());
          //strArray[0] = scan.nextLine();
          //StringTokenizer stkn = new StringTokenizer(strArray[0],",");
          //System.out.println(stkn.nextElement());
          //System.out.println(strArray[0]);
      }
      for(String s : ts){
      System.out.println(s);
      }catch(Exception e){
       System.out.println("Error: "+e.getMessage());
}

But I don't know how to access individual items of this data and add to sets, and then add these the 101 items together. I am very new at this, any help would be appreciated.

Was it helpful?

Solution

You could try and do as follows:

Read the data from the text file and split it using the .split(String regex) method:

try{
      Scanner scan = new Scanner(new File("C:\\Users\\Dustin\\Desktop\\test.txt"));
      TreeSet<String> ts = new TreeSet<String>();
      while(scan.hasNextLine()){
          String[] strArray = scan.nextLine().split("\\s+");    //the will yield an array having {"001", "City", "5000.00", "101"}

Since you want to group by ID, you could take a look at the HashMap and add this line: Map<String, Double> map = new HashMap<String, Double>();

TreeSet<String> ts = new TreeSet<String>();
Map<String, Double> map = new HashMap<String, Double>()

When you read the data, from the file, you will check to see if you have already read a value with the same ID:

if(map.containsKey(strArray[3]))
{
    map.put(strArray[3], map.get(strArray[3]) + Double.parseDouble(strArray[2].trim()));
}
else
{
    map.put(strArray[3], Double.parseDouble(strArray[2].trim());
}

And add the data to your set:

ts.add(strArray[1]);

Once that you are ready, you will need to iterate over the set and then, print its content. Once that you will have that done, you will need to iterate over the map and print the key-value pairs.

That being said, since neither data structure caters for ordering, I would recommend that you include the ID's of the values you put in your tree set for clarity:

ts.add(strArray[1] + "(" + strArray[4] + ")");

OTHER TIPS

For your case, first read each line of the file, then pass it into an object (I called MyRecord):

Scanner scan = new Scanner(new File("C:\\test.txt"));
              TreeSet<String> ts = new TreeSet<String>(); 
              while(scan.hasNextLine()){
                  ts.add(scan.nextLine());
              }
              ArrayList<MyRecord> records = new ArrayList<MyRecord>();
              for(String s : ts){
                  System.out.println(s);
                  StringTokenizer st = new StringTokenizer(s," ");
                      MyRecord record = new MyRecord();
                      record.setOrder(st.nextElement().toString());//001 
                      record.setName(st.nextElement().toString());//City
                      record.setNumber(st.nextElement().toString());//5000.00
                      record.setGroup(st.nextElement().toString());//101
                      System.out.println(record.toString());
                      records.add(record);
              }

After that, you have a ArrayList with many MyRecord, each MyRecord is a row in file
Loop in this ArrayList, you're able to do your requirement.
Do the rest by your self. If problem -> comment <- I will support.

class MyRecord{
    String order, name, group, number;
    //when calculate number, you need to convert number to number type such as long, double, etc
    public String getOrder() {
        return order;
    }
    public void setOrder(String order) {
        this.order = order;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGroup() {
        return group;
    }
    public void setGroup(String group) {
        this.group = group;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "order: " + order + "| name:" + name + "| number:" + number +"| group" + group;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top