How to print an item from list - java

I have a list. I want print list item horizontally with delimiter.
I got output like this:
OderHistory: Item1-1$
OderHistory: Item2-1$
OderHistory: Item3-1$
But i need like this:
OderHistory: Item1-1$Item2-1$Item3-1
Code here:
for(int i=0;i<itemList.size();i++){
String name = itemList.get(i).ItemName;
String quanty = itemList.get(i).Quantity;
Log.d("OderHistory",name+"-"+quanty+"$");
}
Anyone help to me!
Thanks in advance...

Try this,
String result="";
for(int i=0;i<itemList.size();i++){
String name = itemList.get(i).ItemName;
String quanty = itemList.get(i).Quantity;
result=result.concat(name+"-"+quanty+"$");
}
Log.d("OderHistory:",result);

Related

How to append the id to the url after assigning the id to the String

Scenario: I need to append the id to the url.
What I have done :
I have taken the last id from the table and stored it in a list:
Then I get the text of the id and is Stored in a String.
List<WebElement> id = driver.findElements(By.xpath("(//table[contains(#class,'mat-table')]//tr/td[1])[last()]"));
int rowsize = id.size();
for(int i=0;i<rowsize;i++)
{
String text = id.get(i).getText();
System.out.println("Get the id:"+text);
Then I use that text and append it to the URL
String confirmationURL = "https://test-websites.net/#/email?type=confirm";
String newurl = confirmationURL+"&id=text"; = **This part iam giving the text as id ... which is
wrong and I need to enter the id which I got from the list ....**
driver.get(newurl);
So Basically the url should be like: https://test-websites.net /#/email?type=confirm&id=47474
Can someone pls give inputs on what should be done?
You can create a new list of URLS, and can use add method to append text.
List<WebElement> id = driver.findElements(By.xpath("(//table[contains(#class,'mat-table')]//tr/td[1])[last()]"));
String confirmationURL = "https://test-websites.net/#/email?type=confirm";
List<String> newurls = new ArrayList<String>();
int rowsize = id.size();
for(int i = 0; i < rowsize; i++) {
String text = id.get(i).getText();
System.out.println("Get the id:"+text);
newurls.add(confirmationURL + "&id=" + text);
}
after successfully execution of this code, you'd have a newurls list with URLs ending with id's from //table[contains(#class,'mat-table')]//tr/td[1])[last()] xpath.

how get a specific value from a string?

i have have a string looks :
mystring = "<EQHO state="degraded"...> at /NE[1]/EQHO[2]/#state to <EQHO state="working"...> at /NE[1]/EQHO[1]/#state"
and i want to get this value :
value="NE[1]/EQHO[1]"
how can i achieve that ?
thanks
Try this:
mystring.substring(mystring.lastIndexOf("at /")+4, mystring.lastIndexOf("/#"))
but you probably should use a more generic solution. To extract all the section that have this format you can use something like this:
String mystring = "<EQHO state=\"degraded\"...> at /NE[1]/EQHO[2]/#state to <EQHO state=\"working\"...> at /NE[1]/EQHO[1]/#state";
ArrayList<String> values = new ArrayList<String>();
while(mystring.indexOf("at /") < mystring.indexOf("/#")){
String val = mystring.substring(mystring.indexOf("at /") + 4, mystring.indexOf("/#"));
values.add(val);
mystring = mystring.substring(mystring.indexOf("/#")+2);
}
System.out.println(values);
You can change the value of a string like this
mystring = "NE[1]/EQHO[1]";
Remember to include the semicolon!

Getting a part of a string in java

so, right now I have this String:
String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
I want to be able to get just this part of the the string but I'm at a loss as to how to do this.
Needed Output:
String[] requiredOutput;
requiredOutput[1] = 48526; // csfo_offering
requiredOutput[2] = csfo_add_ind A;
or
requiredOutput[2] = A; // csfo_add_ind
EDIT:
I have used some of your suggestions and am trying out subString but it seems like its a temp fix because if the length of the original string changes then it will throw a wrench in my calls. I will try regex next because it seems to go by pattern matching and I might be able to figure something out with that. Thanks everyone for all your help.
Suggestions are still appreciated!
Are the numbers always the same length? If so, use String.subString. If not use String.indexOf("csfo_add") to find the locations of the "csfo_add" parts and then find the relative locations of the required information.
Hi there you can also use split if you always have the same pattern for your string.
for example
String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
System.out.println(csfo.split("csfo_add_ind ")[1].split("\\]\\)")[0]);
Would get the requiredOutput[2] = A; // csfo_add_ind
and this would get the first one
String[] requiredOutput = new String[2];
String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
requiredOutput[0] = "csfo_add_ind " + csfo.split("csfo_add_ind ")[1].split("\\]\\)")[0];
requiredOutput[1] = csfo.split("\\]\\[csfo_add_ind ")[0].split("csfo_offering = ")[1];
//System.out.println(requiredOutput[0] + " et " + requiredOutput[1] );

Saving a JComboBox Selection to an ArrayList

I cannot retrieve the selection in the JComboBox that was selected by the user. the J ComboBox inclued a list of car registration numbers, which then the user has to select one and it will be added to the booking ArrayList. Unfortunately it is not working properly. and the booking is not being saved because of this. Please Help Me! Maybe I need to change the get Method for the combo box.
ArrayList BookingList = CarRentalSystem.BookingList;
ArrayList CarList = CarRentalSystem.CarList;
UsingFiles BookingFile = CarRentalSystem.BookingFile;
String [] regNums;
public NewBooking() {
regNums = new String[CarList.size()+1];
for (int i = 0; i< CarList.size();i++){
regNums[i] = ""+((Car)CarList.get(i)).getCarNum();
}
initComponents();
....
Booking book = new Booking();
String regNum = cmbCar.getActionCommand();
book.getRegNum();
Not easy to understand what u want can you add more code?
try this to convert your list into a String Array that u pass in as a argument to your combobox.
public String[] regNums() {
String tArray[] = null;
for (int i=0; i<carList.size(); i++){
//Convert into a String[] and put the arrayList values in it.
tArray = carList.toArray(new String[i]);
}
}

How to manually set an array in Java?

How do you manually assign variables to an array? I have a code fragment below.
I don't want to manually put shortStraight[0] = "211100", shortStraight[1] = "021110", and so on. Any help?
private String [] shortStraight;
public Sample () {
shorty = new String [12];
shorty = {211100, 021110, 002111, 121100, 112100, 111200, 012110, 011210, 011120, 001211, 001121, 001112 } //this line doesn't work.
Any help?
String[] shorty = {"211100", "021110", "002111", "121100", "112100", "111200", "012110", "011210", "011120", "001211", "001121", "001112"} ;
String[] shorty = {"211100", "021110", "002111", "121100", "112100", "111200", "012110", "011210", "011120", "001211", "001121", "001112"} ;
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Categories

Resources