adding inner label gives error on string variable - java

I have a special case in my code where a label would fix possible issues
start:
String zipCodes1 = "90019,90036,90016,90005,90010,90018,90020,90004,90048,90035,90211,90006,90038,90008,90089,90062,90007,90028,90034,90212,90232,90231,90233,90057,90209,90213,90070,90029,90069,90015,90056,90037,90017,90067,90043,90046,90026,90071,90014,90230,90064,90099,90302,90189,90011,90030,90050,90051,90052,90053,90054,90055,90060,90072,90074,90075,90076,90078,90080,90081,90082,90083,90086,90087,90088,90093,90024,90009,90013,90068,90090,90079,90012,90210,90021,90025,90306,90307,90308,90309,90310,90312,90311,90095,90305,91608,90027,90084,90066,90301,90073,90094,90047,91522,91604,90039,90003,90044,91602,90031,91204,91523,90033,90295,90001,90045,90077,91521,90065,90096,90404,90292,90405,90303,90058,90294,90304,90291,91209,91221,91222,91224,91225,91226,91210,91205,90023,91203,91601,91393,91506,91423,90063,90002,90403,90255,91607,91603,91609,91610,91611,91612,91614,91615,91616,91617,91618,90406,90407,90408,90409,90410,90411,90049,91201,91505,91502,90401,90251,90296,90293,91503,91507,91508,91510,91526,90402,91202,90250,91413,90032,90042,90061,90270,90041,91495,90245,91403,91606,90059,90249,91401,91501,91207,91206,91504,90261,90022,90247,91754,90201,90222,90091,90260,91208,91411,91803,90040,91030,91404,91407,91408,91409,91410,91470,91482,91496,91499,90506,90280,91605,90267,90266,90262,91436,91353,91031,91105,90248,91416,91426,91801,90272,91804,90504,90278,90202,91405,91123,91756,90223,90224,91802,91896,91899,91046,91714,91715,91352,91103,91716,91021,91316,91020,91755,90254,91102,91109,91110,91114,91115,91116,91117,91118,91121,91124,91125,91126,91129,91182,91184,91185,91188,91189,90220,91101,91406,91106,90640,91012,90747,91412,91214,91108,90746,91778,91402,91356,90239,90221,90240,91776,90723,90242,91043,90241,90503,90661,90662,90507,90508,90509,90510,91770,91775,91357,91104,90502,91331,91199,90660,90501,91334,91041,90277,91343,90749,91337,91003,91771,91772,91001,91385,91335,91333,90290,90805,90895,90745,91346,91395,90671,91107,91325,90706,90610,91040,90606,91733,91364,91371,91327,91328,91329,91345,90505,91780,91731,90264,91042,91341,91330,90707,90810,91365,91340,91007,91396,90710,91306,90807,90717,90712,90711,90714,90651,90652,91734,91735,91324,90650,91367,91305,91308,91309,91303,90601,91025,91394,90607,90608,90609,90670,91006,91066,91077,90602,91372,91024,91011,91732,90806,91392,90713,90748,90274,90744,91302,90702,90701,91344,90755,91746,90605,91023,90703";
String[] zipArray1 = zipCodes1.trim().split(",");
Random zipIndex1 = new Random();
int randomIndex1 = zipIndex1.nextInt(395);
System.out.println("Zip bad! New zip = "+zipArray1[randomIndex1]+"");
driver.findElement(By.xpath("//android.widget.EditText[#index='0']")).sendKeys(zipArray1[randomIndex1]);
Thread.sleep(750);
driver.pressKey(new KeyEvent(AndroidKey.BACK));
Thread.sleep(1000);
driver.findElement(saveLoc).click();
Thread.sleep(5750);
if (driver.findElements(By.xpath("//*[#text='Check your zip and try again']")).isEmpty()) {
System.out.println("Zip code change successful");
} else {
continue start;
}
}
but right after start on String zipCodes1 it gives me an error saying that:
String cannot be resolved to a variable
and
zipCodes1 cannot be resolved to a variable
why is this happening? is there a way to fix this?

