0

I have a data in a text file which is in format:

AB-9, Gregson, Brian, R T, Mr

I want to divide the up into and store the first one, AB-9, in a local variable, I have done all of that but the problem currently is that when I print the value of id, it prints with ,. How can I get rid of the extra comma?

Another issue is that the first line in the data is not being read.

public void readData()
{
 File theNewData = new File("new_data.txt");
        Scanner inputFrom = new Scanner(theNewData);
        while(inputFrom.hasNextLine())
        {
             String lineOftext = inputFrom.nextLine().trim();  
                Scanner scanner = new Scanner(lineOftext).useDelimiter(",[ ]*");

                 String id = inputFrom.next();

                 System.out.println(id);
        }

        inputFrom.close();
} 
Aboutblank
  • 697
  • 3
  • 14
  • 31
  • 1
    There is no need to create an additional scanner for each line. Just set the delimeter of the outer scanner to your delimeters + `System.lineSeparator()` and use next() and hasNext() methods to go through the input. – gparyani Apr 16 '13 at 17:04

1 Answers1

0

Check out String.split() Details at: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29

Tralli
  • 398
  • 1
  • 2
  • 12