8 Replies - 4032 Views - Last Post: 13 June 2010 - 12:10 AM Rate Topic: -----

#1 other_me  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 11-June 10

String Index out of range

Posted 12 June 2010 - 02:59 AM


Hey evereyone im new to the world of programming and forms of java help
but i just cant figure out the prob lem with my code.

I trying to check String input for a viable palindrome and it compiles but
i keep on getting the same run-time error.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 27


If anyone could help me suss it out it would be greatly appreciated. :)

MY CODE
//Jason 131002728;
//01;
//this class checks a String if its a palindrome;
//12-06-10;
import java.util.Scanner;
public class Palindrome {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	//promt
		System.out.println("Enter possible palindrome for testing");
		String input = sc.nextLine();
	//variables
		int st = 0;
		int fn = input.length();
		char frontCh;
		char backCh;
	//checks a char from the first halve of the string
	//to its reflective couterpart
		while ((st != fn)||(st == input.length())) {
			frontCh = input.charAt(st);
			if (Character.isWhitespace(frontCh) == true) {
				frontCh = input.charAt(st + 1);
			}
			backCh = input.charAt(fn);
			if (Character.isWhitespace(backCh) == true) {
				backCh = input.charAt(fn - 1);
			}
			if (frontCh != backCh) {
				System.out.println("\nEntered String is NOT a palindrome!");
				System.exit(0);
			}
			++st;
			--fn;
		}
	//displays recognition of an acceptable palindrome
		System.out.println("\nEntered String is a palindrome!");
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: String Index out of range

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2646
  • View blog
  • Posts: 11,169
  • Joined: 20-September 08

Re: String Index out of range

Posted 12 June 2010 - 03:28 AM

Looking again...

This post has been edited by g00se: 12 June 2010 - 03:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2646
  • View blog
  • Posts: 11,169
  • Joined: 20-September 08

Re: String Index out of range

Posted 12 June 2010 - 03:35 AM

Strings are indexed 0 to length - 1, so you need to check your indexes. I would make it easier on yourself and remove all white space first.

You could make it even easier and use StringBuilder.reverse

This post has been edited by g00se: 12 June 2010 - 03:36 AM

Was This Post Helpful? 1
  • +
  • -

#4 other_me  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 11-June 10

Re: String Index out of range

Posted 12 June 2010 - 08:04 AM

I tweaked it a little bit but im still not getting the desired result.

CODE
//Jason 131002728;
//01;
//this class checks a String if its a palindrome;
//12-06-10;
import java.util.Scanner;
public class Palindrome {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	//promt
		System.out.println("Enter possible palindrome for testing");
		String input = sc.nextLine();
	//variables
		int st = 0;
		int fn = input.length();
		char frontCh;
		char backCh;
		input = input.replaceAll(" ", "");
	//checks a char from the first halve of the string
	//to its reflective couterpart 
		while ((st != fn)||(fn == 0)) {
			frontCh = input.charAt(st);
			backCh = input.charAt(fn);
			if (frontCh != backCh) {
				System.out.println("\nEntered String is NOT a palindrome!");
				System.exit(0);
			}
			++st;
			--fn;
		}
	//displays recognition of an acceptable palindrome
		System.out.println("\nEntered String is a palindrome!");
	}
}



for instance:
____if i were to input was it a car or a cat i saw
____I would expect the spaces to be taken out - wasitacaroracatisaw
____Then i would expect the loop to check
____the characters for likeness as follows-_________________key
_____________________________________________________Position = (_)
_____________________________________________________Character = char
________________________________________________________________________________________________________
_______________________________________________(0)________________(18)
________________________________________________w__________________w
________________________________________________(1)______________(17)
_________________________________________________a________________a
_________________________________________________(2)____________(16)__________________________________________________s______________s
__________________________________________________(3)__________(15)
___________________________________________________i____________i
___________________________________________________(4)________(14)
____________________________________________________t__________t
____________________________________________________(5)______(13)
_____________________________________________________a________a
_____________________________________________________(6)____(12)
______________________________________________________c______c
______________________________________________________(7)__(11)
_______________________________________________________a____a
_______________________________________________________(8)(10)
________________________________________________________r__r
________________________________________________________(99)
_________________________________________________________oo
________________________________________________________________________________________________________
____Then i would think the loop would end leaving the the end display.
________________________________________________________________________________________________________
But im still getting the runtime error.
If there is anyway you could elaberate on why that is it would be much appreciated :helpsmilie:

P.S. i know the spacings a little over the top but it was the best way i could think of to demonstrate my thought processes :P
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2646
  • View blog
  • Posts: 11,169
  • Joined: 20-September 08

Re: String Index out of range

Posted 12 June 2010 - 10:49 AM

Quote

If there is anyway you could elaberate on why that is it would be much appreciated


Because you didn't read/take in/implement my first (most important) sentence ;) Try again...
Was This Post Helpful? 0
  • +
  • -

#6 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: String Index out of range

Posted 12 June 2010 - 02:02 PM

What g00se is saying is that strings are indexed from 0 to length -1, meaning that a 10 character string would have a length of 10, but be indexed from 0 to 9. You are using the length to examine the last character of the string on the first iteration of the loop, therefore you're trying to look at an index that does not exist. Subtract 1 from the length (fn) before using it to access the last index.
Was This Post Helpful? 1
  • +
  • -

#7 other_me  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 11-June 10

Re: String Index out of range

Posted 12 June 2010 - 05:41 PM

Its all cool there was no need for the advice

Iunderstand that g00se was intentionally not GIVING me the answer because then how would i learn,
not mention its not allowed in dream.in.code.
however he did give me
- a valid reason for my fault.
- a great suggestion
- and some advice that would make it easiier on me.

and once i went over the previous advice i realised the fault and changed it myself.

now it works perfectly :)

thanks guys
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: String Index out of range

Posted 12 June 2010 - 05:48 PM

So give a +1 to g00se
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2646
  • View blog
  • Posts: 11,169
  • Joined: 20-September 08

Re: String Index out of range

Posted 13 June 2010 - 12:10 AM

Thanks all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1