Try doing this
while(true){
String zipCodes1 = "90019,90036,90016,90005,90010,90018,90020,90004,90048,90035,90211,90006,90038,90008,90089,90062,90007,90028,90034,90212,90232,90231,90233,90057,90209,90213,90070,90029,90069,90015,90056,90037,90017,90067,90043,90046,90026,90071,90014,90230,90064,90099,90302,90189,90011,90030,90050,90051,90052,90053,90054,90055,90060,90072,90074,90075,90076,90078,90080,90081,90082,90083,90086,90087,90088,90093,90024,90009,90013,90068,90090,90079,90012,90210,90021,90025,90306,90307,90308,90309,90310,90312,90311,90095,90305,91608,90027,90084,90066,90301,90073,90094,90047,91522,91604,90039,90003,90044,91602,90031,91204,91523,90033,90295,90001,90045,90077,91521,90065,90096,90404,90292,90405,90303,90058,90294,90304,90291,91209,91221,91222,91224,91225,91226,91210,91205,90023,91203,91601,91393,91506,91423,90063,90002,90403,90255,91607,91603,91609,91610,91611,91612,91614,91615,91616,91617,91618,90406,90407,90408,90409,90410,90411,90049,91201,91505,91502,90401,90251,90296,90293,91503,91507,91508,91510,91526,90402,91202,90250,91413,90032,90042,90061,90270,90041,91495,90245,91403,91606,90059,90249,91401,91501,91207,91206,91504,90261,90022,90247,91754,90201,90222,90091,90260,91208,91411,91803,90040,91030,91404,91407,91408,91409,91410,91470,91482,91496,91499,90506,90280,91605,90267,90266,90262,91436,91353,91031,91105,90248,91416,91426,91801,90272,91804,90504,90278,90202,91405,91123,91756,90223,90224,91802,91896,91899,91046,91714,91715,91352,91103,91716,91021,91316,91020,91755,90254,91102,91109,91110,91114,91115,91116,91117,91118,91121,91124,91125,91126,91129,91182,91184,91185,91188,91189,90220,91101,91406,91106,90640,91012,90747,91412,91214,91108,90746,91778,91402,91356,90239,90221,90240,91776,90723,90242,91043,90241,90503,90661,90662,90507,90508,90509,90510,91770,91775,91357,91104,90502,91331,91199,90660,90501,91334,91041,90277,91343,90749,91337,91003,91771,91772,91001,91385,91335,91333,90290,90805,90895,90745,91346,91395,90671,91107,91325,90706,90610,91040,90606,91733,91364,91371,91327,91328,91329,91345,90505,91780,91731,90264,91042,91341,91330,90707,90810,91365,91340,91007,91396,90710,91306,90807,90717,90712,90711,90714,90651,90652,91734,91735,91324,90650,91367,91305,91308,91309,91303,90601,91025,91394,90607,90608,90609,90670,91006,91066,91077,90602,91372,91024,91011,91732,90806,91392,90713,90748,90274,90744,91302,90702,90701,91344,90755,91746,90605,91023,90703";
String[] zipArray1 = zipCodes1.trim().split(",");
Random zipIndex1 = new Random();
int randomIndex1 = zipIndex1.nextInt(395);
System.out.println("Zip bad! New zip = "+zipArray1[randomIndex1]+"");
driver.findElement(By.xpath("//android.widget.EditText[#index='0']")).sendKeys(zipArray1[randomIndex1]);
Thread.sleep(750);
driver.pressKey(new KeyEvent(AndroidKey.BACK));
Thread.sleep(1000);
driver.findElement(saveLoc).click();
Thread.sleep(5750);
if (driver.findElements(By.xpath("//*[#text='Check your zip and try again']")).isEmpty()) {
System.out.println("Zip code change successful");
break;
}
}

From JLS 14.7. Labeled Statements
LabeledStatement:
Identifier : Statement
LabeledStatementNoShortIf:
Identifier : StatementNoShortIf
The Identifier is declared to be the label of the immediately contained Statement.
Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break or continue statements (§14.15, §14.16) appearing anywhere within the labeled statement.
The scope of a label of a labeled statement is the immediately contained Statement.
So you can't use like below:
start:
String zipCodes1 = "..."
One example you can use this with loops like answer by #Shrey Garg

Related

How to find what went wrong during eval in R?

Part of the code:
Rengine re = getRengine();
re.eval("library(quantmod)");
re.eval("library(PerformanceAnalytics)");
re.eval("library(tseries)");
re.eval("library(FinTS)");
re.eval("library(rugarch)");
re.eval("library(robustbase)");
re.assign("arLagNum", new double[]{1});
re.assign("maLagNum", new double[]{1});
re.assign("archLagNum", new double[]{1});
re.assign("garchLagNum", new double[]{1});
re.eval("garchSpec <- ugarchspec(variance.model = list(model=\"iGARCH\", garchOrder=c(archLagNum,garchLagNum)), mean.model = list(armaOrder=c(arLagNum,maLagNum)), distribution.model=\"std\")");
re.assign("transformedTsValueData", new double[]{getSomeDoubles()};
re.eval("estimates <- ugarchfit(spec = garchSpec, data = transformedTsValueData, solver.control = list(trace = 1))");
re.eval("estimates");
The last line returns null. The API documentation says: "the eval method returns null if something went wrong". How do I find out what went wrong?
Granted it's not the most elegant, but you could try getting some information if you put your command in a try catch:
re.eval("estimates <-tryCatch(suppressWarnings(ugarchfit(spec = garchSpec, data = transformedTsValueData, solver.control = list(trace = 1))), error = function(e) { paste(\"e: \",e$message) }, warning = function(w) { paste(\"w: \", w$message) })");
you can then evaluate the response by inspecting the first few 3 characters.
If you don't want to do this for every call, you could repeat your last command if your response is null when you're not expecting it (and repeating something that went wrong usually doesn't take too long).
Edit:
Come to think of it, if the error occurs only when you evaluate "estimates", it may be better to wrap that last one in the try catch:
re.eval("tryCatch(suppressWarnings(estimates), error = function(e) { paste(\"e: \",e$message) }, warning = function(w) { paste(\"w: \", w$message) })");

Problematic parsing of html table with selenium webdriver

I am using selenium webdriver with java to test a web application in my company and I face a very strange problem. The web application after logging in displays this table with the values as shown in the picture in the link below:
https://drive.google.com/file/d/0B0i_2gNTlx1Sek9EQVQ1ZlY1a0E/view?usp=sharing
I have wrote this code that parses the table with xpath because the table's rows and td cells are id-less.
//Parsing the table.
int increment = 0;
int i = 0;
for(increment=1; increment <= 16; increment++){
StringBuilder nsb = new StringBuilder("");
StringBuilder sb = new StringBuilder("");
for(i=1; i <= 6; i++){
try {
WebElement f = wd.findElement((By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/div[2]/div[2]/div/div[1]/form/div/div/table/tbody/tr["+increment+"]/td["+i+"]")));
sb.append(f.getText().replaceAll("\\n", " ")).append(" ");
} catch (StaleElementReferenceException ed) {
ed.getSuppressed();
}
}
System.out.println(sb);
}
System.out.println();
The problem now is that when I print the values of each row sometimes and in random order i lose some of the values on the output as shown in the picture i post below(for example if i re-run the program the second field in the second row called SAB will be missing).
Correct output is
https://drive.google.com/file/d/0B0i_2gNTlx1SQmFoZDd0UkgzVXM/view?usp=sharing
I tried instead of getText method the getAttribute("textContent") but i face the same random problem.
Does anyone have any idea why is this happening?
Sorry for the links but I didn't have any alternative to show you my problem..I can also post a random faulty output I got during testing if it's needed..
One thought,
why did you add ?
catch (StaleElementReferenceException ed) {
ed.getSuppressed();
}
Are you getting this exception if not catching?
if yes, then
might be your page or element is getting refreshed
and when you are referring f on second line to getText() you may get StaleElementReferenceException, But as you are suppressing it your loop continues and value is lost because of exception.
you can try replacing
WebElement f = wd.findElement((By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/div[2]/div[2]/div/div[1]/form/div/div/table/tbody/tr["+increment+"]/td["+i+"]")));
sb.append(f.getText().replaceAll("\\n", " ")).append(" ");
with single statement
sb.append(wd.findElement((By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/div[2]/div[2]/div/div[1]/form/div/div/table/tbody/tr["+increment+"]/td["+i+"]"))).getText().replaceAll("\\n", " ")).append(" ");
Probably this will avoid staleElement Exception if issue is caused by it.

Eclipse Java AST parser: insert statement before if/for/while

I'm using the org.eclipse.jdt parser.
I want to rewrite this code:
public void foo(){
...
...
if(a>b)
...
...
}
into this:
public void foo(){
...
...
System.out.println("hello");
if(a>b)
...
...
}
Supposing that ifnode is an IF_STATEMENT node, I can do something similar to this:
Block block = ast.newBlock();
TextElement siso = ast.newTextElement();
siso.setText("System.out.println(\"hello\");");
ListRewrite listRewrite = rewriter.getListRewrite(block, Block.STATEMENTS_PROPERTY);
listRewrite.insertFirst(ifnode, null);
listRewrite.insertFirst(siso, null);
rewriter.replace(ifnode, block, null);
but this will insert the syso statement at the beginning of the method, while I want it right before the if.
Is there a way to achieve it?
You can use the below code to achieve this (this will add the sysout just before the first IfStatement) :
Block block = ast.newBlock();
TextElement siso = ast.newTextElement();
siso.setText("System.out.println(\"hello\");");
ListRewrite listRewrite = rewriter.getListRewrite(block, CompilationUnit.IF_STATEMENT);
listRewrite.insertFirst(siso, null);
TextEdit edits = rewriter.rewriteAST(document, null);
Also you can limit the scope of rewrite to the IfStatement:
ASTRewrite rewriter = ASTRewrite.create(ifNode.getAST());
Note: code not tested. Do let me know if you find any issues.

Having problems using a Switch statement in Java

I'm making a Character Creator for fun and I seem have run into yet another java problem! I tried googling around for a bit but I didn't seem to find a working solution... I'm trying to use a switch statement for the compiler to output certain information depending on what class (Knight, Archer, Mage) the user chose, but when inputting my code I get error messages
My code so far (cleaned up a bit) is :
String name;
String className;
int attPoints;
System.out.println("Welcome to 'GameName's' Character Creator 2.0!\n");
Thread.sleep(500);
System.out.print("First off, what do you want your characters name to be? \n\nName : ");
name = Scan.nextLine();
Thread.sleep(500);
System.out.print("\nYou are now to be known as "+ name + "!");
System.out.print("\n\n" + name + ", what class do you want to be? ");
System.out.print("\n\nClasses available :\nKnight");
Thread.sleep(1500);
System.out.print("\nMage");
Thread.sleep(300);
System.out.print("\nDruid");
Thread.sleep(300);
System.out.print("\nNinja");
Thread.sleep(300);
System.out.print("\nArcher");
Thread.sleep(300);
System.out.print("\nAdventurer");
Thread.sleep(300);
System.out.print("\nBerserker");
Thread.sleep(300);
System.out.print("\n\nClass : ");
className = Scan.next();
Class userClass = Class.valueOf(className);
Thread.sleep(500);
System.out.println("\nCongratulations! Your class is now : "+ className + "!");
Thread.sleep(500);
// This is where I get an error.
// - Syntax error, insert "enum Identifier" to complete EnumHeaderName
// - Syntax error, insert "EnumBody" to complete BlockStatement
// - Syntax error on token "void", # expected
public void setClass()
switch (Class) {
case Knight:
System.out.println(" Various lore about knights ");
break;
}
}
I THINK I may be trying to create a class inside another class - but when I tried putting it outside it got another error...
Also, I have an int called attPoints, and after I choose a class I want to add 10 to it, but unsure how.
You're switching on Class which is a Java type. Take a look at this tutorial: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
You probably want to do something like:
switch (userClass) {
case Class.KNIGHT:
System.out.println(" Various lore about knights ");
break;
}

Why is String One = "Øzil" not a statement?

Here is my little example code:
String legend = "Øzil";
if (legend.equals("Øzil")) {
System.out.println("You should have bought him Moyes");
}
Whenever I try to compile this code I get this error message: error: Not a Statement* String legend = Øzil"
You have missed ) in the end of if statement
if (legend.equals("Øzil") {
^_____see here
It's because of the funny character you are using, you should find its ASCII code and use it instead.
EDIT: ooops now I see I got it wrong :)
try this one :
String legend = "Øzil";
if (legend.equals("Øzil")) {
System.out.println("You should have bought him Moyes");
}
Add end bracket ) to the if condition:
From
if (legend.equals("Øzil") {
to
if (legend.equals("Øzil")) {

Categories

Resources