Skipping Certain Characters in a Read File Java

seven Replies - 11230 Views - Terminal Post: 29 September 2011 - 03:twenty PM Rate Topic: - - - - -

#1

  • D.I.C Head

Reputation: 0

  • View blog
  • Posts: 181
  • Joined: 21-September 10

Text file splitting at certain characters

Posted 29 September 2011 - 08:40 AM

Hi guys. I am having a problem. I created my own link list class that is going to agree iii Strings where one string is a title another cord is a first proper noun and the 3rd cord will be a last proper noun. I will eventually get through and sort these. The lines that I have to divide are in a text file and each line will look similar this [email protected]@lastname. I need to split up the line at the '@' characters but, I want the first give-and-take in each line to be stored as the title in my linked list, the second word stored as my firstname in my linked list, etc.... I have the linked list form build and it works. I just want to know what would be best to split these up and exist able to set them to my linked list attributes. I believe Scanner would be best and I tried it but, I can't seem to go it to split up it correctly.

                  public void readFile(File f) throws Exception{ 		Scanner browse = new Scanner(f); 		browse.useDelimiter("[@\n]"); 		String g; 			while(scan.hasNext()){ 				scan.next().split up("@"); 				g = scan.next(); 				System.out.print(one thousand); 			} 		}                


Is This A Skilful Question/Topic? 0

  • +

Replies To: Text file splitting at sure characters

#two blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: two,547
  • Joined: 05-May 05

Re: Text file splitting at sure characters

Posted 29 September 2011 - 08:42 AM

Read in each line using Scanner.nextLine and separate it on the ampersand.


#3 sport10 User is offline

  • D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 181
  • Joined: 21-September 10

Re: Text file splitting at certain characters

Posted 29 September 2011 - 12:17 PM

Would I all the same exist using the while loop similar I accept above? Something like:

While(browse.nextLine){  browse.split("@");  then store information technology... }                


#4 Fuzzyness User is offline

Reputation: 669

  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Text file splitting at certain characters

Posted 29 September 2011 - 12:27 PM

I am not too experienced using a delimiter like that but why not just use the @ as a delimiter, set it up to go past that for then scan.next() will get title name, telephone call scan.next() to get showtime proper noun and scan.adjacent() to get concluding proper noun, then when y'all call the adjacent scan.next() it will get everything until the next @. So even if it is on a new line will go there. put it in a while loop and get?

Scanner browse = new Scanner(f); scan.setDeilimter("@"); String title; Cord get-go; String last; while(browse.hasNext()) {      title = scan.side by side();      beginning = scan.side by side();      last = scan.side by side();      System.out.println(championship + " - " + last + ", " + first + "\n"); }                

So if you have a file like this:
[electronic mail protected]@DIC
[e-mail protected]@DIC
D.I.C. [e-mail protected]@DIC

The output would like this:
expert - DIC, fuzzyness
good - DIC, blackcompe
D.I.C. caput - DIC, sport10

I just don't see a reason why you need to read in the entire line and split the @ if you know exactly how the format is going to exist.

This post has been edited by Fuzzyness: 29 September 2011 - 12:28 PM


#5 sport10 User is offline

  • D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 181
  • Joined: 21-September ten

Re: Text file splitting at sure characters

Posted 29 September 2011 - 02:24 PM

Well somewhen I will have to build up a linked list (that I made a class for myself, I am not using the built in JAVA class). What I want is to get the first part of each line which would be the title and make a new "Node" with the content of that node being the championship that is dissever from each line. I am just having bug getting the three parts from each line seperated and being able to setup a linked list with the three parts.


#6 CasiOo User is offline

Reputation: 1578

  • View blog
  • Posts: three,551
  • Joined: 05-April eleven

Re: Text file splitting at certain characters

Posted 29 September 2011 - 02:47 PM

Fuzzyness already gave yous a full working example of doing just that. Study his lawmaking, and lookup what you do not understand :)

I will just throw in this example as well. It uses a couple of dissimilar techniques

BufferedReader reader = new BufferedReader( new FileReader( myFilePath ) ); //Setup the reader  while (reader.ready()) { //While there are content left to read 	Cord line = reader.readLine(); //Read the adjacent line from the file 	String[] tokens = line.split( "@" ); //Split the string at every @ character. Place the results in an array. 	 	for (String token : tokens) //Iterate through all of the constitute results 		System.out.println(token); }  reader.close(); //Stop using the resource                


#7 sport10 User is offline

  • D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 181
  • Joined: 21-September x

Re: Text file splitting at certain characters

Posted 29 September 2011 - 03:01 PM

Thanks for the assist. I have it pretty much working except when I endeavor to get the 3rd cord (lastname) it is reading the new line grapheme as well so my output looks something like this:
proper name

anothername

yetanothername

I don't desire the whitespaces (the new line grapheme) to exist included. Is there an easy fashion of removing that?


#viii blackcompe User is offline

Reputation: 1159

  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Text file splitting at certain characters

Posted 29 September 2011 - 03:xx PM

Ii reasons why yous shouldn't use Scanner.next.

(1) Suppose the scanner is current positioned at the concluding ampersand of the get-go line. When I call Scanner.side by side it will return the last proper name and the title on the side by side line, since they're separated by ampersands.

(2) Permit'southward Suppose we have the aforementioned scenario equally higher up, but instead Scanner.next returns but the last name on the first line. This wouldn't actually happen, only lets imagine it did. '\r\northward' appears later on the final name and Scanner.next will choice that up to and yous'll become actress white space if you endeavour printing the text to the console (which seems to exist what your experiencing now). Scanner.nextLine discards those characters.

static void scanFile(Scanner scan) { 	String line; 	Cord championship; 	String offset; 	String last; 	while ( scan.hasNextLine() ) { 		line = scan.nextLine(); 		String tokens[] = line.split("@"); 		championship = tokens[0]; 		kickoff = tokens[1]; 		last = tokens[2]; 		System.out.println(championship+" "+commencement+" "+concluding); 	} }                


  • Coffee

scarbroughmandre.blogspot.com

Source: https://www.dreamincode.net/forums/topic/249110-text-file-splitting-at-certain-characters/

0 Response to "Skipping Certain Characters in a Read File Java"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel