• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add user inputted names into array to be called later  RSS feed

 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program needs to ask for user input without knowing how many inputs. The max inputs possible are 283 and this is what i have so far:
I get an error at the return, the error says String [] cannot be converted to String. I am not sure how to fix it. I also cannot import scanner or arraylist. Help would be greatly appreciated.


 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please remind yourself about the code button; half of the tag goes before your code and the other half follows. I have corrected it for you
No, you cannot return a String[] from that method because it says it returns String. You can sort that out easily by changing the declared return type. But if you are passing that array as a parameter, maybe you don't need to return it at all.
Actually I would return the array and assign from the method, myself:-
String[] names = getArray();
Call it names not name.
Why are you catching number format exception? I can't see how you are ever going to have such an exception thrown in the first place, and you couldn't do anything about it there if one were ever thrown. Where do you get the bit about "" from? Remember what a newly‑created array (not of primitives) contains. I think your loop will not do what you think it does.
 
Nguyen Tuyen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change your defined function from: to
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! These both helped and I can't believe I didn't see the problem before.
Another problem though, it compiles now but like Campbell Ritchie said it didn't do what I thought it would. I need it to ask for names until the user says their done. How would I do this, I though the do while loop would be the right way to go because I need at least one input, but I am not sure. Thank you
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention what was going wrong
It becomes a continuous loop and won't exit out.
 
Nguyen Tuyen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:I forgot to mention what was going wrong
It becomes a continuous loop and won't exit out.

Add a break to the final if statement to solve it.
if (enterNames[x].equals("Q")) {
    JOptionPane.showMessageDialog(null, "all done");
    break;
}
 
Jerry Girgich
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nguyen Tuyen wrote:
Jerry Girgich wrote:I forgot to mention what was going wrong
It becomes a continuous loop and won't exit out.

Add a break to the final if statement to solve it.
if (enterNames[x].equals("Q")) {
    JOptionPane.showMessageDialog(null, "all done");
    break;
}



Is there another way? Because I can't use a break statement.
 
Nguyen Tuyen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Girgich wrote:Is there another way? Because I can't use a break statement.
Why you can not use break? You want to get out of an infinitive loop without a break? Using return or else it's impossible!
 
timothy adigun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nguyen Tuyen wrote:
Jerry Girgich wrote:I forgot to mention what was going wrong
It becomes a continuous loop and won't exit out.

Add a break to the final if statement to solve it.
if (enterNames[x].equals("Q")) {
    JOptionPane.showMessageDialog(null, "all done");
    break;
}


Using break as you specified, will not only opt out of the do/while loop not the for loop. So it wouldn't work as the OP intended.
 
timothy adigun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great replies had been given to you on this. But allow me to also point out 1 or 2.

1. You have been told that your method, getName should return an array of String NOT just a string. Which is correct.
BUT do you also notice that your method is suppose to return an array, which you are throwing AWAY from the caller stand-view?

Your method is called like thus:
where is the returned array? Yes, it works, because array is pointing to a reference though passed by value.

Can you still use your getName method like you are doing? Yes! But define the method to return a void instead of a string or array of strings. Therefore, your last return statement should be taken out too.

OR if you don't want to chance the method definition after you have made it to return an array, then from the caller, use an array variable to get the return of the method.

2. Like I pointed out in my previous post, one way to get out of the do/while and for loop is using break like others too had pointed out. And that could be combined with boolean type like thus:
 
Nguyen Tuyen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
timothy adigun wrote:
Nguyen Tuyen wrote:
Jerry Girgich wrote:I forgot to mention what was going wrong
It becomes a continuous loop and won't exit out.

Add a break to the final if statement to solve it.
if (enterNames[x].equals("Q")) {
    JOptionPane.showMessageDialog(null, "all done");
    break;
}


Using break as you specified, will not only opt out of the do/while loop not the for loop. So it wouldn't work as the OP intended.

Using return instead!
if (enterNames[x].equals("Q")) {
    JOptionPane.showMessageDialog(null, "all done");
    return enterNames;
}
 
timothy adigun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nguyen Tuyen wrote:
timothy adigun wrote:

Using break as you specified, will not only opt out of the do/while loop not the for loop. So it wouldn't work as the OP intended.

Using return instead!
if (enterNames[x].equals("Q")) {
    JOptionPane.showMessageDialog(null, "all done");
    return enterNames;
}


Yes, that would work but would defeat the wisdom of providing only one exit from a method.
Secondly, exit string Q will still be taken as part of the array. Of course, this is the "fault" of how the OP is storing h(is/er) input.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While the previous suggestions may appear to fix the problem, they are like putting a wig on a pig that you have put lipstick on. (If you didn't catch my drift, I'm alluding to the expression, "That's like putting lipstick on a pig")

A for-loop like the one you defined, when left to complete all its iterations, will execute its body enterNames.length times. As a matter of good programming form, whenever you put a break or continue inside a for-loop, you should reconsider your logic formulation to see if you can avoid using the break or continue statement. Don't take the use of break and continue lightly. These statements are basically dressed-up "goto" statements (or if you prefer to stick with the pig metaphor, lipstick-up'ed).

There is a classic essay by an ACM Turing Award-winning computer scientist that is entitled "Goto Considered Harmful" Therefore, you should use the break and continue statements carefully and sparingly, perhaps break more carefully and sparingly than continue.

In a language like Go, where the only loop construct is the for-statement, the continue statement is actually used as part of the "Guard Clause" idiom:

In Go, this is an idiom that allows you to avoid writing arrow code or using an if-else within the for-loop.

This idiom is not very common in Java. The same logic would normally be coded in Java like so:

To be honest, I like the Go idiom and I'd probably not think twice about using it in Java code if it were up to me. However, the danger is that uninformed programmers might see that code and think that using a continue is normal and then they start using in ways where it only makes the code more convoluted in ways that the "Goto Consider Harmful" essay was written to rile against.

BTW, if you read this post carefully and you are really cut out to be a programmer, you might find a nice Easter Egg.

(EDIT: changed "uniformed" to "uninformed" -- LOL at my typo)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the real problem was that you had the do-while loop inside a for-loop.  You should rework your logic so that you exit your while-loop when user wants to exit (indicated by entering an end flag value like "Q" or "") or has to exit (because there is no more room to put more numbers in the array).

Also, you should consider renaming your parameters and variables. For example, the name enterNames is a bad name. That's a verb phrase, more fitting of a method name.  You could rename it to enteredNames or namesEntered or simply just names.  You should choose names that reveal their purpose/intent, not implementation or what you did with them. Also be careful of semantics.  The method getName(), does that imply getting multiple names or one name? If you consider proper English, that name would suggest just getting one name, right? Yet, the implementation actually returns multiple names that are stored in an array.  So, the more logical name for that method is getNames() (plural).

These things may seem trivial but in programming, little things like this count. Don't make your code lie. Lies are the source of bugs.

The TWO hardest problems in computer science:
1. Cache invalidation
2. Choosing good names
3. Off-by-one errors

By far, #2 is the hardest.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!