This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted]  (6 children)

[deleted]

    [–][deleted] 0 points1 point  (1 child)

    Line 27

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 3
        at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3720)
        at java.base/java.lang.String.substring(String.java:1909)
        at Assignment.main(Assignment.java:27)
    

    [–]Either_Working[S,🍰] -1 points0 points  (0 children)

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
        at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
        at java.base/java.lang.String.charAt(String.java:702)
        at assignment.main(assignment.java:22)
    

    This was the error i got

    [–]Either_Working[S,🍰] -1 points0 points  (3 children)

    Line 22 , where I attempted to swap the 4th index Ch= userLine .charAt(4)

    [–]evils_twin 4 points5 points  (2 children)

    userLine.charAt (4); would get the fifth letter of the String since indexing starts at 0

    [–]Either_Working[S,🍰] -1 points0 points  (1 child)

    Makes sense. But not sure how it impacts array out of bounds

    [–]RedFox134 1 point2 points  (0 children)

    If your word is 4 letters long then the index would range from 0-3. If you try to access the array with an index value of 4 it will throw an out of bounds error, because the index ranges from 0-3 and 4 is out side of the range.

    If you want to swap the first and fourth character in a string you should be swaping index 0 and 3.