5 Replies - 352 Views - Last Post: 06 April 2013 - 07:55 PM Rate Topic: -----

#1 JustCags  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-April 13

Importing Text File / Separating Data by ";"

Posted 06 April 2013 - 07:49 AM

This is a homework question. I just began learning Java a few weeks ago. The assignment requires that a text file be imported and the data be analyzed. The problem is the data is not separated by a space, it is separated by a semicolon. I am beginning the program and at this point, I simply want to see the data output separated properly. The output for my current code shows it all as one line without spaces.

package edu.ben.cis200;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lab5 {

	public static void main(String[] args) throws FileNotFoundException {

		File data = new File("c:/javatext/Lab5.txt");

		Scanner input = new Scanner(data);

		while (input.hasNext()) {
			String fileData = input.next();
			
			System.out.print(fileData);
		
		}
		input.close();
	}

}



I have searched for solutions but it seems most people are using a different import technique of which I am not familiar. I have tried an if check that searches for the semicolon, but that doesn't seem to work.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Importing Text File / Separating Data by ";"

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Importing Text File / Separating Data by ";"

Posted 06 April 2013 - 07:55 AM

What you want to do will likely involve regex pattern matching. You could also try the String.split() method.

String.replace(String target, String replacement) might help also :)
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: Importing Text File / Separating Data by ";"

Posted 06 April 2013 - 08:49 AM

Try making line 14 the following

input.useDelimiter("[;\r\n]");

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Importing Text File / Separating Data by ";"

Posted 06 April 2013 - 09:30 AM

Read line by line and then use ths String class split() method

while(input.hasNextLine()) {
   String line = input.nextLine();
   String[] token = line.split(";");
   ... do what ever you have to do with the token array of String
}


Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: Importing Text File / Separating Data by ";"

Posted 06 April 2013 - 10:42 AM

Using a Scanner and calling split on the line read is actually quite redundant - the main function of a Scanner is to tokenise a String. There could be cases where it might be necessary, but this certainly doesn't look like one of them ;)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Importing Text File / Separating Data by ";"

Posted 06 April 2013 - 07:55 PM

Just faster to type than FileInputStream() :whistling:/>

This post has been edited by pbl: 06 April 2013 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1