String Index out of bound error in Eclipse

can't figure out what is wrong with code to give me error message

Page 1 of 1

6 Replies - 2260 Views - Last Post: 08 April 2010 - 10:52 PM Rate Topic: -----

#1 qwerty505  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

String Index out of bound error in Eclipse

Posted 08 April 2010 - 09:35 PM

Hello,
A deadline is looming on an assignment for class and i was hoping someone could help me out...

I am trying to decipher some code that the professor gave the class. When i test the code in eclipse i find that i get an error in eclipse saying:

exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -10160
at java.lang.String.substring(Unknown Source)

any ideas where to look or what it could be?

thanks

edit:

testing the code still and this time just used a snippet of the original string that it was supposed to be searching for a number in and then turning into a number... this time instead of -10160 i came back -30

ok i have narrowed it down to one line of code that looks totally fine and is for some reason shooting back a negative number, i am wondering whether or not i could paste his code here...

This post has been edited by qwerty505: 08 April 2010 - 09:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: String Index out of bound error in Eclipse

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: String Index out of bound error in Eclipse

Posted 08 April 2010 - 09:38 PM

Not without your posting your code. :)

Please use the code tags:

:code:

This post has been edited by n8wxs: 08 April 2010 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 qwerty505  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

Re: String Index out of bound error in Eclipse

Posted 08 April 2010 - 10:00 PM

String data = "Results <b>1</b> - <b>10</b> of about <b>39,800</b> for <b>&quot;How tall am I?&quot;</b>.";

// After inspecting the result we can see that a page with many hits contains 
 // the words 'of about' so we look for those words and we know that right after
// that comes our question.  So first we pull out the interesting parts of that
// string
int indexOfAbout = data.indexOf ( "of about" );
	System.out.println(indexOfAbout);
int questionIndex = data.indexOf ( "<b>&quot;"+ question + "&quot;" );
        System.out.println(questionIndex);



yeah so printing out questionIdex comes back with a negative number

This post has been edited by qwerty505: 08 April 2010 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: String Index out of bound error in Eclipse

Posted 08 April 2010 - 10:18 PM

what is the value of question?
Was This Post Helpful? 0
  • +
  • -

#5 qwerty505  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

Re: String Index out of bound error in Eclipse

Posted 08 April 2010 - 10:32 PM

Okay i thought i was being helpful by cutting out must the junk:) sorry. here is the complete code for the program:


String question = "How old am I?";
String data = "Results <b>1</b> - <b>10</b> of about <b>39,800</b> for <b>&quot;How tall am I?&quot;</b>.";

		    
		    
int indexOfAbout = data.indexOf ( "of about" );
System.out.println(indexOfAbout);
int questionIndex = data.indexOf ( "<b>&quot;"+ question + "&quot;" );
 System.out.println(questionIndex);
 String numberIsHere = data.substring ( indexOfAbout, questionIndex );
 System.out.println  ( "Number should be in here: " + numberIsHere );
		    
//Then within that string we know the number will be enclosed by the html tags
// <b> and </b> so we look for those and use those as starting and ending index.
int numberStartIndex = numberIsHere.indexOf ( "<b>" );
int numberEndIndex = numberIsHere.indexOf ( "</b>" );
String theNumber = numberIsHere.substring ( numberStartIndex, numberEndIndex );
  System.out.println ( "This should be the number: " + theNumber );
		    
 // Finally we need to convert the string containing the number to an actual
 // number that we can use in our program. First we need to remove the commas
 // in the string, and then convert it.
String numberString = theNumber.replace ( ",", "" );
int number = Integer.parseInt ( numberString );
System.out.println ( "This is the numerical number: " + number );



This post has been edited by qwerty505: 08 April 2010 - 10:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: String Index out of bound error in Eclipse

Posted 08 April 2010 - 10:36 PM

A negative return is correct: question is not contained in data

See public int indexOf(String str)

This post has been edited by n8wxs: 08 April 2010 - 10:36 PM

Was This Post Helpful? 1
  • +
  • -

#7 qwerty505  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 10

Re: String Index out of bound error in Eclipse

Posted 08 April 2010 - 10:52 PM

okay, thanks, that was pretty stupid of me:(

i am in the process of making it work
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1