• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting the first four characters from a line

 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to search a file for a specific string that has been entered from a text field, find it and then display the first four characters from the line that it is in.....
here is the code that I have now....
if(e1.getSource()==checkButton){
String SrNo;
SrNo = String.valueOf(SerNo.getText());
try {
BufferedReader in = new BufferedReader(new FileReader("wavail0.txt"));
String str;
while ((str = in.readLine()) != null) {

if (str.indexOf(SrNo) >= 0) {
Quantity.setText("this works");
}
}
in.close();
} catch (IOException e) {
System.out.println("no it didn't work");
}
}
now this will read the line in...I just need to make it so it will show the first four characters in a textfield also.
Any and all suggestion would be great!!
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might be misunderstanding the question, but wouldn't
String first4Chars = SrNo.substring( 0 , 4 );
do the trick? You could even break that String into a char[] if you need to deal with the characters individually.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will that just give the first four characters of the document or will that do it line by line?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're storing each line in the variable "str" as you read it, right? So all you need to do is take the first 4 characters of "str". Surely what Jason meant was

to be inside the loop.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! Wrong variable name. Yup, what the big E said.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks for the help that did the trick perfect!!
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this topic is still opened and this question still pertains to the original question I'm going to stick with this Post . Just wondering how would I go about getting all the characters up to a comma as compared to the first four characters?
The reason I ask this is for now the length of the field that will be read in is only four digits long. After that it will go to a comma something like this...0085,6545,231. However there might be an instance were the first digits might be -0034,. This is an error field and it would only read in -003, which would be enough to know that it is an error, but it's not reading in the whole record. I wanted to correct this before it became a problem.

Thanks in advance.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're still reading the file a line at a time, and efficiency isn't a huge concern, you might use java.util.StringTokenizer, which is an easy way to break a String into pieces.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea I'm just going to read to the first comma any output that number. that should do it.
thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!