I put a partial of my code which I think is the source of problem but I could not figure out hence why I am at StackOverFlow now. Anyways this Class is where i set my data and pass it into an array.
public ArrayList<select.rates> caseGetRates() throws RateTableException, SessionDisconnectedException {
try {
for(int i=0;i < arrayRate.size();i++){
ArrayList<select.rates> arr = new ArrayList<select.rates>();
this.setPair(array[0]);
this.setBid((array[2]));
this.setAsk((array[3]));
arr.add(this);
}
return arr;
} finally{}
}
When I System.out.print the data which I set in this class it gives me:
EUR/USD
1.12372
1.12384
USD/JPY
100.622
100.641
which is correct and what I would like it to be displayed on my webpage.However when I pass the data to my Servlet
try {
ArrayList<select.rates> rates = example.caseGetRates();
for(int i=0;i < rates.size();i++){
System.out.println("");
System.out.println(rates.get(i).getPair());
System.out.println(rates.get(i).getBid());
System.out.println(rates.get(i).getAsk());
}
request.setAttribute("rates", rates);
}
request.getRequestDispatcher("/NewFile.jsp").forward(request, response);
The result I get on my Servlet is:
USD/JPY
100.622
100.641
USD/JPY
100.622
100.641
The result does loop twice however the data seems to be overwritten and I still can't figure out why is this happening. I hope someone can pin point my mistake.
Create ArrayList object outside for loop
and inside for loop create new Object that you are adding to ArrayList
try {
ArrayList<select.rates> rates = example.caseGetRates();
for(int i=0;i < rates.size();i++){
// create new object here and then add to ArrayList
}
request.setAttribute("rates", rates);
}
request.getRequestDispatcher("/NewFile.jsp").forward(request, response);
Related
ArrayList<Rolling> copy = new ArrayList<Rolling>();
for (int i = 0; i < sequence.size(); i++) {
copy.add(sequence.get(i));
}
ListIterator<Rolling> listIterator = copy.listIterator();
// Remove each element one by one
for (int j = 0; j < copy.size(); j++) {
listIterator.next();
if (copy.contains()) {
listIterator.remove();
}
}
public static void main(String[] args) {
remove(sequenceOfDice(4), 4);
}
}
short summary: I have a class called Rolling and I wanna thru this method copy its elements and then remove the value of n from the new list and return the rest but I am not geting far since i got a couple of errors.
The error I get is:The method contains(Object) in the type ArrayList is not applicable for the arguments ()
the method "copy.contains()" should have a parameter(argument).
that means,you should write like this "copy.contains(x)"
what's more, i think you are a new learner.because your code have many errors or sames strange.
your comment is "// Remove each element one by one"
but you said "then remove the value of n from the new list", is this conflict?
please paste all the code and compile it successfully on your laptop.
By the way, in a for loop, we usually use iterator to remove element because remove element will cause modcount change,try below code:
while(listIterator.hasNext()){
Rolling next = listIterator.next();
if (copy.contains(next)) {
listIterator.remove();
}
}
Okay, let me be more specific on what is happening. The code through each iteration of the for loop runs fine in that, code not included here, inputs are taken in from a user which are stored in the "Ball" object which is then stored in ballStorage. I have a to print in the for loop to show me what the "Ball" object at ballStorage[i] contains and the results are accurate. However, when I print out the results of the entire array's content after the for loop, every single "Ball" object in the array is overwritten with the last "Ball" object's values. I then checked to see what happens if I add a new "Ball "object outside of the for loop to ballStorage.length-1 which is empty and find that "Ball" object overwrote all the other "Ball" objects in the array. Please help and sorry for not being clear about what is happening.
for(int i = 0; i < ballStorage.length-1; i++){
ballStorage[i] = new Ball();
System.out.println("\n Ball number "+i+": \n"+ballStorage[i].toString());
}
if you want an array that store objects do this:
class Ball {
public static void main (String[] args) {
Ball[]obj =new Ball[5];
for(int i=0;i<5;i++)
{
obj[i]=new Ball();
}
for(int i=0;i<5;i++)
{
System.out.println(obj[i]);
}
}
}
I didn't much gets your question. But you can access the object of an array inside the loop by index after assigning its new object. let say
for(int x=0; x<ballStorage.Length; x++){
ballStorage[x] = new Ball(x); //assign value of the ball inside constructor
//access the ball
ballStorage[x].bounce(); //example
// or you can do this, access previous ball
if(x > 0)
ballStorage[x-1].bounce();
}
{"a":"AAA","b":32.4,"c":34.65},
{"a":"AAM","b":10.8,"c":11.55}
I use JSONArray to get jsonarray above. I want to use this code get data b:10.8 if a:AAM.
public static void main(String[]args) throws Exception{
String get = sql.getQuote();
JSONArray jsonarray = new JSONArray(get);
for (int i = 0; i<jsonarray.length();i++){
JSONObject obj = jsonarray.getJSONObject(i);
String symbol = obj.getString("a");
if (symbol.equals("AAM")){
double price = obj.getDouble("b");
System.out.println(price);
} else {
break;
}
}
}
Please help me to fix it. Thank you in advance.
The problem is with else statement in for-loop.
Remove else block i.e.
else {
break;
}
In your code, else block stops the for-loop on very first iteration. Because here "a":"AAA". Since the condition if (symbol.equals("AAM")) is false so else block is executing which further breaks the loop iteration.
The problem is the break in the else, just take it out.
If anywhere, it would be most appropriate in the if part:
if (symbol.equals("AAM")) {
System.out.println(obj.getDouble("b"));
break; // or leave it out if there might be a second AAM entry
}
I need to clear all the fields in several arrays to re-load a new template to parse. I currently set up a function to clear all the relevant array lists and counts. Those are working fine except I am having a hard time clearing out obrDD inside my clearMicroArray() function.
Here is my current code (without logic to clear obrDD). How can I clear the dropdown information inside my function?
static void clearMicroArray() {
fullMicroArray.clear();
int returnSize = fullMicroArray.size();
System.out.println("size of full array" + returnSize);
obrCount = 0;
fileRowCount = 0;
// obr List
obrList.removeAll(obrList);
obxList.removeAll(obxList);
};
// Populating the OBR number in dropdown
public static void populateOBRDropdown(JComboBox<String> obrDD) {
for (int i = 0; i < fullMicroArray.size(); i++) {
if (fullMicroArray.get(i).substring(0, fullMicroArray.get(i).indexOf("|")).equals("OBR")) {
i++;
obrCount++;
obrDD.addItem("OBR " + obrCount);
}
}
}
obrDD is a JComboBox object that you pass into populateOBRDropdown. Your clearMicroArray method, however, also needs obrDD as an input in order for you to do anything with it. Once you pass a reference to the JComboBox, you can call removeAllItems or removeItem from clearMicroArray.
public static List<double[]> pc = new LinkedList();
public void fillTable1() {
int index = getWorstI();
double[] temp = pc.get(index);
for(int n=0;n<4;n++)
System.out.println( temp[n]);
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
(pc.get(i))[j]/=temp[j]; // values change only in object pc.get(1)! O_o
}
for(int i=0;i<4;i++)
Main.fillTheRow(table1, pc.get(i), i);
}
It is assumed to be only pc.get(0) that the value is correctly changed.
The variable temp and pc.get(0) is reference to same object.
So the results of "(pc.get(0))[j]/=temp[j]" will be all "1".
Therefor all the calculations after that become (pc.get(i))[j]/1 .
Change as follows not to become a reference.
double[] temp = new double[pc.get(index).length]; // new array
for(int n=0;n<4;n++) {
temp[n] = pc.get(index)[n]; // copy content
System.out.println(temp[n]);
}
I strongly suspect that your problem is somewhere else. I can see no reason why (pc.get(i))[j]/=temp[j]; would only work for i == 1. Perhaps there is a problem in the way you populate the pc list, or the way youo are reading it.
Are you sure the problem lies there?
Try printing the pc before and after the modification to see if it contains what you were actually expecting to be in pc;
Quick convenience method:
public static void printList(List<double[]> list)
{
for (double[] arr : list)
{
// import java.util.Arrays;
System.out.println(Arrays.toString(arr));
}
}