[英]Java - How to store multiple JOptioPane strings in one array?
So, I am working on a program, called Notes counter. 因此,我正在开发一个名为Notes计数器的程序。 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.....)
在第二个Java文件中,我要问的是用户要添加多少个便笺,然后按顺序显示所有便笺(1.2 .....)
I cannot put multiple JOoptionPane.showInpuDialogs into one array – user2547460 31 secs ago edit 我无法将多个JOoptionPane.showInpuDialogs放入一个数组中– user2547460 31秒前编辑
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. abobe方法应将来自JOoptioPane的所有用户答案放入单个数组中。 so later I can print out all the notes the user entered for the JOOptionPane as one array,
所以以后我可以将用户为JOOptionPane输入的所有注释打印为一个数组,
Second file viewerN: 第二个文件查看器N:
So I want to ask the user, "how many notes u want to add"? 因此,我想问用户“您要添加多少个笔记”? and I store this string as an int.
我将此字符串存储为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?).
然后我想问用户“输入您的笔记”和int一样多(您要添加多少个笔记?)。
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!!
字符串numberNotes []数组,并在infoView()中将它们打印出来。希望您能理解这一点! 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(){ 谢谢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.
75%的代码几乎不相关,您可以将其省略掉,这将使每个人都更加容易。 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. 您所做的将继续使用新的String数组覆盖当前数组。 And that SOP needs to be commented because
SOP需要评论,因为
ArrayIndexOutOfBoundsException
in case the user wants to enter 2 or lesser notes. ArrayIndexOutOfBoundsException
。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.