Skip to content
Advertisement

Java “”(” expected” compiler error

I’m a beginner in Java and after a few time of studying this language by books, I decided to write a simple program that “encrypts” a string by replacing numbers with letters and vice versa. It is not finished yet and has some considerable restrictions, but it should work as is.

The code:

import java.util.Scanner;

class Chiffrator {
public static void main (String args[]) {

    Scanner input = new Scanner(System.in); // Initializing keyboard scanner, like arrays (Scanner [name] = new Scanner(System.in)

    String initial_code; // Initializing the string into which the text will be entered
    System.out.println ("Enter initial code: ");
    initial_code = input.next(); // Initializing input process, with syntax [string_name] = [scanner_name].next()

    char letters[] = {a,b,c,d,e,f,g,h,i,j};
    numbers = new numbers[9];
    int i,j,k;


    for (i=0; i=9; i++) {
        numbers[i] = i;
        i++;
    } 


    for (k=0; k=init_code.length(); k++) { // .length() is a method!
        if initial_code.contains(numbers))
            numbers[k] = letters[k];
        else if initial_code.contains(letters))
            letters[k] = numbers [k];
        k++;
    }       


    System.out.println ("Your chiffred code is " + initial_code);
}
}

It doesn’t work. When I try to compile it through Terminal on my Mac, there are 2 errors:

Chiffrator.java:24: error: '(' expected
        if initial_code.contains(numbers))
          ^
Chiffrator.java:26: error: '(' expected
        else if initial_code.contains(letters))
               ^

If I try to insert those brackets, compiler says I have 20 errors in code.

Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                      ^
symbol:   variable a
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                        ^
symbol:   variable b
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                          ^
symbol:   variable c
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                            ^
symbol:   variable d
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                              ^
symbol:   variable e
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                                ^
symbol:   variable f
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                                  ^
symbol:   variable g
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                                    ^
symbol:   variable h
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                                      ^
symbol:   variable i
location: class Chiffrator
Chiffrator.java:12: error: cannot find symbol
    char letters[] = {a,b,c,d,e,f,g,h,i,j};
                                        ^
symbol:   variable j
location: class Chiffrator
Chiffrator.java:13: error: cannot find symbol
    numbers = new numbers[9];
    ^
symbol:   variable numbers
location: class Chiffrator
Chiffrator.java:13: error: cannot find symbol
    numbers = new numbers[9];
                  ^
symbol:   class numbers
location: class Chiffrator
Chiffrator.java:18: error: incompatible types: int cannot be converted to boolean
    for (i=0; i=9; i++) {
               ^
Chiffrator.java:19: error: cannot find symbol
        numbers[i] = i;
        ^
symbol:   variable numbers
location: class Chiffrator
Chiffrator.java:24: error: cannot find symbol
    for (k=0; k=init_code.length(); k++) // .length() is a method!
                ^
symbol:   variable init_code
location: class Chiffrator
Chiffrator.java:24: error: incompatible types: int cannot be converted to boolean
    for (k=0; k=init_code.length(); k++) // .length() is a method!
               ^
Chiffrator.java:25: error: cannot find symbol
        if (cont_let = (initial_code.contains(numbers)))
                                              ^
symbol:   variable numbers
location: class Chiffrator
Chiffrator.java:26: error: cannot find symbol
            numbers[k] = letters[k];
            ^
symbol:   variable numbers
location: class Chiffrator
Chiffrator.java:27: error: incompatible types: char[] cannot be converted to CharSequence
        else if (cont_num = (initial_code.contains(letters)))
                                                   ^
Chiffrator.java:28: error: cannot find symbol
            letters[k] = numbers [k];
                         ^
symbol:   variable numbers
location: class Chiffrator
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
20 errors

Thanks for help!

Advertisement

Answer

Your code has way too many problem’s as listed in other answer, below is a version of code without compilation errors, but I have no idea what you are trying to do in code, And I am pretty sure whatever you are trying, you are not doing it right. Happy Learning!

import java.util.Scanner;

class Chiffrator {
    public static void main(String args[]) {

        Scanner input = new Scanner(System.in); // Initializing keyboard
                                                // scanner, like arrays (Scanner
                                                // [name] = new
                                                // Scanner(System.in)

        String initial_code; // Initializing the string into which the text will
                                // be entered
        System.out.println("Enter initial code: ");
        initial_code = input.next(); // Initializing input process, with syntax
                                        // [string_name] = [scanner_name].next()

        char letters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
        int[] numbers = new int[9];
        int i, j, k;

        for (i = 0; i <= 9; i++) {
            numbers[i] = i;
            i++;
        }

        for (k = 0; k <= initial_code.length(); k++) { // .length() is a method!
            if (initial_code.contains(numbers.toString()))
                numbers[k] = letters[k];
            else if (initial_code.contains(letters.toString()))
                letters[k] = (char) numbers[k];
            k++;
        }

        System.out.println("Your chiffred code is " + initial_code);
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement