set array values in to a text area in java - java

i want to set an array list value in to a text area.
this is my code,
ArrayList<String> phone_numberArray = db.getNumberSms();
for(int i=0; i<phone_numberArray.size(); i++){
jtext_area.addElement(phone_numberArray.get(i));
}
this code getting errors.
some one help me please....

I assume jtext_area is an instance of JTextArea , I don't see any addElement() in that class, You might be looking for append()

Try:
for(String number : db.getNumberSms()) {
jtext_area.append(number);
}

Related

Trying to Reference a Button With a String

I have a situation in an app I'm making where I have an activity that has 15 buttons and a string[] that contains 15 strings. I'm looking for an easy way to assign each string in the string[] to its corresponding button. I was hoping I could somehow do something like:
for(int i; i<myStringArray.length; i++){
String ref = "btn" + (i + 1);
ref.setText(resultString[i]);
}
where all the buttons are labeled "btn1", "btn2", etc so that they could all be accessed with the string "ref". Obviously this doesn't work so I was wondering if there was another way of doing something similar to this instead of doing:
btn1.setText(resultString[0]);
btn2.setText(resultString[1]);
btn3.setText(resultString[2]);
...
thanks for the help!
I think you can solve this by having a Button[] table where you store all your Button objects and then you can access them by index instead of the actual object name:
Button[] btns = new Button[15]
for(int i = 0; i < btns.length; i++) {
btns[i].setText(resultString[i]);
}
Hope that helps.
EDIT: Ofcourse you have to fill your Button[] btns with your objects first.
You can do what you tried using reflection. It would be way better in your case to store your buttons in an Array or a Map instead though.

Putting unique random number in array

i want to try to put a unique random number from shuffled list to array but keep failing.
im using this answer with little modification.
this is my method
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<10;i++) {
list.add(i);}
Collections.shuffle(list);
for(int x=0;x<3;x++){
RNGee=RNG.nextInt(9);
RN[x]=list.get(RNGee);
if(RN[x]==QNum){
x=0;
}
}
When i print out the RN array, some of it have a chance to get same value.
Is there something wrong with my code ?
Please explain it to me.
Thank you.
Your easiest way of doing this would just be to use list.get(0), list.get(1) and list.get(2). The list is shuffled, so the three elements at the head of the list will be random and different.
for(int x=0;x<3;x++){
RN[x]=list.get(x);
if(RN[x]==QNum) { //but this is very unclear
x=0;
}
}
There's part of your code that you haven't explained, though, which is the bit that sets the loop variable back to 0 if the random number you choose is equal to QNum. It's not at all clear what that's there for, but I suspect you will need to remove QNum from the list beforehand, so that it never turns up:
for(int i=0;i<10;i++) {
if (i!=QNum) {
list.add(i);
}
}
Despite the fact that your solution, as chiastic-security said, is unneccessary your mistake is that your list still contains 10 elements and that you are randomly picking from.
To be able to use your solution you would need to remove the element that you did select from the list and generate your random number in the range of 0-list.size-1.
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<10;i++) {
list.add(i);}
Collections.shuffle(list);
for(int x=0;x<3;x++){
RNGee=RNG.nextInt(list.size()); // You collection gets smaller after each itteration so be sure to not run into a OutOfBound
RN[x]=list.get(RNGee);
list.remove(RNGee); // Remove the duplicate to be sure that the element doesn´t get picked twice
if(RN[x]==QNum){
x=0;
}
}
Edit: also make sure to follow the java convention, which says that methods and variables should start lowercase and classes uppercase. That will make you code more readable to us and yourself in the end.

Printing individual parts of an array in a Java Gui app

OK, so I created a console app that, among other things, takes an array of numbers and prints them out one by one, line by line. Now, I have to take the class that I created for that console app, and pop it into a separate GUI app we're creating. I have all of the other methods working fine, but for the life of me I cannot get the array method to print out correctly. It just gives me the last number I typed into the text field. I'm hoping someone can give me a nudge to help me figure this part out so I can move along, and get to the whole SpringLayout stuff, (the main part of the new assignment) I am limited in what I can show you here because this is a current assignment, so I will have to stick to this stuff as specifically as I can. And please, don't just post the code as an answer, (because then I can't use it), thanks.
Here's what I had for my original project for the array method:
int [] getArray(int x)
{
breakUpNum(x);
return numAry;
}
From there, inside my new class I have this, in an attempt to get it to print:
private class ButtonTest implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Lab1 tester = new Lab1();
int[] test4 = tester.getArray(num);
for(int i = 0; i < test4.length; i ++)
{
crossTest.getArrCross.setText("" + test4[i]);
}
}
}
Any help pointing me in the right direction would be greatly appreciated, thanks!
setText does just that, sets the text you pass to as the current text content, it does not append it.
If you were to use JTextArea, you could use it's append method...however, for a JTextField you need to have a different approach.
Now you could use getArrCross.setText(getArrCross.getText() + test4[i])...but to quite frank, that's rather inefficient, as each call to setText is going to stage a paint event...
StringBuilder sb = new StringBuilder(128);
for(int i = 0; i < test4.length; i ++)
{
sb.append(test4[i]);
}
crossTest.getArrCross.setText(sb.toString());
Now, if you want to separate each element, you need to add
if (sb.length() > 0) {
sb.append(", ");
}
Before sb.append(test4[i]);
The last bit of actionPerformed in the for loop isn't working right. setText replaces the current text with its argument, and it doesn't seem like you want to do that. To fix it, replace the line in the for loop with this:
crossTest.getArrCross.setText(crossTest.getArrCross.getText() + test4[i]);

Incompatible data types in Java, array and integer

I'm trying to get a program so that it loops and adds up the sum of an array. My code appears to be working, with the exception that it states that the text[j] in adding = adding + text[j] is an incompatible type (I'm assuming data type). Earlier in the code, I have int adding = 0;. This is the erroneous code:
for (int j=0;j<=total;j++){
adding = adding + text[j];
System.out.println(text[j]);
}
where total is the limiting factor. If I put:
for (int j=0;j<= total;j++){
adding = adding + j;
System.out.println(text[j]);
}
the program compiles but gives 45, which is incorrect.
Why is this happening? Thanks!
The answer actually turned out to be outside the code given. I had set my array to be a String, not an int as it should have been.
If your text[] is String[] or char[] as the name suggests then I believe you are trying to update text[] elements with suffix j or adding, which you can write as:
If it is char[] then write
text[j] = (char)(adding + (int)text[j]);
If it is String[] then write
text[j]= text[j]+adding;
as required. It all depends on what is the data type of text[] and what are you trying to achieve?
Also as suggested in one of the answers, if total is length of the array, then change the comparison to < to avoind ArrayIndexOutOfBoundsException
Your second example, adds j into adding but prints text[j] value, which is nothing to do with the addition of adding and j.

Use JTextField to List items

I'm not too familiar with Java GUI programming, and I wanted to do something where I have a loop that spits out a list of stuff and have the JTextField render it in the order it comes out.
I just do not know how the second parameter of the JTextField insert() function works. Right now when I do something like:
for(int i = 0; i < list.size(); i++){
textArea.insert(list.get(i), 0);
}
It does what I want, except it lists everything in backwards order that I put it in. I want it to display everything the other way around.
Thank you for any advice.
All you need to define a temporary string, result and for every item in the list add the string representation to that variable. When you have looped through everything, all you need to do is textArea.setText(result).
String result = "";
for(int i = 0; i < list.size(); i++)
{
result += list.get(i).toString();
}
textArea.setText(result);

Categories