Increment e009 to e010 using java - java

I have a table with fields like empNo, Name , etc.
and i want java to generate numbers automatically.
for eg. if the number of the last employee was e009,
java should give me the number for the next one as e010.
I tried this and it works well but seems tedious.
rs.last();
String no,temp;
no = rs.getString(1);
int len = no.length();
len=len-1;
int n = Integer.parseInt(no.substring(1,no.length()));
n=n+1;
int length = (int)(Math.log10(n)+1);
no="E";
for(int i=1;i<=len-length;i++)
no=no+"0";
no=no+n;
txtNo.setText(no);

Have a static counter variable inside the class that represents your table's row. In its constructor, assign the empNo value to the value of the counter.
You could also add formatting to make the number a fixed size of, say, 3 in another method or within the constructor itself.
And don't forget to increment the counter within the constructor.

Related

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]

Create A Method In Java

Hello I am trying to create a method in Java that Accepts an integer from the user. Calculate and display how many occurences of the integer are in the array(i'm Creating a random array) as well as what percentage of the array values is the entered integer.
This is how i create my Array:
public void fillVector ( )
{
int myarray[] = new int [10];
for (int i = 0 ; i < 10 ; i++)
{
myarray [i] = (int) (Math.random () * 10);
}
}
Any sugestions how can i do to accomplish this ?
This seems like a homework to you so I am not gonna give you the full solution but I will break down the steps of what you need to do in order to solve your problem. You have to find out how to code those steps yourself, or at least provide some code and your specific problem because your question is too vague right now.
Ask the user to input the number.
Store that number somewhere.
Check each cell of the array for that number. If you find one appearance
increase the counter and continue until the end of your index.
Print out the appearances of the given number.
Print out the percentage of the cells containing the given value to the total amount of cells.
As I can see from your code (if it's yours) you are capable to pull this off on your own. It shouldn't be too hard.

Access String Array outside of two loops

I have an array of Strings created inside of a while loop nested inside a for loop. This has to do with creating an array from a column of strings in Derby but I will leave out some stuff for simplicity's sake.
The reason I am not providing the full code is because the problem is very specific. I am having no problems whatsoever with my database, resultset, statements, or queries. It's just accessing the array outside of the two loops.
//in the class,
private int rowCount; //Count of the rows in my column of strings
public String stringArray[];
//in the method I am using
public void myMethod() {
rowCount = 4; //In my code it actually counts it, lets say its four though.
stringArray = new stringArray[rowCount]; //set
for (int i = 0; i < rowCount; i++) {
while (rs.next()/*rs is simply a result set of a statement executeQuery to select the correct table*/)
{
stringArray[i] = rs.getString(2); //2 is the column number in the table
}
}
//Need to access stringArray here. When I try to print out values, it always prints out as null.
}
Thanks!
There's something wrong with your nested loops. For each row (each value of i / each execution of the outer loop), you iterate through your whole result set and overwrite stringArray[i] (i not chaning) many times.
When you get to the second row (i.e. i is 1 or higher), rs.next() will already be false, since you tranversed the whole rs in the first iteration of the outer loop.
Maybe you just need to replace your inner loop while(rs.next()) with a single call to rs.next()
Perhaps in your actual code, where you say:
rowCount = 4; //In my code it actually counts it, lets say its four though.
you are performing the count of the rows by doing something like:
for( rowCount = 0; rs.next(); rowCount++ ) { }
If you're doing something like that, then you've basically read all the rows already during the counting phase, and when you try to re-process the rows later the rs.next() method simply returns false since it's already read all the rows.
Of course, I'm just guessing...

How to merge two strings into another variable

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.

Categories