This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes using .length()    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using .length()    " Watch "using .length()    " New topic

using .length()

jake mullet

Joined: May 09, 2001
Posts: 15
Hello JavaGurus, I need to know if everyfile I read in has a 'Z' contained within the last line of the file. in particular it is the 10th byte from the EOF, but each file is different in length, so I'm using the .length() to get the total size but how would I say sBuffer.length() - 10 , then get the charAt() to check if it is a 'Z' thanks for anyone's help
import java.util.*;

public class injRecFix{
static PrintWriter prntWrter;

public static void main(String[] args){
char[] chBuffer =null;
boolean bReadFile = false;
String sINFileName = null;
String sOUTFileName = null;
int i;

InputStream fin;
File outputFile = new File("newPunchFile.txt");

if(sINFileName == null)
sINFileName = "Punch.001";

try {
fin = new FileInputStream(sINFileName);
prntWrter = new PrintWriter(new FileWriter(outputFile));
} catch (IOException except) {
sINFileName = "Punch.001";
try {
fin = new FileInputStream(sINFileName);
} catch (IOException eIO) {
fin = null;
if(fin != null) { // dumping PunchFile into chBuffer
try {
chBuffer = new char[fin.available()];
int iC = 0;
while(iC < chBuffer.length) {
i =;
chBuffer[iC] = (char)i;
bReadFile = true;
} catch (IOException except) {
bReadFile = false;
if (bReadFile) {
// Convert buffer into a string.
String sBuffer = new String(chBuffer, 0, chBuffer.length);
}//end main()
}//end class injRecFix
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
I'm not a guru, but String has a method indexOf() that will return the location of the string or character passed to it. If non is found it returns a -1.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Jake. Let me preface all this by saying, I'm not a Java guru, either.

I should note that the contents of C:\zonk.txt are

The method match() goes to the tenth byte from the end of the file and compares that byte to the byte representation of Z, and returns true if they match, false if they don't.
It's not the soundest solution, but I think it'll work. Hope it helps.
[This message has been edited by Art Metzer (edited May 09, 2001).]
Susan Delph
Ranch Hand

Joined: Feb 24, 2001
Posts: 34
I'm definitely NOT a Java Guru, but....
lastIndexOf('z') will find the last occurrence of 'z' in the String.
int index = sBuffer.lastIndexOf('z');
This will search the String backwards, and index will be set to whatever position the final 'z' of the String is found in.
(If no 'z' is found, index is set to -1, as Richard explained above.)

[This message has been edited by Susan Delph (edited May 09, 2001).]
Susan Delph
Ranch Hand

Joined: Feb 24, 2001
Posts: 34
Actually, dump my above suggestion. This is better -- in my opinion:

It printed out 'true' when I ran this.

[This message has been edited by Susan Delph (edited May 09, 2001).]
I agree. Here's the link:
subject: using .length()
It's not a secret anymore!