I an tryin to convert a string array to a double array, depending on what weather station was chosen from database - some weather stations have no data, so the whole string array is filled with nulls(12 of them in fact) So obviosly if one of them stations is chosen I get an exception. I realy have no time to write a lot of code to work around it, since I have to submit my work very soon... Is there any solution to catch it and display an error message to user insted of lines and lines explaining an error? Thanks!
Here is the loop to convert a string array into a double array
for(int i = 0; i<12; i++)
{
avMaxTempOptimised[i] = Double.parseDouble(avMaxTempSplit[i]);
avMinTempOptimised[i] = Double.parseDouble(avMinTempSplit[i]);
meanTempOptimised[i] = Double.parseDouble(meanTempSplit[i]);
highestTempOptimised[i] = Double.parseDouble(highestTempSplit[i]);
lowestTempOptimised[i] = Double.parseDouble(lowestTempSplit[i]);
maxWindOptimised[i] = Double.parseDouble(maxWindSplit[i]);
totalRainfallOptimised[i] = Double.parseDouble(totalRainfallSplit[i]);
maxDayRainfallOptimised[i] = Double.parseDouble(maxDayRainfallSplit[i]);
rainDaysOptimised[i] = Double.parseDouble(rainDaysSplit[i]);
totalSunshineOptimised[i] = Double.parseDouble(totalSunshineSplit[i]);
mostSunshineDayOptimised[i] = Double.parseDouble(mostSunshineDaySplit[i]);
avMaxTemp2Optimised[i] = Double.parseDouble(avMaxTemp2Split[i]);
avMinTemp2Optimised[i] = Double.parseDouble(avMinTemp2Split[i]);
meanTemp2Optimised[i] = Double.parseDouble(meanTemp2Split[i]);
highestTemp2Optimised[i] = Double.parseDouble(highestTemp2Split[i]);
lowestTemp2Optimised[i] = Double.parseDouble(lowestTemp2Split[i]);
maxWind2Optimised[i] = Double.parseDouble(maxWind2Split[i]);
totalRainfall2Optimised[i] = Double.parseDouble(totalRainfall2Split[i]);
maxDayRainfall2Optimised[i] = Double.parseDouble(maxDayRainfall2Split[i]);
rainDays2Optimised[i] = Double.parseDouble(rainDays2Split[i]);
totalSunshine2Optimised[i] = Double.parseDouble(totalSunshine2Split[i]);
mostSunshineDay2Optimised[i] = Double.parseDouble(mostSunshineDay2Split[i]);
}
if you do try/catch outside for loop, you'll stop processing the rest of the loop. Try/catch inside for loop is probably closer, as long as you don't mind the unassigned values left in the array. Something like this might be best, changing Optimized arrays to type Double[]:
for(int i = 0; i<12; i++)
{
avMaxTempOptimised[i] = safeDouble(avMaxTempSplit[i]);
avMinTempOptimised[i] = safeDouble(avMinTempSplit[i]);
meanTempOptimised[i] = safeDouble(meanTempSplit[i]);
highestTempOptimised[i] = safeDouble(highestTempSplit[i]);
lowestTempOptimised[i] = safeDouble(lowestTempSplit[i]);
maxWindOptimised[i] = safeDouble(maxWindSplit[i]);
totalRainfallOptimised[i] = safeDouble(totalRainfallSplit[i]);
maxDayRainfallOptimised[i] = safeDouble(maxDayRainfallSplit[i]);
rainDaysOptimised[i] = safeDouble(rainDaysSplit[i]);
totalSunshineOptimised[i] = safeDouble(totalSunshineSplit[i]);
mostSunshineDayOptimised[i] = safeDouble(mostSunshineDaySplit[i]);
avMaxTemp2Optimised[i] = safeDouble(avMaxTemp2Split[i]);
avMinTemp2Optimised[i] = safeDouble(avMinTemp2Split[i]);
meanTemp2Optimised[i] = safeDouble(meanTemp2Split[i]);
highestTemp2Optimised[i] = safeDouble(highestTemp2Split[i]);
lowestTemp2Optimised[i] = safeDouble(lowestTemp2Split[i]);
maxWind2Optimised[i] = safeDouble(maxWind2Split[i]);
totalRainfall2Optimised[i] = safeDouble(totalRainfall2Split[i]);
maxDayRainfall2Optimised[i] = safeDouble(maxDayRainfall2Split[i]);
rainDays2Optimised[i] = safeDouble(rainDays2Split[i]);
totalSunshine2Optimised[i] = safeDouble(totalSunshine2Split[i]);
mostSunshineDay2Optimised[i] = safeDouble(mostSunshineDay2Split[i]);
}
...
safeDouble( String str){
return str== null ? null : Double.parseDouble(str);
}
Add in quick null checks where you read Optimized arrays and you're golden.
Related
I am attempting to get the items and some of the related information from a Purchase Order with SuiteTalk. I am able to get the desired Purchase Orders with TransactionSearch using the following in Scala:
val transactionSearch = new TransactionSearch
val search = new TransactionSearchBasic
...
search.setLastModifiedDate(searchLastModified) //Gets POs modified in the last 10 minutes
transactionSearch.setBasic(search)
val result = port.search(transactionSearch)
I am able to cast each result to a record as an instance of the PurchaseOrder class.
if (result.getStatus().isIsSuccess()) {
println("Transactions: " + result.getTotalRecords)
for (i <- 0 until result.getTotalRecords) {
try {
val record = result.getRecordList.getRecord.get(i).asInstanceOf[PurchaseOrder]
record.get<...>
}
catch {...}
}
}
From here I am able to use the getters to access the individual fields, except for the ItemList.
I can see in the NetSuite web interface that there are items attached to the Purchase Orders. However using getItemList on the result record is always returning a null response.
Any thoughts?
I think you have not used search preferences and that is why you are not able to fetch purchase order line items. You will have to use following search preferences in your code -
SearchPreferences prefrence = new SearchPreferences();
prefrence.bodyFieldsOnly = false;
_service.searchPreferences = prefrence;
Following is working example using above preferences -
private void SearchPurchaseOrderByID(string strPurchaseOrderId)
{
TransactionSearch tranSearch = new TransactionSearch();
TransactionSearchBasic tranSearchBasic = new TransactionSearchBasic();
RecordRef poRef = new RecordRef();
poRef.internalId = strPurchaseOrderId;
poRef.type = RecordType.purchaseOrder;
poRef.typeSpecified = true;
RecordRef[] poRefs = new RecordRef[1];
poRefs[0] = poRef;
SearchMultiSelectField poID = new SearchMultiSelectField();
poID.searchValue = poRefs;
poID.#operator = SearchMultiSelectFieldOperator.anyOf;
poID.operatorSpecified = true;
tranSearchBasic.internalId = poID;
tranSearch.basic = tranSearchBasic;
InitService();
SearchResult results = _service.search(tranSearch);
if (results.status.isSuccess && results.status.isSuccessSpecified)
{
Record[] poRecords = results.recordList;
PurchaseOrder purchaseOrder = (PurchaseOrder)poRecords[0];
PurchaseOrderItemList poItemList = purchaseOrder.itemList;
PurchaseOrderItem[] poItems = poItemList.item;
if (poItems != null && poItems.Length > 0)
{
for (var i = 0; i < poItems.Length; i++)
{
Console.WriteLine("Item Line On PO = " + poItems[i].line);
Console.WriteLine("Item Quantity = " + poItems[i].quantity);
Console.WriteLine("Item Descrition = " + poItems[i].description);
}
}
}
}
Comparing text extracted from two different loops in selenium and assert whether they are equal or not. Following is the selenium code, I need to compare two strings as project_text and actualVal:
driver.findElement(By.xpath(OR.getProperty("projects"))).click();
Thread.sleep(5000);
System.out.println("Selecting from list");
Select project_dropdown = new Select(driver.findElement(By.xpath(OR.getProperty("client"))));
project_dropdown.selectByVisibleText("Marketo");
System.out.println("Verifying the selection of the list");
String part1=OR.getProperty("project_start");
String part2=OR.getProperty("project_end");
String assign_part1=OR.getProperty("assign_users_start");
String assign_part2=OR.getProperty("assign_users_end");
int i=1;
String project_text = null;
String assign_text = null;
while(isElementPresent(part1+i+part2)){
project_text = driver.findElement(By.xpath(part1+i+part2)).getText();
assign_text = driver.findElement(By.xpath(assign_part1+i+assign_part2)).getText();
if (assign_text.contains("aaaa"))
System.out.println(project_text);
i++;
}
System.out.println("Project_text = " + project_text);
driver.findElement(By.xpath(OR.getProperty("calender"))).click();
Thread.sleep(5000);
driver.findElement(By.xpath(OR.getProperty("date_link"))).click();
Thread.sleep(5000);
Select project_dropdown1 = new Select(driver.findElement(By.xpath(OR.getProperty("client_select"))));
project_dropdown1.selectByVisibleText("Marketo");
WebElement project_droplist= driver.findElement(By.xpath(OR.getProperty("project_select")));
List<WebElement> droplist_cotents = project_droplist.findElements(By.tagName("option"));
System.out.println(droplist_cotents.size());
//System.out.println(droplist_cotents.get(3).getText());
String actualVal=null;
for(int j=0;j<droplist_cotents.size();j++){
actualVal = droplist_cotents.get(j).getText();
System.out.println(actualVal);
}
System.out.println("actualVal = " + actualVal);
Assert.assertEquals(project_text, actualVal);
As i can see from your code, your project_text and actualVal are in the form of strings. The best way to compare values in them is to store them as string array's as you are looping through many values and you need to store them somewhere to assert. Here's how to store the values in an array and compare them -
int i = 0;
String[] project_text = new String[100];
String[] actualVal = new String[100];
while(isElementPresent(part1+i+part2)){
project_text[i] = driver.findElement(By.xpath(part1+i+part2)).getText();
assign_text = driver.findElement(By.xpath(assign_part1+i+assign_part2)).getText();
if (assign_text.contains("aaaa"))
System.out.println(project_text[i]);
i++;
}
for(int j=0;j<droplist_cotents.size();j++){
actualVal[j] = droplist_cotents.get(j).getText();
System.out.println(actualVal[j]);
}
for(int i = 0;i<project_text.length();i++)
Assert.assertEquals(project_text[i], actualVal[i]);
However if you don't know the size of the array, use ArrayList and work it out. Here's a sample.
Hope this helps.
I am working on a JSF based Web Application where I read contents from a file(dumpfile) and then parse it using a logic and keep adding it to a list using an object and also set a string using the object. But I keep getting this error. I am confused where I am wrong. I am a beginner so can anyone be kind enough to help me?
List<DumpController> FinalDumpNotes;
public List<DumpController> initializeDumpNotes()
throws SocketException, IOException {
PostProcessedDump postProcessedDump = (PostProcessedDump) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("postProcessedDump");
List<DumpController> FinalNotes = new ArrayList<>();
if (postProcessedDump.getDumpNotes() == null) {
dumpNotes = new DumpNotes();
}
DumpListController dlcon = (DumpListController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dumpListController");
DumpInfo dumpinfo = dlcon.getSelectedDumpInfo();
String fileName = dumpinfo.getDate() + dumpinfo.getTime() + dumpinfo.getSeqNo() + dumpinfo.getType() + dumpinfo.getTape() + dumpinfo.getDescription() + ".txt";
if (checkFileExistsInWin(fileName)) {
postProcessedDump.setDumpnotescontent(getFileContentsFromWin(fileName));
String consolidateDumpnotes = getFileContentsFromWin(fileName);
String lines[];
String content = "";
lines = consolidateDumpnotes.split("\\r?\\n");
List<String> finallines = new ArrayList<>();
int k = 0;
for (int i = 0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
finallines.add(lines[i]);
k++;
}
}
for (int j = 0; j < finallines.size(); j++) {
if (finallines.get(j).startsWith("---------------------SAVED BY")) {
PostProcessedDump dump = new PostProcessedDump();
dump.setDumpMessage(content);
content = "";
FinalDumpNotes.add(dump);
} else {
content = content + finallines.get(j);
}
}
}
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("postProcessedDump", postProcessedDump);
return FinalDumpNotes;
}
I get the following error:
If you want to add instances of type PostProcessedDump to your List you should change it's type. Also, don't forget to initialize it. Something like,
List<PostProcessedDump> FinalDumpNotes = new ArrayList<>();
Also, Java naming convention is to start variable names with a lower case letter. FinalDumpNotes looks like a class, I would suggest something like
List<PostProcessedDump> processedList = new ArrayList<>();
Problems with your code:
List<DumpController> FinalDumpNotes;
You declare FinalDumpNotes to be a List of DumpController objects, but you never initialize it. In addition, your IDE is barfing on the following line of code:
FinalDumpNotes.add(dump);
because you are attempting to add a PostProcessedDump object to the List instead of a DumpController object.
For starters, you need to initialize your list like this:
List<DumpController> finalDumpNotes = new ArrayList<DumpController>();
Notice that I have made the variable name beginning with lower case, which is the convention (upper case is normally reserved for classes and interfaces).
I will leave it to you as a homework assignment to sort out the correct usage of this List.
I am trying to create a double string. I thought that this is one way to assign values. I know there are better ways, but it was suggested by my teacher to do it this way. However when I put this in I get errors for each one stating:
can't find symbol cellPhoneNumbers
']' expected
Ultimately what I am trying to do is create a graph that looks something like this
Chile *******
Sweden *
Peru ***************
public class GraphNumbers
{
String[][] cellPhoneNumbers = new String[5][1];
cellPhoneNumbers[0][0] = "Chile";
cellPhoneNumbers[0][1] = "21";
cellPhoneNumbers[1][0] = "Sweden";
cellPhoneNumbers[1][1] = "11";
cellPhoneNumbers[2][0] = "Peru";
cellPhoneNumbers[2][1] = "33";
cellPhoneNumbers[3][0] = "Bulgaria";
cellPhoneNumbers[3][1] = "10";
cellPhoneNumbers[4][0] = "Guatemala";
cellPhoneNumbers[4][1] = "18";
}
Why am I receiving this message?
Some of the code must be placed in a method for example:
public class GraphNumbers
{
//changed the size of the array so you could do what you want
//you must have had a misscount when you originally made it
String[][] cellPhoneNumbers = new String[5][2];
//put in constructor or another appropriately named method
public GraphNumbers()
{
cellPhoneNumbers[0][0] = "Chile";
cellPhoneNumbers[0][1] = "21";
cellPhoneNumbers[1][0] = "Sweden";
cellPhoneNumbers[1][1] = "11";
cellPhoneNumbers[2][0] = "Peru";
cellPhoneNumbers[2][1] = "33";
cellPhoneNumbers[3][0] = "Bulgaria";
cellPhoneNumbers[3][1] = "10";
cellPhoneNumbers[4][0] = "Guatemala";
cellPhoneNumbers[4][1] = "18";
}
}
As per Java language syntax, you cannot put executable statements in class. Those should be put in either method/constructor/code blocks.
So you need to move these statements:
cellPhoneNumbers[0][0] = "Chile";
cellPhoneNumbers[0][1] = "21";
cellPhoneNumbers[1][0] = "Sweden";
cellPhoneNumbers[1][1] = "11";
cellPhoneNumbers[2][0] = "Peru";
cellPhoneNumbers[2][1] = "33";
cellPhoneNumbers[3][0] = "Bulgaria";
cellPhoneNumbers[3][1] = "10";
cellPhoneNumbers[4][0] = "Guatemala";
cellPhoneNumbers[4][1] = "18";
to appropriate place, maybe in a constructor.
Also your code is overflowing the array in stamens such as :
cellPhoneNumbers[0][1] = "21";
so you need the second dimension of array to be of size 2 and not 1. Change this
String[][] cellPhoneNumbers = new String[5][1];
to
String[][] cellPhoneNumbers = new String[5][2];
I have some xml data I am looping through. I would like to store each "entry" in an array spot in order to see it with an intent.putExtras(). My data has 3 elements: latlon,name,description. I would like to put each in my array. So I am setting it up like so: markerInfo[i][0] = Loc; etc... like so:
final List<XmlDom> entries = xml.tags("Placemark");
int i = entries.size();
int j=0;
for (XmlDom entry : entries) {
XmlDom lon = entry.tag("longitude");
XmlDom lat = entry.tag("latitude");
XmlDom name = entry.tag("name");
XmlDom desc = entry.tag("description");
String cdatareplace = desc.toString();
String description = cdatareplace.replace("<![CDATA[", "");
description = description.replace("]]>", "");
final String firename = name.text();
final String firedesc = description;
String geoLon = lon.text();
String geoLat = lat.text();
String coor = lat + "," + lon;
// Log.e("COORS: ", coor);
double lati = Double.parseDouble(geoLat);
double lngi = Double.parseDouble(geoLon);
LOCATION = new LatLng(lati, lngi);
String Loc = LOCATION.toString();
String[][] markerInfo = new String[i][3];
markerInfo[j][0] = Loc;
markerInfo[j][1] = firename;
markerInfo[j][2] = firedesc;
Log.e("MARKERINFO",markerInfo[j][0]);
Log.e("MARKERINFO",markerInfo[j][1]);
Log.e("MARKERINFO",markerInfo[j][2]);
map.addMarker(new MarkerOptions()
.position(LOCATION)
.title(markerInfo[j][1])
.snippet(markerInfo[j][2])
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.wfmi_icon48)));
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker arg0) {
// Show full description in new activity.
// fireDesc(arg0.getTitle(), arg0.getSnippet());
Intent i = new Intent(Map.this, MapSingleActivity.class);
i.putExtra("name", arg0.getTitle())
.putExtra("description", arg0.getSnippet())
.putExtra("lat", arg0.getPosition().latitude)
.putExtra("lon", arg0.getPosition().longitude);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
j++;
}
I am getting array index out of bounds. I figured if I filled it with the entries.size() that would not be the problem, so maybe i am not telling it how big correctly?
Thanks for any help
You need to make sure that the second dimension is also big enough
Fix it by changing the declaration of markerInfo to:
String[][] markerInfo = new String[i][3];
At the moment you are only creating i empty String arrays. With the above code you will create i arrays that can hold three String objects each.
Also, at the moment you are writing to the last location which is outside of the array bounds.
You need to change it to write to an available location. If you are trying to write to the last available location that would be i-1.
markerInfo[i-1][0] = Loc;
markerInfo[i-1][1] = firename;
markerInfo[i-1][2] = firedesc;
Looking at your code however, it seems like you may want to declare markerInfo outside of the loop and create a counter variable that you increment at each step of the loop.
Second size of the array is 0 - new String[i][0]. Then you try to insert something on position 0,1,2, which are not available.
Even if you write new String[i][3], then the maximum index is i-1.