How to merge two strings into another variable - java

The following is the situation:
I have three String parameters
a1,a2,a3
Each of the parameter has a different number inside
a1: 12
a2: 34
a3: 56
So using a for -loop i want to insert these number into a method
items = the amount of parameters so in this case 3
for (int i=1;i<=items;i++){
popupCmplx_RPM(a+i);
sleep(2);
}
So the problem is if I run the functionality it will create
for the String a1: a+i -> 121 instead of 12
The parameters are already set I can't change that part of the code
so help is appreciated. I sure there is an easier way without the parameters, but other that adding new code I can't remove those
The total amount of parameters set at the moment are 16 some of which can be 0
so in this example there are only three and the rest are zero.
with the int items variable the amount of parameters used is given

It looks like you're looping and trying to use the index of the loop to reference a variable. You cannot do that (without reflection) in Java.
(If that's an incorrect interpretation of your question, please update it to clarify.)
You probably have a couple options:
Just reference the variables without looping:
popupCmplx_RPM(a1);
sleep(2);
popupCmplx_RPM(a2);
sleep(2);
popupCmplx_RPM(a3);
sleep(2);
Store the values in a collection instead of individual variables:
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(34);
list.add(56);
for(Integer value : list) {
popupCmplx_RPM(value);
sleep(2);
}

You have to parse the String as an int, and then add one to it, like
int myInt = Integer.parseInt(a) + 1;
popupCmplx_RPM(myInt);
Careful, this can throw NumberFormatException if a isn't a valid integer.

Related

How to add the result of a quake entry index to a new list?

What is the variable type of quakeData.get(bigIndex)? How do I add quakeData.get(bigIndex) to my new list? I plan on passing a substring with each iteration to find the Largest quake how should I go about that?
I have also been using the .add() and it hasn't been working. Also, the x value is incorrect because I do not know what the variable type is.
public ArrayList<QuakeEntry>getLargest(ArrayList<QuakeEntry>quakeData,int howMany){
ArrayList<QuakeEntry> answer = new ArrayList<QuakeEntry>();
int bigIndex=indexOfLargest(quakeData);
x=quakeData.get(bigIndex);
// I do not understand what the data type I am getting back and I do not
currently know how to add it to my new Arraylist
answer=answer.set(0,x);"
// I will be adding a for loop and use variable k to replace 0 to get the 2nd, 3rd, 4th,etc highest by passing in a substring with each
// iteration
return answer;
}

Using temporary variables to manipulate values in array

So say I have an array with columns {item, quantity}. I am getting these values from an sql table, then sorting them by item. I am using a temporary variable "quantity" to manipulate the double value:
while(rs.next()){
item = rs.getString(1);
for(int i = 0 ; i < array.size ; i++){
if(item.equals(array[i][0]){
double quantity = rs.getDouble(2);
quantity = quantity + Double.parseDouble(array[i][1]);
array[i][1] = quantity;
}
}
//add code for adding items if not found in array
}
My question is will using this temporary variable be slower/create garbage while it iterates the array? Does Java properly dispose of this variable at the end of the if statement? I am asking because I am having issues with heap space, and I do not want to have to rewrite everything to accomodate this.
My other option is:
if(item.equals(array[i][0]){
array[i][1] = String.valueOf(Double.parseDouble(array[i][1]) + rs.getDouble(2));
}
Thanks for reading
It will always override quantity with new value, when old value will be keep of course in array. Quantity won't have impact on your memory especially that this is primitive.
If you will deal with complex type and you will do something like that, you will override with every loop the reference to object, so in array you will have all the time reference to same object.

create a class for each user name with varrying variables [duplicate]

This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 5 years ago.
Okay so for what I am doing i need to increment my variables name, so for example
int Taco1 = 23432.....
int Taco2 = 234235656.....
int Taco3 = 11111.......
But instead i need it to be a variable like
int X = 0;
some method with loop or recursion()
int Taco(X) = bla bla bla
x++
Trying to get my variable names to auto name themselves incremented by 1 every time, so they don't overwrite themselves. If this is impossible then my apologies.
You can't do this in Java and more importantly, you don't want to do this as this isn't how Java works. In fact variable names aren't nearly as important as you think and hardly even exist in compiled code. What is much more important is that you are able to get a reference to your objects in as easy and reliable a way as possible. This can involve an array, an ArrayList (likely what you want here), a LinkedList, a Map such as a HashMap, a Set, and other types of collections.
For example:
List<Taco> tacoList = new ArrayList<Taco>();
for (int i = 0; i < MAX_TACOS; i++) {
tacoList.add(new Taco(i));
}
Indeed it is impossible to generate identifier names based on a variable. Perhaps what you want is an array:
int[] Taco = new int[ 15 /*some appropiate upper limit*/ ];
Taco[X] = bla bla bla;
Search the web for basic information on what arrays are and how they work.
Use an int[] or a List<Integer>:
int[] tacos = new int[numberOfTacos];
// in some loop or whatever
tacos[x] = someValue;
use array of int. say int taco[50]; and you can reference each location as taco[0],taco[1] etc
I TacoX is going to be an integer, I would create an array of X ints. If the max number is 10, we have:
int[] taco = new int[10];
Then to modify/read tacoX, you just look at taco[X]

check if array is full

I am trying to do check if an array is full and print out to the user he is unable to enter more books.
static Object[][] books=new Object[2][];
I am asking 3 values from the user and am storing to another array called "row".
Object[]row=new Object[3];
After that i loop through the books array and check if it has a null value inside of it and add the "row" array with the given values of the user.
the problem am having is i cant give any feedback if books array is full after entering two rows of values.
boolean empty=false;
while(empty==false){
for (int i = 0; i < books.length; i++) {
if(books[i]==null){
books[i]=row;
empty=true;
break;
}
}
}
Why not having a variable
int bookCounter = 0;
which you can increase/decrease everytime you add/delete books and then just check it against your max number of books (which I assume is books.length)
In this way you don't need to loop over the array, which is not efficient.
There is no need for the while loop. After the for loop ends, check your empty variable that you're already setting. If it's false, then give your feedback to the user.
Arrays are always full. Even each element is null. If you initialize it with the size of 10, then JVM will alocate memory and fill the all the arrays positions with default value. What is: 0 for primitives numbers and char type, false for primitive boolean type and null for Objects.
So, your code won't work with a int[] for instance. Because there won't exist any null element.
That's why you dont have a count() method... You can create a method that is named countNotNull(). But you'll need to interate for all the array long.
The best solution is to use a variable to count when you add/remove itens form array. As
Guillermo Merino said.

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.

Categories