简体   繁体   中英

Java - How to store multiple JOptioPane strings in one array?

So, I am working on a program, called Notes counter. In the second java file, what I want is ask the user, how many notes he/she wants to add and then display all the notes in order(1.2.....)

I cannot put multiple JOoptionPane.showInpuDialogs into one array – user2547460 31 secs ago edit

for this line:

for(int i = 0; userEnterADD >i;i++){
String add1 = JOptionPane.showInputDialog("Enter your note here!");
 numberNotes= new String[userEnterADD];}

The abobe method should place all the user's answers from the JOoptioPane into one single array. so later I can print out all the notes the user entered for the JOOptionPane as one array,

Second file viewerN:

So I want to ask the user, "how many notes u want to add"? and I store this string as an int. and then I want to ask the user "enter your note" as many times as the int(how many notes u want to add?).

Then I want to store the user's answers in one array. string numberNotes[] array, and print them out in the infoView().. Hope, you can understand this!! Thanks

And I WANT TO PRINT OUT THE USER'S ENTERED NOTES HERE AS ONE ARRAY, how do I do that?

Thanks public void infoView(){

System.out.println("\n\tYour notes:\n");
for(int ii = 0; userEnterADD >ii;ii++){
        System.out.println(ii+1 + ". " + numberNotes[ii]);

    //end for
    }
    }



    // end of the program
}
for(int i = 0; userEnterADD >i;i++){
     String add1 = JOptionPane.showInputDialog("Enter your note here!");
     numberNotes= new String[userEnterADD];
}

This code will always overwrite your array.

In order to add a value to an array, use it as such:

for(int i = 0; userEnterADD >i;i++){
     String add1 = JOptionPane.showInputDialog("Enter your note here!");
     numberNotes[i] = add1;
}

Sidenote: avoid such clutter in your post the next time. 75% of your code doesn't come near being relevant, you can leave it out which will make it a lot easier for everyone. It's important that you learn how to identify a problem on your own, you'll often find the issue by making sure your question here contains all the information.

You need to make the below changes

userEnterADD = Integer.parseInt(numbAddn);
numberNotes = new String[userEnterADD]; // Need to initialize your numberNotes array here.
...
for (int i = 0; userEnterADD > i; i++) {
    String add1 = JOptionPane.showInputDialog("Enter your note here!");
    numberNotes[i] = add1; // Add the text received from the user to your array
}
..
// System.out.println(numberNotes[2]); // Commen this line

What you did will keep overriding your current array with a new String array. And that SOP needs to be commented because

  1. It does not serve any purpose
  2. It'll give an ArrayIndexOutOfBoundsException in case the user wants to enter 2 or lesser notes.

Edit:

After making the above changes, I got the below output by running your code

Heyyyy
Hi! Welcome to Notes Counter!
By Marto ©2013

        Main Menu (hint: just type the number!)

1 - Start counting
2 - View/add notes
3 - Help
4 - About
2  tttttt

You have successfully added!2       notes!

    Your notes:

1. test
2. test1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM