5 Replies - 9680 Views - Last Post: 07 May 2010 - 07:35 AM Rate Topic: -----

#1 jinx3y   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-April 10

removing delimiters from arrayList

Posted 04 May 2010 - 07:46 AM

Hi,
I need some help with an arraylist. My assigment is to populate an arraylist with lines of text from an input.txt file. Each line has 11 data fields separated by a "-".

Here is the code:

import java.util.*;
import java.io.*;
//import javax.swing.*;

public class readin2{

    public static void main(String args[]) throws IOException{
        ArrayList<String> empList = new ArrayList<String>();
        File file = new File("EmpDB.txt");
        Scanner scan = new Scanner(file);
        int i=0;
        
        //populate List
        while(scan.hasNext()){
            empList.add(scan.nextLine());
            //post increment i
            i++;
            //use i value as line numbers and output each line
            System.out.println(i+") "+empList.get(i-1));
        }
    }
}



this is the output:
1) Currier-Glen-A-123456789-NTAD-F-2-0-1500-12-0
2) Public-John-Q-098765432-SALE-P-5-25-1000-0-120
3) Doe-Jane-S-938475610-HRMG-F-3-0-2000-9-0
4) Doe-John-H-069783049-CCEO-F-4-0-2500-12-0
5) Page-Jimmy-F-994857685-SALE-P-5-30-1000-0-360



...exactly the way it looks in the text file.

So the question remains: how do I "remove" the "-" and leave it as a space?(this is to feed another method in a subclass for manipulation in display.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: removing delimiters from arrayList

#2 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: removing delimiters from arrayList

Posted 04 May 2010 - 08:12 AM

scan.nextLine().replace('-', " ");

will replace all hyphens with spaces. Something you may want to look at is the split method:

String[] tokens = scan.nextLine().split("-");

this splits up your string at the hyphens and returns an array of all the tokens.
Was This Post Helpful? 1
  • +
  • -

#3 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: removing delimiters from arrayList

Posted 04 May 2010 - 08:14 AM

you can use the replaceAll method of the String method:
http://java.sun.com/...ava.lang.String, java.lang.String)
your regex is the "-".
tou should replace it with whatever you'd like.
space, empty string etc..
consider that example:
String str = "Currier-Glen-A-123456789-NTAD-F-2-0-1500-12-0";
str = str.replaceAll("-", " ");
System.out.println(str);

Was This Post Helpful? 1
  • +
  • -

#4 jinx3y   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-April 10

Re: removing delimiters from arrayList

Posted 04 May 2010 - 10:02 AM

Outstanding!

Next question (and this is where I really have problems)

I must now call the method to return each value of the array created using .split()

I thought that this would work:

public class database {

    public database()throws IOException{

    ArrayList<String> empList = new ArrayList<String>();
    File file = new File("EmpDB.txt");
    Scanner scan = new Scanner(file);
    int i=0;

        while(scan.hasNext()){
            scan.useDelimiter("-*");
            empList.add(scan.next());
            String a[]= scan.nextLine().split("-");
            i++;
        }

    }

    public fName(){
        return a[1];
    }
}



Of course it doesn't. the database method has a void result. Sigh...I think I've been at this for too long...lol.

This all part of a project that separates full time from part time employees and calculates salaries (everything based on the data fields in the input file - .split has allowed me to index them). The database class is as listed above. I have an additional "employee" abstract class and two extensions of it: FullTimeEmployee and PartTimeEmployee (used for salary calculations and toString overriding. All this is run by the main class "test" and output (formatted) goes to a file with the use of ...io.* and ...swing.*.

This is an intro class (So it says) and blah, blah, blah (not gonna rant, I really need the help - I'm lost in this).

Thanks

Attachments are the source files for comparison.

Attached File(s)

  • Attached File  Proj2.zip (3.2K)
    Number of downloads: 159

Was This Post Helpful? 0
  • +
  • -

#5 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: removing delimiters from arrayList

Posted 04 May 2010 - 06:30 PM

The problem you are having is one of scope. a[] cannot be seen outside the while loop and empList cannot be seen outside the constructor. Maybe something like this:

public class Database {

  // Now this can be used throughout the class.
  private ArrayList<String[]> empList;

  // add flexibility by passing the file as an argument
  public Database(File inputFile) {
    empList = new ArrayList<String[]>();
    Scanner scan = new Scanner(file);
    while (scan.hasNext()) {
      String line = scan.nextLine();
      String[] splitLine = line.split();
      empList.add(splitLine);
    }
  }

  public String getEmployeeSurname(int id) {
    String[] employeeSplitLine = empList.get(id);
    String surname = employeeSplitLine[0];
    return surname;
  }

}

Was This Post Helpful? 2
  • +
  • -

#6 jinx3y   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-April 10

Re: removing delimiters from arrayList

Posted 07 May 2010 - 07:35 AM

That did it! Thank you. Now to create extended classes to modify the output format...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1