Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
below are the For Loop
//Declare 5 String variable
String p1, p2, p3, p4, p5;
for (int row = 1; row <= 5; row++) {
String pno = driver.findElement(
By.xpath("//*[#id='body']/table/tbody/tr[" + row + "]/td")).getText();
p1 = pno;
}
my question
while executing the First row (row =1) it should be assign that value in p1 variable
While executing the Second row (row = 2) it should be assign that value in p2 variable
, vice-versa
how could possible to assign the each row value with separate variable in java
you are looking for an array or ArrayList
//in your loop where i is iterator variable
arr[i] = someNewValue;
or if you are not sure how many elements
arrayList.add(someNewValue);
//Declare 5 String variable
String p1, p2, p3, p4, p5;
String[] pArray = new String[5]; // create an array to store values
for (int row = 1; row <= 5; row++) {
String pno = driver.findElement(
By.xpath("//*[#id='body']/table/tbody/tr[" + row + "]/td")).getText();
pArray[row - 1] = pno; // store value to an array
}
// put the values in the array to the Strings originally created
p1 = pArray[0];
p2 = pArray[1];
p3 = pArray[2];
p4 = pArray[3];
p5 = pArray[4];
You can use ArrayList if you want to dynamic sized collection. i.e when you don't know the exact size you would need
ArrayList<String> p = new ArrayList<String>();
for (int row = 1; row <= 5; row++) {
String pno = driver.findElement(
By.xpath("//*[#id='body']/table/tbody/tr[" + row + "]/td")).getText();
p.add(pno);
}
If you know the exact size, then String Array would be sufficient. I see that you have hardcoded the max value conditional statement in your for loop. then, it is evident that you know the exact size you need. Here, you can go for String array as well.
String[] p = new String[5]; // length is 5
for (int row = 1,count=0; row <= 5; row++,count++) {
String pno = driver.findElement(
By.xpath("//*[#id='body']/table/tbody/tr[" + row + "]/td")).getText();
p[count] = pno;
}
Hope you understand now
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
For the question below use 2d array greetings seen below:
![image of 2D array][1]
"bob"
"joe"
"billy"
"george"
"janise"
"dell"
The code that I wrote is:
public class c
{
// instance variables - replace the example
below with your own
private int columns; int rows;
/**
* Constructor for objects of class c
*/
public c()
{
int columns = 2;
int rows = 3;
String[][] newArray = new String[columns][rows];
newArray[0][0] = "bob";
newArray[0][1] = "joe";
newArray[1][0] = "billy";
newArray[1][1] = "george";
newArray[1][2] = "janise";
newArray[2][2] = "dell";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
}
}
When creating the array, you need to use new String[rows][columns], not new String[columns][rows].
The third row should then be assigned using newArray[2][0] and newArray[2][1].
You can use an array initializer to create the array in a much easier way:
String[][] newArray = { { "hello", "ni hao" },
{ "konnichiwa", "hola" },
{ "guten tag", "bonjour" } };
So... your code is close to being correct.
The first mistake is that you mixed the columns and rows: You are defining your array to have two columns, but in the end, you are using three (0, 1, 2).
The second mistake is in the last two assignments: You really did confuse the numbering here.
Your finished constructor should look like this:
int columns = 2;
int rows = 3;
String[][] newArray = new String[rows][columns];
newArray[0][0] = "hello";
newArray[0][1] = "ni hao";
newArray[1][0] = "konnichiwa";
newArray[1][1] = "hola";
newArray[2][0] = "guten tag";
newArray[2][1] = "bonjur";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
For any given dimension (here I just set it to 8 to make it easy) I want to print out the letters with odd indexes. So I am trying to get an output like: B D F H... and other letters depending on the dimension). I put a while loop to make the row 0 since I only want to print out the the odd letters on the first row and then inside the while loop I added a for loop to print out the columns (letters) with odd n. However I am getting a not an error:
error: not a statement
for (n = 1; n<dimension; n +2){
^
I am also unsure of where to put the loops to print out the odd letters.
This is my code so far:
public static void main(String[] args) {
int dimension = 8; // normally any given dimension (int dimension = Integer.parseInt(args[0]))
int n = dimension - 1;
int m = dimension -1;
char [] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int [] nums = new int [dimension];
for (int i = 0; i < dimension; i ++) {
nums [i] = i + 1;
}
int [] [] position = new int [dimension] [dimension];
for (int row = 0; row < dimension; row ++) {
for (int col = 0; col < dimension; col ++) {
position [row] [col] = alphabet[col] + nums[row];
}
}
char p = (char)(position[m][n] - nums[n]);
while (m == 0) {
for (n = 1; n<dimension; n +2){
System.out.println(p); //odd letters on the first row
}
}
}
}
Edit: the program is compiling but the loop is not working so I am not getting any output. How can I fix it?
This is a simple syntax error.
for (n = 1; n<dimension; n +2){
Should be:
for (n = 1; n<dimension; n += 2){
The final part of the for statement is an operation that can be used to change the iterator (or do other operations). If you consider the following line of code:
n +2;
This is not a valid statement by itself. However, the following statement is valid:
n += 2;
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 4 years ago.
Improve this question
I have one matrix which has two rows and one column. Each row has specific name such as row0 and row1. I added some numbers in each row and column of this matrix. For example I have String index which can get one of the name of rows (row0 or row1). How can I check if index == "row0" then print row0[1] and if index == "row1" then print row1[1]??
int[][] s = new int[2][3];
s[0][0] = 5;
s[0][1] = 10;
s[0][2] = 15;
s[0][1] = 25;
s[1][1] = 30;
s[2][1] = 45;
int[] row0 = new int[]{s[0][0], s[0][1], s[0][2]};
int[] row1 = new int[]{s[0][1], s[1][1], s[2][1]};
String index = "row0";
// if index= row0
System.out.println(row0[1]);
// if index=row1
System.out.println(row1[1]);
In your example there is probably something wrong:
int[][] s = new int[2][3]; // two rows, three cols
s[0][0] = 5;
s[0][1] = 10;
s[0][2] = 15;
s[0][1] = 25;
s[1][1] = 30;
s[2][1] = 45; // <---- 2 is out of bounds
int[] row0 = new int[]{s[0][0], s[0][1], s[0][2]};
int[] row1 = new int[]{s[0][1], s[1][1], s[2][1]};
Anyway, you could use a Map for your matrix and then address the rows with a String:
// ...code for s here..
Map<String, Integer[]> matrix = new HashMap<String, Integer[]>();
matrix.put("row0", new Integer[] {s[0][0], s[0][1], s[0][2]});
matrix.put("row1", new Integer[] {s[0][1], s[1][1], s[2][1]});
String index = "row0";
// if index= row0
System.out.println(matrix.get(index)[1]); // row0[1]
// if index=row1
index = "row1";
System.out.println(matrix.get(index)[1]); // row0[1]
You can not check the equality of String objects with the == operator.
You have to use .equals() instead. This question has been asked many times in StackOverflow and this answer, explains it concisely.
What you want to do, is something like this:
final String ARRAY_NAME1 = "row0";
final String ARRAY_NAME2 = "row1";
String index = "row0";
if(index.equals(ARRAY_NAME1))
System.out.print(row0[1]);
else if((index.equals(ARRAY_NAME2))
System.out.print(row1[1]);
We got that out of the way.
Now, you say that you don't want to hardcode this and I absolutely understand.
So, if I were in your position, I would create a new class for this. It's really simple and easy to do, even for a total beginner.
//class can be named anything. (As long as it is meaningful)
public class IntegerArrayIdentity{
/*This class will have two variables.
A name, for comparison operations and
the integer array that it will represent.*/
public String name;
public int[] array;
/*Simple constructor. Takes the name and array as its parameters.
You can change it to only take a name as a parameter, in case
that you want to initialise the array later. Add new methods as you like*/
public IntegerArrayIdentity(String name, int[] array){
this.name = name;
this.array = array;
}
}
Then in your code, you can do:
int[] row0 = {8, 3, 5, 143, 27, 0};
IntegerArrayIdentity row0id = new IntegerArrayIdentity("row0", row0);
if(index.equals(row0id.name))
System.out.println(row0id.array[1]);
output -> '3'
All the above is just an example. You can implement it however you like.
Hope it helped.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
i have program that makes operations on string and this is the principal function of one of those operations,this works perfectly,but it is not efficient:
private void printString(ArrayList<String> operations, ArrayList<String> set) {
int numerOfStrings = 0;
int numberOfletters = 0;
String toPrint = operations.get(1);
outOfLoop: for (int i = 0; i < set.size(); i++) {
String[] toFind = set.get(i).split(" ");
for (int k = 0; k < toFind.length; k++) {
if (toPrint.equals(toFind[k])) {
String[] splited = set.get(i).split(" ");
for (int j = 0; j < splited.length; j++) {
numberOfletters += splited[j].length();
}
numerOfStrings = splited.length;
break outOfLoop;
}
}
}
System.out.println(numerOfStrings + " " + numberOfletters);
}
explanation:
this function takes as parameter a an arrayList of operations, and an arraylist of set:
for the arraylist of operations i get always a specific position so,i don't iterate , it is always O(1)
for the arrayList of set i have to iterate,or rather, as I think of that to proceed:
so , for example, if have as operation print foo i have to do these steps:
first of all i have to find where foo is :
inside set i can have this situation:
position 1 : {car tree hotel}
...
position n : {foo lemon coffee}
when i find the string foo i have to print the number of strings inside that position and the number of letters of each string,so in this case i will print :
3(number of strings) 14(number of letters)
my program works,and also this function works, but it is a nasty solution,and not efficent,what do you think, how can I improve in efficiency in my program?
question inserted on code Review
try this version:
private void printString(ArrayList<String> operations, ArrayList<String> set) {
int numerOfStrings = 0;
int numberOfletters = 0;
String toPrint = operations.get(1);
for (String s : set) {
if (s.contains(toPrint)) {
String[] arr = s.split(" ");
numerOfStrings = arr.length;
numberOfletters = s.length() - (numerOfStrings - 1);
break;
}
}
System.out.println(numerOfStrings + " " + numberOfletters);
}
The 'worst' case here would be O(n2), because of for loop O(n) and contains O(n).
hope it helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
items_arr = 4;
System.out.println("The elements in the array are: ");
for (int x = 0; x < items_arr; x++)
System.out.println("Array[" + x + "]=" + array[x]);
System.out.print("\n");
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
for (s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
for (s = 0; s < items_arr; s++)
System.out.println("Array[" + s + "]=" + array[s]);
break;
The output is. The elements are
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Enter an element to Insert: 5
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Array [4]= 0
when I insert 5 it posts 0
any suggestions please.. thanks!
To insert in to the array you shuould be doing follwoing operation
array[s]=input
Two notes here
Arrays are fixed length, and you should be checking the array length before inserting values in to that,other wise you will get ArrayIndexOBException. Safer to sue List/Set
As better coding practise, and to improve the readablity, you should be enclosing the conditional/loop statements (such as if or for) - see eg below
eg: 1
for (int x = 0;x<items_arr;x++) {
System.out.println("Array["+x+"]="+array[x]);
}
eg 2:
for(int s = 0; s < items_arr; s++) {
if (array[s] == input) {
break;
}
}
You have not inserted 5 in your array,
do something after items_arr++
array[ items_arr] = input;
If you do not insert any thing then by default every element is 0
You should be using a Collection type; I would recommend an ArrayList - that is -
List<Integer> al = new ArrayList<Integer>();
for (int i = 1; i < 5; i++) {
al.add(i);
}
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
al.add(input); // And so on...
You are not updating/inserting the array with the new input.
for(s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
just replace the above code with
array[ items_arr] = input;
items_arr++;