5 Replies - 7182 Views - Last Post: 25 March 2012 - 02:52 PM Rate Topic: -----

#1 db440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 12

program that counts double letters

Posted 18 March 2012 - 09:17 PM

I keep getting an error (Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(Unknown Source)), i believe its in my if statement (Line 14). not sure what the problem is.

import java.util.*;

public class Recitation_8B 
{
	public static void main (String [] args)
	{
		Scanner kbd = new Scanner(System.in);
		System.out.println("Please type a text sentence.");
		String colodny = kbd.nextLine();
		int index=1;
		
		while(index < colodny.length())
		{
			if ((colodny.charAt(index)==(colodny.charAt(index+1))))
			{
				System.out.println("Double-letter found: " + colodny.charAt(index+1));	
			}
			index++;
			
		}
		
		System.out.println("Thank you, program over");
		
		
	}
}



*Please :code:

This post has been edited by pbl: 18 March 2012 - 09:27 PM
Reason for edit:: Code tags fixed


Is This A Good Question/Topic? 0
  • +

Replies To: program that counts double letters

#2 pbl  Icon User is offline

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

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: program that counts double letters

Posted 18 March 2012 - 09:30 PM

If a String has a length of 4, you can
charAt(0)
charAt(1)
charAt(2)
charAt(3)

// so index will be 0, 1, 2, 3
while(index < colodny.length())
		{
			if ((colodny.charAt(index)==(colodny.charAt(index+1))))  


and when index will be 3, charAt(index+1) will try charAt(4) and generate you exception

:^:
Was This Post Helpful? 0
  • +
  • -

#3 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 280
  • Joined: 07-November 10

Re: program that counts double letters

Posted 18 March 2012 - 09:38 PM

View Postdb440, on 18 March 2012 - 09:17 PM, said:

I keep getting an error (Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(Unknown Source)), i believe its in my if statement (Line 14). not sure what the problem is.

import java.util.*;

public class Recitation_8B 
{
	public static void main (String [] args)
	{
		Scanner kbd = new Scanner(System.in);
		System.out.println("Please type a text sentence.");
		String colodny = kbd.nextLine();
		int index=1;
		
		while(index < colodny.length())
		{
			if ((colodny.charAt(index)==(colodny.charAt(index+1))))
			{
				System.out.println("Double-letter found: " + colodny.charAt(index+1));	
			}
			index++;
			
		}
		
		System.out.println("Thank you, program over");
		
		
	}
}



*Please :code:





int index=0;

try {
				if ((colodny.charAt(index)==(colodny.charAt(index+1))))
				{
					System.out.println("Double-letter found: " + colodny.charAt(index+1));	
				}
		} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}
			index++;
			
		
	 


But be warned that your program fails when entered "aaa" or such, it double counts a
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8376
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: program that counts double letters

Posted 18 March 2012 - 09:44 PM

burakaltr would you please stop, when replying to a post, to paste in you reply all the previous posted text/code ?
That makes replies long for nothing. Unless you want to attire the attention to a particular phrase or piece of code please avoid that. We have already seen what the original poster has written, his question and his code.
Thanks :^:
Was This Post Helpful? 0
  • +
  • -

#5 db440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-March 12

Re: program that counts double letters

Posted 25 March 2012 - 01:34 PM

Thanks for the replies! i figured it out by putting a +1 in line 14 of my while statement. My only problem is i dont know why it works now, can anyone please explain?
Was This Post Helpful? 0
  • +
  • -

#6 codeMonkey_1066  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 71
  • Joined: 22-March 12

Re: program that counts double letters

Posted 25 March 2012 - 02:52 PM

Quote

i figured it out by putting a +1 in line 14 of my while statement. My only problem is i dont know why it works now, can anyone please explain?

I'm guessing your code now looks like
int index= 0 ;
while(index + 1 < colodny.length() )
{
 if(colodny.charAt(index) == colodny.charAt(index +1)
  ...
  index++;
}


You don't want to check the last character ( index = colodny.lenght() - 1) as your test will try and look at the next character to index, which will be out of range.

Also it's usually cleaner to use a for loop such as
for(int index = 0 ; index < colodny.lenght() -1 ; index++)
{
 ...
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1