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:
- 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.
- 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.
- 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:
- weeding out typos, trivial errors, etc,
- 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
- 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.