6

I have a question about my Stack Overflow post: java.lang.StringIndexOutOfBoundsException: String index out of range: a number

I'm struggling to obtain a reputation above five so that I can post on Meta Stack Overflow about previous posts which are locked. So, I tried to answer the question above as best I could.

How would you suggest I improve it, so that it gets accepted as an answer (or otherwise upvoted)? At this early stage, should I not be answering questions but rather adding answers as a comment to the original post (I've noticed people doing that).

Posting other answers at the moment is unfortunately not an option. I have two other posts which are currently locked and were voted as spam. Because my answer above hasn't had any positive response, Stack Overflow no longer allows me to answer any other questions.

9
  • You are not answer blocked because of this answer specifically, but because of the sum of your contributions has not been well received. Review not only this answer, but all previously posted answers. Even deleted ones count.
    – yivi
    Commented Feb 1, 2019 at 13:26
  • @yivi I understand what you're saying, and would if I could. The main problem is I have two posts that were both marked as spam (deleted) and locked by Community. I am not allowed to edit them. I wanted to post something about them but couldn't because I didnt't have enough rep. And when I tried to gain rep, it wasn't getting me anymore becuase my answer (above) didn't get any responses. I understand now that I should have answered a better quality post
    – user10885864
    Commented Feb 1, 2019 at 13:32
  • 2
    DanBron and @Servy. I apologize for causing this dispute. As an experienced programmer, I've used the knowledge collected on this site many times over many years, and decided its time to give back what I can. Ultimately, I'm still learning and trying to do right by this community. Thanks for helping me to learn how.
    – user10885864
    Commented Feb 1, 2019 at 13:36
  • 4
    @PeterSienkewicz These sorts of “disputes” are a multiple-times-a-day occurrences on Meta. Don’t give them any more thought than fights in a hockey game. You don’t need to worry and you definitely don’t need to apologize. You’re the kind of user we need to encourage on SO. Illegitimati non carborundum. Go post more answers, rep follows a power law, it’s hardest at the beginning.
    – Dan Bron
    Commented Feb 1, 2019 at 13:47
  • 2
    I think your head and heart were in the right place writing this answer. What you probably need to take care with a little is to not answer just any question you see. Even if they shouldn't, people will "punish" answers to what are considered bad or even off-topic questions.
    – Gimby
    Commented Feb 1, 2019 at 14:09
  • @Gimby Thank you for your advice. I've spent many years looking at other peoples "bad code" and corresponding "bad comments" (if there even were any) and trying to understand the actual intent. I take some delight in doing so, and I feel like I've gotten good at it. I understand now that when it comes to answering questions on this site, however, I should not engage those super-powers and instead stick to answering well-formed questions with well-formed answers.
    – user10885864
    Commented Feb 1, 2019 at 14:24
  • You didn't cause a problem. If people get into an argument under a post, it's the people who are arguing who are at fault, no one else.
    – user3956566
    Commented Feb 1, 2019 at 14:38
  • 1
    @PeterSienkewicz indeed because that is what the goal of the site is, to build up a repository of good questions. Avoiding to answer questions that are not well-formed to avoid getting downvoted is not exactly the right motivation...
    – Gimby
    Commented Feb 1, 2019 at 14:46
  • "...should I not be answering questions but rather adding answers as a comment to the original post" Please don't do this. There's no way to mark it as the best answer, so the question appears unanswered. It also can't be voted on, edited, linked to, etc etc. If you have an answer, please post it as a real answer.
    – bta
    Commented Feb 2, 2019 at 0:05

1 Answer 1

6

Your Answer

Here is what I think could be improved about the answer:

The questioner basically asked (paraphrasing):

How do I fix:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:658)
at NumeroZanoret.numeroZanoret(NumeroZanoret.java:27)
at NumeroZanoret.main(NumeroZanoret.java:19)

Here is my code:

<insert incomplete and not runnable code snippet here>

You basically answered (again, paraphrasing, I want you to see how I read it):

You might be confusing arrays of strings vs strings (which are arrays of characters). Here is some code that might do what I assume you wanted to do:

...

Ignoring that the question isn't great:

  1. I would advise that you first explain what the error means! Namely: it means that a zero-length string was provided, you tried to ask "what character is in position 1 of this string", and there is no character in position 1.
  2. After that, I would hone in on the line(s) that commit the error and suggest that those be modified, or if the structure of the code needs to be revised, a small snippet that doesn't include all of the OP's code/requirements, but just shows how to iterate over an array of strings, then over each string properly.
  3. Your answer may lead the OP to believe that StackOverflow is a code writing service. (i.e., one can upload bad code, and people volunteer to re-write it correctly.) I don't think you meant to do this, but that is how I interpret answers that don't explain much and provide a re-written form of the OP's code. I suggest not using their code even if I want to provide a snippet, and write my own that only gives them the framework so they can study it.

The Question

So, the first thing to notice is that the question isn't a Minimal, Complete, and Verifiable Example, so even if you answer it well, (as pointed out by Gimby in the comments) some users may punish you for answering bad questions.

The reason for that rule is that the goal of most of StackOverflow's contributors (that is, answer contributors), is to compile a useful repository of programming questions and answers. If we have people regularly making the mistake of indexing an array or array-like type out of bounds, then we don't want to answer the same base question day after day, we want new and different questions to expend out time/energy on. Requiring that questioners provide a MCVE means that the questioner will be respecting the community's time by:

  1. weeding out typos, trivial errors, etc,
  2. getting the questioner to see what the core of this issue, which increases the probability that they will be able to find an existing Q/A for their problem, and
  3. if they do need to ask a question, it will allow them to ask a better question that is easier for the community to replicate and study.

So all of this is difficult in the beginning, but both asking and answering are skills. I recommend reading: http://www.catb.org/esr/faqs/smart-questions.html.

1
  • 1
    In addition, questions on StringIndexOutOfBoundsException are asked daily. Commented Feb 3, 2019 at 8:54

You must log in to answer this question.