Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am new to Java Programming.I want to fetch data from the database and show it in JTable.
This is my current code.
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
Object data[][]=new Object[200][5];
int i=0;
while(rs.next())
{
result_Transaction_id[i]=rs.getInt("f.Transaction_id");
result_User_name[i]=rs.getString("U.User_name");
data[i][5]={(Object)result_Transaction_id[i],(Object)result_User_name[i]};//This is where i get error
++i;
}
JTable View_table =new JTable(data,columns);
I want to collect the results of the query and add them in the specified object.
But i get the following error "Array constants can only be used in initializers".
I found a lot of solutions to "Array Constants can only be used in initializers" error, but those don't suit my case.
An "array constant" means { ... }.
As the error is trying to tell you, you can only use that syntax when declaring an array variable.
To assign an array to an existing expression, use new TypeName[] { ... }.
The error is the way you declare your array.
As stated here: https://stackoverflow.com/a/10520659/3558900 You can only declare an array NOT like this:
String columns[]={"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
BUT
String[] s;
s = new String[] {"Transaction_id","User_name","Amount","Recharge_Spending","Spend_by"};
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Do Numbers inside such brackets { } always belong to arrays or can it be a primitive Type too?
The exact task is: Decide for the following values (!) whether Java provides primitive data types for their representation. If yes, specify all matching ones.
I'm only not sure about this one: {1,4,2}
There isn't any int [ ] infront of that, that's why i'm asking.
(sorry for the dumb question, very big noob here)
The code :
int[] array = {1,2,3};
Is equivalent to :
int[] array = new int[3];
array[0] = 1;
array[1] = 2;
array[2] = 3;
In java, arrays are objects (even if it's an array of primitive type)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to allow the user to target a variable using a string name. Example,
int number = 5;
String variableTarget = "number";
I need to target the int, number, and then change its value in later code. Any way?
I cannot think of any way to do it. There seems to be alot of class targeting, but I already have that.
I don't think that there is a way to do this in java. But, to have such a functionality, you can use a map to assign the value to the number key in the Map :
HashMap <String, Integer> myMap = new HashMap<>();
myMap.put("number", 5);
and you can alter the value later using the same put statment :
myMap.put("number", myMap.get("number")+1);
You can use Java Reflections, or create another structure like a HashMap answered by #DodgyCodeException
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I saw this snippet of code in a forum with the same question, however I needed 50 reputation to comment on the reply. So I posted it here:
array = ArrayUtils.removeElement(array, element);
I was wondering how this would be applied in code, as this is just the code:
Would it look something like this:
myArray = ArrayUtils.removeElement(myArray, 2);
or
myArray = ArrayUtils.removeElement(int[], 2);
The first line
myArray = ArrayUtils.removeElement(myArray, 2);
is correct syntax. It would return a new version of your array, with the element at index 2 removed. The other line would result in an error, because you aren't actually passing an array object but rather just a type.
Here's the JavaDoc for ArrayUtils if you would like to learn more about this method or other, related methods: https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to show images upon clicking a button.
These images must come from an array so that i can easily add or delete images without having to edit all of my code.
However the compiler aborts my build and i have 2 errors.
public void showQuestion()
{
currentQuestion++;
imageNumber++;
if(currentQuestion == questions.length)
currentQuestion = 0;
if(imageNumber == myImageList.length)
imageNumber = 0;
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
imagesview.setImageResource(myImageList[imageNumber]);
}
the first error is at imageNumber == myImageList.length, saying
Multiple markers at this line
length cannot be resolved or is not a field
myImageList cannot be resolved to a variable
the second error is on images view.setimageResource(myImageList[imageNumber]);
Multiple markers at this line
The type of the expression must be an array type but it resolved to ArrayList<Integer>
myImageList cannot be resolved to a variable
Edit:
The mistake i made was i overlooked that i was using an INT with the .length attribute. and a set instead of a .get!
Hope it helps
An ArrayList is of type list. Therefore .length should be .size() and myImageList[imageNumber] should be myImageList.get(imageNumber).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was asked to use an array to create a phonebook which would read in a text file.
However I am having trouble each line on the text file into my object array.
so my solution was to create an array of type String then make my object array = to string array... but thats the problem.
Is there any way of my directly using the Scanner class to read in my text and save it into an object array as Scanner class's next() method is type string... and my array is type object... ?
thanks.
Try:
List<String> lines = new ArrayList<>();
while(scanner.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] array = lines.toArray(new String[lines.size()]);
ArrayList is the key All you need to do is convert your Strings to arrayLists and then you can add or pop the elements more easily