Usage of Substring()

Getting out of bounds exception

Page 1 of 1

8 Replies - 4186 Views - Last Post: 22 October 2008 - 07:14 PM Rate Topic: -----

#1 ritab68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-October 07

Usage of Substring()

Post icon  Posted 21 October 2008 - 01:57 PM

Hello,

Please forgive me for the simplicity of the code that follows. I've been
coding java for about 3 weeks now... experienced with many other
languages but never had the oppertunity till now.

I'm struggling with the usage of the substring() function. I'm sure it
is just that I'm not stating the syntax correctly but I can't seem to
find what I'm looking for. Basically, I want to interrogate a string of
text that is read in from an input file, and depending on what is
contained in the string write an equal LISP statement. Should
be simple right???? Yeah... that's what I thought too.

Here's where I'm at now...

As soon as it hits the substring() statement I get an out of bounds error.
I want to say, "... look in positions 1-4 and if //// is present, read the next
line..."

Any help would be greatly appreciated.
Thanks,
Rita.

try {
  in = new BufferedReader(new FileReader(inFileName));
 out = new BufferedWriter(new FileWriter(clFileName,true));

 // write header information
	clLine = "# script to convrert control file to cl code";
	out.write(clLine);
	outLineNum = outLineNum + 1;
		
	while(in.readLine() != null){

	 read = in.readLine();
	 linenum = linenum + 1;
			   
			   
// skip over comments and blank lines
	if (read.substring(1,4)== "////") {
				   clLine = "";
				}
			   else {
		   if (read.substring(1)== " ") {
			  clLine = "";
			};
	:
	:
	:



Is This A Good Question/Topic? 0
  • +

Replies To: Usage of Substring()

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,726
  • Joined: 20-September 08

Re: Usage of Substring()

Posted 21 October 2008 - 02:11 PM

Firstly, you can't compare String with ==. It needs to be equals, or you're only comparing references

Secondly, don't make assumptions about the length of the String - check it first

Thirdly, read the API docs carefully for substring:

http://java.sun.com/.../java...ing(int,%20int)
Was This Post Helpful? 0
  • +
  • -

#3 ritab68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-October 07

Re: Usage of Substring()

Posted 21 October 2008 - 02:45 PM

View Postg00se, on 21 Oct, 2008 - 02:11 PM, said:

Firstly, you can't compare String with ==. It needs to be equals, or you're only comparing references

Secondly, don't make assumptions about the length of the String - check it first

Thirdly, read the API docs carefully for substring:

http://java.sun.com/.../java...ing(int,%20int)


Thanks for your assistance, g00se.

when I change the first substring to

(read.substring(0,3) = "////")

Eclipse tells me that the left side must be a variable... all I want to do is check it, is there a way to do this?

Thanks,
Rita.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,726
  • Joined: 20-September 08

Re: Usage of Substring()

Posted 21 October 2008 - 03:23 PM

Quote

(read.substring(0,3) = "////")


When i say 'equals' i mean the method equals(), so that should be

(if "////".equals(read.substring(0, 3))) 


Was This Post Helpful? 0
  • +
  • -

#5 ritab68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-October 07

Re: Usage of Substring()

Posted 21 October 2008 - 05:44 PM

View Postg00se, on 21 Oct, 2008 - 03:23 PM, said:

Quote

(read.substring(0,3) = "////")


When i say 'equals' i mean the method equals(), so that should be

(if "////".equals(read.substring(0, 3))) 



Thanks g00se! It appears that it may be working now. I know I should check for the length, but this is for a class room assignment and I'm not putting that much effort into it... quick and dirty..

Rita.
Was This Post Helpful? 0
  • +
  • -

#6 ritab68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-October 07

Re: Usage of Substring()

Posted 22 October 2008 - 08:34 AM

one more Q... when interogating a string using the startsWith method of String, is white space considered? For example, if I had a line like the following, where would startsWith begin?

	rita is a new to the java language. 


would it start in position 0 (space) or would it start at position 5 the r?

Thanks Again!
rita.

This post has been edited by ritab68: 22 October 2008 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Usage of Substring()

Posted 22 October 2008 - 05:01 PM

View Postritab68, on 22 Oct, 2008 - 08:34 AM, said:

one more Q... when interogating a string using the startsWith method of String, is white space considered? For example, if I had a line like the following, where would startsWith begin?

	rita is a new to the java language. 


would it start in position 0 (space) or would it start at position 5 the r?

Thanks Again!
rita.

no but you can always:

String trimString = originalString.trim();

that will give you in trimString the String in origibalString without leading/trailins spaces and tab
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2728
  • View blog
  • Posts: 15,804
  • Joined: 24-September 08

Re: Usage of Substring()

Posted 22 October 2008 - 06:48 PM

you guys and your fancy stuff. :P personally, what i wouldve done is:

firstly, read the file using scanner (opening using file, then scanner)
second, if you know what line in the file, and assuming there is only one "////" or youre reading from the last one anyway, use

Char backslash = '/'	   //or 47 in numbers

String string = read.nextLine();
int index = lastIndexOf(backslash);
String string_sub = string.substring(backslash - 3, backslash);
if (String "////".equals(string_sub){
	 int length = string.Length();

	 String string_sub2 = string.substing(backslash, length - 1);
   
	 System.out.println(string_sub2);
}


or something (notice: code may not be 100% correct, but it was an estimate off the top of my head)
Was This Post Helpful? 0
  • +
  • -

#9 ritab68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-October 07

Re: Usage of Substring()

Posted 22 October 2008 - 07:14 PM

Thanks! I'll give this a go.

Have a great night,
rita.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1