AnswerBun.com

StringIndexOutOfBoundsException:

Stack Overflow Asked by BENJAMIN WEED on January 5, 2022

I know this error has been asked a lot. I have looked at them but still don’t really understand how it applies to my problem. I am trying to write a program that creates a diamond pattern from a string.

public static String nameDiamond(String s) {
    int len = s.length();
    int i = 0;
    String out = "";
    while(i < len) { 
        out = out + s.substring(0, i + 1) + "n";
        i++;
    }
    while(i > 0) { 
        int numSpaces = len - i + 1;
        for(int j = 0; j < numSpaces; j++)
            out = out + " ";
        out = out + s.substring(len - i + 1, i - 1) + "n";
        i--;
    }
    return out;
}

Now my understanding of substring is, that it is structured like s.substring(startIndex, endIndex) and a cause of this error can be when the end index is less then the start but I don’t think this is the problem here. I also saw some causes of the error is when something does not exist but again I don’t think this is the problem. What am I missing?

When it is tested with "Marty" it says -1 and with "Bill Nye" it says -2. Would the space be causing this?

I looked at lots of other versions of the questions but the ones that seemed relevent were:

StringIndexOutOfBoundsException: string index out of range

StringIndexOutOfBoundsException: String index out of range: -1

But I don’t think they apply.

For reference, this is the problem:
https://www.codestepbystep.com/problem/view/java/strings/nameDiamond

One Answer

Here is one you can use as an example. It doesn't do exactly what you want but it will illustrate one important thing. For fixed width fonts, each row must be an odd number of characters. Otherwise they won't line up properly.

If n is an even number, this particular version will print the same diamond for n and n+1. It is also limited by the string of spaces.

public static void printDiamond(int len) {
    String str = "*";
    String space = "                              ";
    for (int i = 0; i < len/2 + 1; i++) {
        System.out.print(space.substring(0,len/2-i));
        System.out.println(str);
        str = str+"**";
    }
    str = str.substring(2);
    for(int i = 0; i < len/2; i++) {
        str = str.substring(2);
        System.out.print(space.substring(0,i+1));
        System.out.println(str);
    }
}

As you debug your program, use ubiquitous print statements to print the different variables such as indices and string lengths.

Answered by WJS on January 5, 2022

Add your own answers!

Related Questions

Can i show all variable results in a div in jQuery?

0  Asked on December 5, 2021 by nippledisaster

   

Comparing two generics in compareTo()

2  Asked on December 5, 2021 by chalupabatmac

       

arranging the json file base on key value

1  Asked on December 5, 2021 by prateek-singh

     

Vue watch method not filtering object

3  Asked on December 5, 2021

   

Passing a variable between HTML pages using JavaScript

2  Asked on December 5, 2021 by robert-little

   

How to avoid code redundancy with JAVA generics

1  Asked on December 5, 2021 by cosimoth

   

Why is my project no longer updating when I write new code?

1  Asked on December 5, 2021 by stephen-horton

       

node https ssl your connection is not private error

2  Asked on December 5, 2021 by andrea-dattero

       

Nested for loop using dict

9  Asked on December 5, 2021 by purwu

     

React Hooks – Input loses focus when 1 character is typed in

4  Asked on December 5, 2021 by dragos-strugar

   

How do I build a C Python Extension for Windows using Cygwin64?

2  Asked on December 5, 2021 by zenith2198

       

Wificonfiguration is deprecated Android 10

1  Asked on December 5, 2021 by ahmad-idrees

         

Wrong tootip in chartjs

1  Asked on December 5, 2021 by thiago-scher

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir