Results 1 to 10 of 10

Thread: I don't get it.

  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Angry I don't get it.

    When I run this program I get this after I enter the two strings :
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.substring(String.java:1934)
    at Coincidence.main(Coincidence.java:37)
    The point of the program is to compare two strings of equal length and output their index of coincidence ie
    weallliveinayellowsubmarine
    whowrotethatbookaboutdoggie
    would be 2/27 and then that would be converted to a percent. I haven't added the part that converts to a percent yet but here's my code
    import java.util.Scanner;
    import java.lang.String;

    public class Coincidence {

    public static void main(String[] args) {
    String string1 = "";
    String string2 = "";
    int length1 = 1;
    int length2 = 2;
    String char1;
    String char2;
    int pos = 0;
    int match = 0;
    double coinc;
    Scanner input = new Scanner(System.in);
    while (length1 != length2) {
    System.out.print("Please enter the first string: ");
    string1 = input.next();
    length1 = string1.length();
    System.out.print("Please enter the second string: ");
    string2 = input.next();
    length2 = string2.length();
    if (length1 != length2) {
    System.out.println("Please enter two strings of equal length.");
    }
    } while (pos <= length1) {
    pos += 1;
    char1 = string1.substring(pos, pos + 1);
    char2 = string2.substring(pos, pos + 1);
    if(char1 == char2) {
    match++;
    }
    }
    coinc = (double)match / (double)length1;
    System.out.print("The index of coincidence is: " + coinc);

    }
    }

  2. #2
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Question

    At several points I was forced to assign a value to variables that shouldn't have had one at the start, and I think that might be the problem, but I'm not sure.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gandalf5166 View Post
    while (pos <= length1) {
    pos += 1;
    char1 = string1.substring(pos, pos + 1);
    char2 = string2.substring(pos, pos + 1);
    if(char1 == char2) {
    match++;
    }
    If a String has a length 'length1' valid index values are between 0 and length-1; see you while condition, it's not correct. In the body of your while loop you want to access the character at position pos+1; think what happens when pos == length1-1. Also char1 and char2 are Strings despite their names. You can't compare Strings for equality with the == operator. Read the API documentation for the String class and pay special attention to the equals( ... ) method.

    btw, your placement of the curly braces stinks. Don't put a right curly brace before a completely unrelated while statement; new lines don't cost anything.

    kind regards,

    Jos

  4. #4
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    I think the curly braces was cause of copy and paste doing weird things but thank you very much for the help.

  5. #5
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    The problem with making char1 and char2 actually char type is that then they can't be assigned the substring of string1 and string2; how do I pull that off?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gandalf5166 View Post
    The problem with making char1 and char2 actually char type is that then they can't be assigned the substring of string1 and string2; how do I pull that off?
    For single character substrings it's much more comfortable to use the charAt( ... ) method and assign it to a simple char type variable. You can compare those simple chars with the == operator.

    kind regards,

    Jos

  7. #7
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Thanks a lot, that method wasn't in my book but that sounds a lot easier. Edit: It is in my book but not for another 4 chapters...... >.<

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gandalf5166 View Post
    Thanks a lot, that method wasn't in my book but that sounds a lot easier. Edit: It is in my book but not for another 4 chapters...... >.<
    Keep this link: Java Platform SE 6 in your bookmarks of your browser and you have all the documentation for all classes and methods that come with the distribution.

    kind regards,

    Jos

  9. #9
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Oh my god that link is like the magical solution to all of my problems lol. It even tells me what the hell those error messages mean.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gandalf5166 View Post
    Oh my god that link is like the magical solution to all of my problems lol. It even tells me what the hell those error messages mean.
    Those are Sun's Javadocs for all the classes and methods; I have them downloaded on my computer so I can read them even when I'm not online; everybody should do that, those documents are essential to your Java development.

    kind regards,

    Jos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •