Because the eclipse was error:
else delete token????
the code is this:
public void go(View v)
{
String a= url.getText().toString();
if (a.contains("www.") == true); {
webView.loadUrl(url.getText().toString());
}
else {
String search = "https://www.google.com/search?q="+url.getText();
webView.loadUrl(search);
}
}
PLease help me
An if statement requires a body - remove the semi-colon
if (a.contains("www.") == true); {
^
if (a.contains("www.") == true);
==
if (a.contains("www.") == true) { }
Which does nothing. So webView.loadUrl(url.getText().toString()); will be always executed since it's in an inner block.
If you're using an IDE, you can make it warn you of things like that (empty if statements).
You're getting an error because your code is:
if (a.contains("www.") == true) { }
{
//some code
}
else {
}
But the else has no if before it..
Replace this
if (a.contains("www.") == true); {
with this
if (a.contains("www.") == true) {
Related
This is what I've tried:
private static ApplicationGroup fetchDashboardParamInfo(List<ApplicationGroup> applicationGroup, String uniqueId) {
ApplicationGroup dashParamInfo = null;
for (ApplicationGroup a : applicationGroup) {
if (a.getUniqueId() == null || !a.getUniqueId().equals(uniqueId)) {
fetchDashboardParamInfo(a.getChildren(), uniqueId);
} else if (a.getUniqueId().equals(uniqueId)) {
dashParamInfo = a;
}
}
return dashParamInfo;
}
I'm simply running throug applicationGroup which is a list of application groups. It's actually a hierarchy of application groups. I'm trying to make the method recurse if the "if"-statement is true, with a.getChildren() as the new a. If the "else if"-statement is true, dashParamInfo should simply be equal to whatever a is at that point, and then the method should return dashParamInfo without further looping. The problem I have is that when the "if"-statement becomes true, it doesn't recurse, it goes inside the statement but then it just goes to return and ends the method right away. What am I doin wrong?
If the correct answer is not found in the current applicationGroup you need to recurse deeper. But if you recurse you need to check if the recurrent call has found what you were looking for; if so, you need to return it.
private static ApplicationGroup fetchDashboardParamInfo(List<ApplicationGroup> applicationGroup, String uniqueId) {
for (ApplicationGroup a : applicationGroup) {
if (a.getUniqueId() == null || !a.getUniqueId().equals(uniqueId)) {
ApplicationGroup dashParamInfo = fetchDashboardParamInfo(a.getChildren(), uniqueId);
if (dashParamInfo != null)
return dashParamInfo;
} else if (a.getUniqueId().equals(uniqueId)) {
return a;
}
}
return null;
}
I think you are missing return before fetchDashboardParamInfo(a.getChildren(), uniqueId);
private static ApplicationGroup fetchDashboardParamInfo(List<ApplicationGroup> applicationGroup, String uniqueId) {
ApplicationGroup dashParamInfo = null;
for (ApplicationGroup a : applicationGroup) {
if (a.getUniqueId() == null || !a.getUniqueId().equals(uniqueId)) {
return fetchDashboardParamInfo(a.getChildren(), uniqueId);
} else if (a.getUniqueId().equals(uniqueId)) {
dashParamInfo = a;
break;
}
}
return dashParamInfo;
}
Im not being able to fix this code smell:
public static boolean esStringVacio(final Object valor) {
if (valor == null) {
return true;
}
String valorTrim = valor.toString().trim();
if ((valorTrim).equals("")) {
return true;
}
if ((valorTrim).equals("null")) {
return true;
}
return false;
}
Tried like so but code smell persist:
if (valor == null || valor.toString().trim().equals("") || valor.toString().trim().equals("null")) {
return true;
} else {
return false;
}
You can shorten it to:
return (valor == null || valor.toString().trim().equals("") || valor.toString().trim().equals("null"));
Edit :
You can shorten even more to:
return ((String.valueOf(valor).trim().equals("null")) || (StringUtils.isBlank(valor)) ;
Thanks to Ernest for suggesting this.
You can combine the last 3 returns into a single OR, and it'd still be reliable / readable.
public static boolean esStringVacio(final Object valor) {
if (valor == null) {
return true;
}
String valorTrim = valor.toString().trim();
return valorTrim.equals("") || valorTrim.equals("null");
}
I'm trying to create a program which prints a datastructure from the input. The input and output looks like this: http://puu.sh/kDMc9/2d46462d4d.png. So for example, in the first test case: the first line indicates how many lines will follow in that case. Then if it's the number 1 as the first number on a line it means that you want to add elements to stack/queue/priority-queue and 2 means you want to take out an element, so the second number on a line is the value. Then the output prints if it's stack,queue, priority-queue, impossible or not sure(can be more than one)
This is the code I have now:
import java.util.PriorityQueue;
import java.util.Scanner;
public class DataStructure {
public static void main(String[] args)
{
while(calculate());
}
private static boolean calculate()
{
Scanner input = new Scanner(System.in);
int numberOfRowsPerCase = input.nextInt();
Stack<Integer> stack = new Stack<Integer>();
Queue<Integer> queue = new Queue<Integer>();
PriorityQueue<Integer> prioQueue = new PriorityQueue<Integer>();
boolean stackBool = true;
boolean queueBool = true;
boolean prioQueueBool = true;
int next;
for(int i = 0; i < numberOfRowsPerCase; i++)
{
next = input.nextInt();
if(next == 1)
{
next = input.nextInt();
stack.push(next);
queue.enqueue(next);
prioQueue.add(next);
}
else if(next == 2)
{
next = input.nextInt();
if(!stack.pop().equals(next))
{
stackBool = false;
}
else if(!queue.dequeue().equals(next))
{
queueBool = false;
}
else if(!prioQueue.poll().equals(next))
{
prioQueueBool = false;
}
}
if(stackBool == true)
{
System.out.println("stack");
}
else if(queueBool == true)
{
System.out.println("queue");
}
else if(prioQueueBool == true)
{
System.out.println("priority queue");
}
else if((stackBool == true && queueBool == true) || (queueBool == true && prioQueueBool == true) || (stackBool == true && prioQueueBool == true))
{
System.out.println("not sure");
}
else
{
System.out.println("impossible");
}
}
//Check EOF
String in;
in = input.nextLine();
in = input.nextLine();
if(in.equals(""))
{
return false;
}
return true;
}
}
But when I run the test-case on the picture above, my program prints this: https://ideone.com/mIO1bs which is wrong. I can't find why it does that, can anyone else here maybe see?
Assuming that your logic setting the boolean flags is correct then this part
if(stackBool == true)
{
System.out.println("stack");
}
...
else if((stackBool == true && queueBool == true) || (queueBool == true && prioQueueBool == true) || (stackBool == true && prioQueueBool == true))
{
System.out.println("not sure");
}
will never work as intended because parts of the second condition were already caught by the first condition.
The better suggestion is to come up with a clearer way of representing this. A suggestion that will still probably work is to put your more complicated if statements at the start of the if-else chain.
Disregarding above assumption:
if(!stack.pop().equals(next))
{
stackBool = false;
}
else if(!queue.dequeue().equals(next))
{
queueBool = false;
}
else if(!prioQueue.poll().equals(next))
{
prioQueueBool = false;
}
These should not be elses, they're all completely independent.
I recently had to code up an interpreter for Bitcoin's script language; part of this involved coming up with an algorithm to check that the control flow in a given script made sense (i.e. every OP_IF had a matching OP_ENDIF, every OP_ELSE and OP_ENDIF had a matching OP_IF, etc.).
This is what I came up with:
public class if_else_checker {
public static boolean search(String[] commands, String[] tracker, int if_index) {
boolean seenElse = false;
for (int i = if_index; i < commands.length; i++) {
if (commands[i].equals("OP_ELSE")) {
if (seenElse == true && tracker[i] == null) return false;
if (tracker[i] == null) {
tracker[i] = "OP_ELSE";
seenElse = true;
}
}
else if (commands[i].equals("OP_ENDIF")) {
if (tracker[i] != null && tracker[i].equals("OP_ENDIF"))
{
continue;
}
tracker[i] = "OP_ENDIF";
return true;
}
else if (commands[i].equals("OP_IF")) {
if (tracker[i] != null && tracker[i].equals("OP_IF")) {
continue;
}
tracker[i] = "OP_IF";
if (search(commands, tracker, i + 1) == false) return false;
}
}
return false;
}
public static boolean validate(String[] args)
{
String[] tracker = new String[args.length];
for (int i = 0; i < args.length; i++)
{
if (args[i].equals("OP_IF"))
{
if (tracker[i] == null || !tracker[i].equals("OP_IF"))
{
tracker[i] = "OP_IF";
if (search(args, tracker, i + 1) == false) return false;
}
else continue;
}
else if (args[i].equals("OP_ELSE"))
{
if (tracker[i] == null || !tracker[i].equals("OP_ELSE")) return false;
}
else if (args[i].equals("OP_ENDIF"))
{
if (tracker[i] == null || !tracker[i].equals("OP_ENDIF")) return false;
}
}
return true;
}
public static void main(String[] args)
{
System.out.println(validate(args));
}
}
It works, but I was wondering if there is a way to optimise it/if there is a standard way of doing this? (One optimisation is to have validate() return the index of the OP_ENDIF it finds, rather than a boolean; this would change runtime from quadratic-time to linear).
The best way of solving this is by using a Stack data structure. Every new opening instruction (e.g. OP_IF) is pushed into the stack. When you find a closing instruction (e.g. OP_ENDIF), you pop the top element of the stack and check if it is the corresponding opening instruction for that closing instruction. If so, then it's valid, and you proceed to the next step. In the end, if the stack is empty then the control flow you're checking is correct. Otherwise, it's not.
public static boolean isCompatibleForMultiplcation(final Matrix a, final Matrix b)
{
if (a == null)
{
throw new IllegalArgumentException("a cannot be null");
}
if (b == null)
{
throw new IllegalArgumentException("b cannot be null");
}
if(!(a.getNumberofColumns()== b.getNumberOfRows()))
{
return false;
}
else
{
return true;
}
}
I am getting a 'Conditional Logic can be removed argument in checkstyle for the following method. I cannot seem to figure out why... Can someone give me a pointer?
It is complaining about this part right here :
if(a.getNumberofColumns() != b.getNumberOfRows())
{
return false;
}
else
{
return true;
}
Whenever you see yourself writing code like this you can easily replace it with a single line by just returning the condition from the if statement:
return a.getNumberofColumns() == b.getNumberOfRows();
This statement will return true if the the number of columns for a and rows for b are equal, and false otherwise.