• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to extract the value?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a text file with following format:

I want to read this file and extract the values of x, y, z ,t. So how to extract it? I use regular expression for that, but it's not work, so please help me.

Thanks you very much.

[ September 08, 2008: Message edited by: Tran Tuan Hung ]
[ September 08, 2008: Message edited by: Tran Tuan Hung ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, when you say that "it's not work", that implies that you actually tried something. So, what have you tried? Having a comment that says "readLine one by one" isn't really trying, is it?

Show us what have you tried so far, that doesn't work, and we'll give you some hints to point you to why it doesn't work.

Henry
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't this post in a few other forums ? I've seen this twice before. Be sure you only post in the correct forum and only ask once.

Also, it would help if you posted the ACTUAL code you have tried. And the error you receive. This will help us see if there was a typo or anything like that in your code.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I searched and didn't find any duplicates of this post.

Read carefully what the String#startsWith() method takes as its parameters.
And, as people have said, please post the code and your results.
 
Tran Tuan Hung
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks very much
Here is my code
This is FileFormat.java

Here is class ReadFile.java to read the text file.

Now I have two questions:
1/ When I run the program, it appear the error, help me to correct it.

java.lang.NumberFormatException: For input string: "123 test1y"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at com.goodsw.myroute.ReadFile.read(ReadFile.java:24)
at com.goodsw.myroute.ReadFile.main(ReadFile.java:40)


2/
In the my code, i only set value of x, y , z, t (format.setX(), format.setY()�..) when line startWith(�123�), but I have many, many lines something like this it is line that start with an integer number with three digits:
So what kind of regex I can try? The idea as following:


Please help me to slove my problem, thanks very much.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A NumberFormatException thrown by Integer#parseInt means that whatever you have passed to it is not an integer when printed out. Please check that the lines are actually delimited by tabs. Try the standard regex for whitespace as a delimiter instead of "\\t"; I think it is "\\s", but not certain.

And why are you calling the argument to String#startsWith "regex"? I have already mentioned that point.
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic