2 questions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

2 questions

1. why does it say "orphaned default" and how can I fix it? 2. the variables "qh" and "qj" are not being picked up is there a reason for this or is it not allowed in java? https://code.sololearn.com/cwvVipCEpbYI/?ref=app

21st Dec 2017, 1:33 AM
jalen
jalen - avatar
9 Answers
+ 1
1) I think you misplace a default it should be one curly brace lover : } System.out.print(password3); } break; // default was here } default: System.out.print("An error occured, check if your inputs are correct and enter it again"); break; } 2) If I am not mistaken you declare those vars inside loops, so they will exist only in this loop. 3) It is really hard to read code you written: Names of variables better be more descriptive. If you mistype gh with qj how fast you will see that it is other variable you wanted to use? If you have a lot of repeated actions it is better to think how not to duplicate them, may be you can take look at Matcher or something like this or came up with some separated method to do some of repeated things.
21st Dec 2017, 2:20 AM
Dima Makieiev
Dima Makieiev - avatar
+ 1
I assume you get my point about var declaring. If you do this If (condition) {int a =1; } else {a=2;} you will get a error. you need to do this: int a; if (condition) {a=1;} else {a=2;} so variable will be seen in both parts of if.
21st Dec 2017, 3:36 AM
Dima Makieiev
Dima Makieiev - avatar
+ 1
You can put them after String type = token.nextToken(); before first while. And than use them in all if statements you need.
21st Dec 2017, 5:13 AM
Dima Makieiev
Dima Makieiev - avatar
+ 1
If I get what you want to achieve you can do it like this: String optiona; if (user chooses 1) {optiona="one";} else if (user chooses 2) {optiona="two";} after this optiona will be have one OR two not the both
21st Dec 2017, 5:55 AM
Dima Makieiev
Dima Makieiev - avatar
0
I made some changes to the code including the variables qj and qh to optiona and optionb I hope its easier to read in thinking the problem is that the } are closing off some of the code making it so when I use the 2 variables it doesn't know what they are, I was just using this method to make the code shorter so I don't have to use a ton of else if statements. Correct me if I'm wrong but I'm just assuming that's the problem with the code right now.
21st Dec 2017, 3:27 AM
jalen
jalen - avatar
0
I think see, would I put string optiona/optionb on top of the if statement or on each individual else if statement?
21st Dec 2017, 4:13 AM
jalen
jalen - avatar
0
how would I do that because optiona/optionb is supposed to be one of many methods depending on what the user chooses?
21st Dec 2017, 5:47 AM
jalen
jalen - avatar
21st Dec 2017, 5:58 AM
Vova
Vova - avatar
0
I fixed it a certain way I added 2 strings the same way you mentioned earlier I don't know if I did it correctly though
21st Dec 2017, 7:09 AM
jalen
jalen - avatar