We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

readLine and goto a position

ncoo
ncoo asked
on
Medium Priority
467 Views
Last Modified: 2010-03-31
Hi

I'm kinda new to java, i'm hoping there is a simple answer to this:

I've created in java the following,

read textfile (using the method readLine),
once each line is read in, I was wondering if I could go to a specific position in the string (i.e. char 15 and 16) and only dealing with these.  If it helps a single line is as follows: " adtfg,678,dfg,62,yud,dud " so in that line I only want to deal with the 62.
I was hoping i could treat the line as an array and just skip to the number 62 but it keeps causing errors when I try to do this. And saving this value (62) as an int.

Hope that makes sense, thankyou for reading hope someone can help.
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
// read line
String s = in.readLine();
// only want string from postition 15
s = s.substring(15);

CERTIFIED EXPERT
Top Expert 2016

Commented:
You can use

line = line.substring(line.indexOf("62"));

or

line = line.substring(14);

Commented:
String s = in.readLine();
s = s.substring(15,16);
int i = Integer.parseInt(s);
CERTIFIED EXPERT
Top Expert 2016

Commented:
int i = Integer.parseInt(line.substring(14, 16));
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
StringTokenizer and split() are other possible options.

String tokens[] = line.split(",");
int i = Integer.parseInt(tokens[3]);

or

StringTokenizer st = new StringTokenizer(line, ",");
st.nextToken();
st.nextToken();
st.nextToken();
int i = Integer.parseInt(st.nextToken());
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> int i = Integer.parseInt(line.substring(14, 16));

gee, thats original :D
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>gee, thats original :D

Quite correct - it's original in the sense that so far i am the only one to have posted the correct string indexes
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
That code was already posted, if you have a clarification to make about it then state what that clarification is.
Posting a duplicate line of code just confuses the thread.

And the line in the question is just an example so the actual index values are not really relevant anyways.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>That code was already posted...

It's much clearer to post working code than to enter into explanations

>>...so the actual index values are not really relevant anyways

LOL
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> It's much clearer to post working code than to enter into explanations

You're kidding right.
CERTIFIED EXPERT
Top Expert 2016

Commented:
What's more, the last time i pointed out inaccuracies in someone's code, i got accused of 'critisism':

https://www.experts-exchange.com:8080/Programming/Programming_Languages/Java/Q_20926569.html
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
ncoo,

If you're lines have a fixed format, ie. the value you need to extract is always at the same position in the line then you can use the technique suggested by fave_17 to extract the value.
If the lines are not fixed, and the column position may vary then you will need to tokenise the string using split() or StringTokenizer.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Either way, this will allow you to access the retrieved numbers as an array:

      public int[] getNumbers(String fileName) {
            BufferedReader r = null;
            List numbers = new ArrayList();
            try {
                  r = new BufferedReader(new FileReader(fileName));
                  StreamTokenizer tok = new StreamTokenizer(r);
                  while (tok.nextToken() != tok.TT_EOF) {
                        if (tok.ttype == tok.TT_NUMBER) {
                              numbers.add(new Integer((int)tok.nval));
                        }
                  }
                  int[] result = new int[numbers.size()];
                  for(int i = 0;i < result.length;i++) {
                        result[i] = ((Integer)numbers.get(i)).intValue();
                  }
                  r.close();
                  return result;
            }
            catch (IOException e) {
                  e.printStackTrace();
                  return null;
            }
      }
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> this will allow you to access the retrieved numbers as an array

Must have missed that requirement in the question :)

Author

Commented:
CEHJ,

That is the prefered way of doing it through arrays, since the length of the desired number i want can sometimes be one digit (eg adtfg,678,dfg,6,yud,dud in this case i require 6) I'm just having some difficulty setting up the code you've supplied.

When I try to run it I just get "illegal start of expression".  Can you just make it so it prints each array to the screen one by one, because then I'll be able to see what's going on better and addapt it more easily, since I haven't come accross the class Token before and not totally sure what it does.

Thanks Everyone,
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
if you want an array containing values for each lines then simply add each value to a List


String tokens[] = line.split(",");
values.add(tokens[3]);

Author

Commented:
Thanks Objects, that does exactly what i wanted.

It looks so simple though, to work it all out.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
CERTIFIED EXPERT

Commented:
Grade B - ROTFL! :))
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>That is the prefered way of doing it through arrays

Yes, it certainly is convenient to have the numbers in an array

>>When I try to run it I just get "illegal start of expression".  

Probably because you put it in the wrong place
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.