• Post Reply Bookmark Topic Watch Topic
  • New Topic

Substring or a nested loop problem - noob LF help  RSS feed

 
S. Freeman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment:
Write a program that reads a word and prints all substrings, sorted by length. For
example, if the user provides the input "qwe", the program prints
q
w
e
qw
we
qwe


Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at line 5

My understainding and logic behind my attempt at solving this problem.
In the first iteration of the outer loop, inner loop should get me an output of all characters in the String word. Second iteration of the outer loop should output two-characters substrings of the String word. And in the third iteration i get an error because i am trying to access a character that is at position > word.length().This assignment is in a section about loops and asks for the use of substring in solving it.

I would greatly appreciate it if someone could help me get a better grasp of these basics / point me in the right direction or critique my faulty logic.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if i == words.length() - 1 and j == words.length() - 1? More importantly, what will i + j be?
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is possibly because when it enters the last iteration of outer loop, inside it, the last iteration of the inner loop method invocation becomes,



Whereas 4 is not an end index. It must be 3 for a 3 character length string.

Regarding your thought process, I may not be able to provide insightful inputs









 
S. Freeman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i came up with after reading your advice/hint


Thought process: Because i cant be adding values inside the substring to select a character/characters, shouldn't i just increase the value of i so that i can select what i need to. Content wise, I get the correct
Output:
q
qw
w
qwe
we
e

but as you can see it is not sorted. I guess that i should sort it outside of the nested loops or did i miss something?
Damn its too late and i am starting to shutdown (keep pressing ctrl + F11 to preview this post) anyway...

Thank you for your replies they are/were useful.

 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggested steps.
  • 1: ctrl-A-delete
  • 2: Turn your computer off
  • 3: Get the appropriate hardware: pencil paper and eraser. The eraser is the most important part
  • 4: Divide that job into parts: you want to find the substrings and you want to sort them. Take one task and one only.
  • 5: Write down in terms of 1st letter, 2nd letter, etc., exactly how you would do that task: on paper, not by computer.
  • 6: Now do the same for the other task.
  • When your instructions contain no Java at all, and when they are written so even a small child can understand them, then you can consider turning your computer back on.

    Remember: 1st letter ≡ myText.charAt(0)
     
    Sahas Jangam
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Freeman,

    For word of length 3 ( qwe ) ,
    if you need to print - q, w, e, qw, we, qwe
    and
    if you want to use subString(int, int);

    You need,

    q - which is word.substring(0, 1)

    similarly,

    q - word.substring(0, 1)
    w - word.substring(1, 2)
    e - word.substring(2, 3)

    qw - word.substring(0, 2)
    we - word.substring(1, 3)

    qwe - word.substring(0, 3)

    **where word length is 3.

    if you observe, there is a hidden pattern of numbers used in substring,
    try understand the pattern and create loop to print those numbers in same order. GOOD LUCK !

     
    S. Freeman
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I forgot about this,anyway have to say thanks for advice and help, was useful.

    Sharing code in white text color so select if you want to see it.

    for(int i = 1; i <= word.length(); i++)
    {
    for(int j = 0; j < word.length(); j++)
    {
    if(i + j <= word.length())
    {
    System.out.println(word.substring(j , j + i ));
    }
    }
    }
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!