Solved

readLine and goto a position

Posted on 2004-03-21
21
411 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.
0
Comment
Question by:ncoo
  • 10
  • 7
  • 2
  • +2
21 Comments
 
LVL 92

Expert Comment

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10645254
You can use

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

or

line = line.substring(14);
0
 
LVL 3

Expert Comment

by:fave_17
ID: 10646116
String s = in.readLine();
s = s.substring(15,16);
int i = Integer.parseInt(s);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10646133
int i = Integer.parseInt(line.substring(14, 16));
0
 
LVL 92

Expert Comment

by:objects
ID: 10646141
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());
0
 
LVL 92

Expert Comment

by:objects
ID: 10646143
> int i = Integer.parseInt(line.substring(14, 16));

gee, thats original :D
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10646164
>>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
0
 
LVL 92

Expert Comment

by:objects
ID: 10646174
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10646188
>>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
0
 
LVL 92

Expert Comment

by:objects
ID: 10646195
> It's much clearer to post working code than to enter into explanations

You're kidding right.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

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

http://www.experts-exchange.com:8080/Programming/Programming_Languages/Java/Q_20926569.html
0
 
LVL 92

Expert Comment

by:objects
ID: 10646213
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10646334
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;
            }
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 10647057
> this will allow you to access the retrieved numbers as an array

Must have missed that requirement in the question :)
0
 
LVL 15

Author Comment

by:ncoo
ID: 10647289
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,
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 10647309
> That is the prefered way of doing it through arrays

Is it??

> since the length of the desired number i want can sometimes be one digit

Both example code I posted above handles this.

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());

They just show how to extract one int field as you mentioned in your question, but if you needed more it would be a simple modification.

String tokens[] = line.split(",");
int[] no = new int[] ( Integer.parseInt(tokens[1]), Integer.parseInt(tokens[3]) };
0
 
LVL 92

Expert Comment

by:objects
ID: 10647332
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]);
0
 
LVL 15

Author Comment

by:ncoo
ID: 10647906
Thanks Objects, that does exactly what i wanted.

It looks so simple though, to work it all out.
0
 
LVL 92

Expert Comment

by:objects
ID: 10647928
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10647993
Grade B - ROTFL! :))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10648098
>>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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 86
Python Assistance 7 73
JAVA part two 5 57
how to add new optional parameter to JSP 1 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now