1

I would like to know if the following code would increase the array value to the next array value every time it runs.

For e.g, on the first run, it stores value in count[1] of the array, on the next run does it store the value in count[2]?

public static void getadminName(){
        String[] name= new String[20];
        for (int count=0;count<name.length;count++){
            name[count]= JOptionPane.showInputDialog(null,"Please enter 
            admin's name:");
            String scount=Integer.toString(count);
            name[count]= scount+1;
            break;
        } 
    } 
8
  • There are several problems with your code. Before giving any advice, can you elaborate on what you want to do ? Commented Jul 25, 2018 at 14:10
  • 1
    name[count] = JOptionPane.showInputDialog(null, "Please enter admin's name:"); is redundant here. name[count] = scount + 1; overwrites the value Commented Jul 25, 2018 at 14:12
  • This is basically part of my program, where im running a program that will allow me to input a admin's name and go back to a main menu. However, i need to be able to store multiple admin's names, hence the need to store names in the array indexes 1, 2, 3, and so forth. The thing is that I will not be inputting all of the names at one shot. Also, may I know what are the problems with my code? Commented Jul 25, 2018 at 14:13
  • Just a small advice: Don't give void methods a name beginning with get, that is really disturbing and the only thing you get doing so will be trouble. Commented Jul 25, 2018 at 14:14
  • Ok thank you for ur suggestion!!!! Commented Jul 25, 2018 at 14:16

1 Answer 1

3

Remove the break statement and the code should do approximately what you want it to. Keep in mind that name is an array of strings, with twenty elements. When you do name[count], you're referring to the countth element of that array, which is a String.

You notice that the for loop has three parts:

  • int count=0; declares an integer named 'count', initialized to the value 0.
  • count < name.length declares a "termination condition" - if, before executing the code inside the loop, this condition is false, then the loop ends. In this case, it runs until the variable count has a value greater than the length of the array name.
  • count++ is executed immediately after each iteration of the loop finishes.

The sum, in this case, is that you start with count=0, accessing the first element of name, and then you do things in the loop, and then count increases by one, and the code inside the loop runs again, until count is greater than the length of name.

The statement break specifically jumps out of the loop, regardless of whether it otherwise would have. That's what you don't want to do here - you want to continue looping.

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm I see, but what I want to do is to have a JOptionPane that only loops once, as i am creating a menu system that allows me to input names 1 at a time. I will not be inputting all of the names in at one shot.
I'd advise thinking on your own about how to handle that, and at which point in the program. Maybe, instead of using an array string[], use an ArrayList<String>, which you can change at any time, or add to using .add(item).
yes i've thought about that, problem is im not allowed to use arraylists'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.