0

I would like to know why I get this following error in the terminal:

Users-MBP:~ user$ javac Testing2.java
Testing2.java:18: error: variable result is already defined in method main(String[])
    boolean result = true;
            ^
1 error

When I try to run the following program:

public class Testing2 {

    private static int TwiceRepetition(int[] elems) {

      ///

    }   

    public static void main(String[] args) {

        ///
    }
}

In fact, the desired output is that it prints true if there's a consecutive repetition length two of any element. Otherwise, it prints false.

The error states that variable result is already defined. Yes. But can't you redefine it just like in Python?

For example, in python, I'm allowed to do this (meaning there's no errors that prevent me from redefining the variable result to true if the conditions is satisfied):

def p1(x): 
     result = False
     i = 0
     while i < len(x) - 1:
       if x[i]== x[i+1]:
            result = True
       i = i + 1
     return result

But in java, it doesn't work, why?

  • 2
    Just do `result = true`. Java already knows the type of `result`, you don't have to remind it – Patrick Haugh Jan 09 '17 at 00:41
  • because `Java` is not `Python` - it uses different rules. – furas Jan 09 '17 at 00:42
  • By the way, the pythonic way to go is to use `zip` and list comprehension: `def p1(x): return any(a==b for a,b in zip(x,x[1:]))` – DYZ Jan 09 '17 at 01:02
  • @DYZ I now see the difference. However, there's still a problem with my algorithm I assume. Because I tested it and it keeps returning me false. If you could help out, I would appreciate it. –  Jan 09 '17 at 01:03
  • @AndrewHaiydon Check the answer that I posted. – DYZ Jan 09 '17 at 01:08
  • use `println()` to see what you have in all variables and which part of code is executed - or learn how to use debugger. – furas Jan 09 '17 at 01:14
  • @DYZ FYI: you don't have to ping the question OP manually. He already gets pinged when you leave an answer. – Christian Dean Jan 09 '17 at 01:15
  • `args[s] == args[s + 1]` is not the way to compare strings in Java. – Andreas Jan 09 '17 at 01:15
  • @furas What are you referring to? There is no builtin `println()` function in Python. – Christian Dean Jan 09 '17 at 01:15
  • @leaf but we talking about code in Java, not in Python – furas Jan 09 '17 at 01:18
  • @Andreas This question is not just about comparing strings, it's about breaking the loop on time. So, no, it is not an exact duplicate. – DYZ Jan 09 '17 at 01:18

1 Answers1

0

Break the loop when you find the first repetition:

for (int s = 0; s + 1 < args.length; s++) {
    if (args[s].equals(args[s + 1])) {
        result = true; 
        break;
    }
}

Otherwise, the variable result becomes overwritten again.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • I checked it. It's still not working. It's still returning false when it shouldn't. –  Jan 09 '17 at 01:10
  • 2
    Right. Because you compare strings, you must use `.equals()`, not `==`. – DYZ Jan 09 '17 at 01:15