0

I am creating some project for converting decimals to binary and back. I get: Multiple markers at this line

Syntax error on token ";", Identifier expected
Syntax error on token ".", @ expected after 

on the line involving my first System.out.println() method. Why can this not compile?

package numbers;

import java.util.Scanner;

class DecimalBinaryWork {
    DecimalBinaryWork() {
    }
    String number = "-1";       
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter in type: binary is 0 or decimal which is 10\n");
    number = scan.next();
    if (number != "0" && number != "10") {
        System.out.println("Error: Enter in 0 for binary or 10 for decimal. Try again");
        number = scan.next();
    }
    else if (number == "-1") {
        System.out.println("Enter in some type please");
    }
    else {
        convert(number);
    }

    String convert(String aString) {
        //fill in later
    }
}
ljk
  • 488
  • 1
  • 5
  • 11
Jinzu
  • 1,325
  • 2
  • 10
  • 22
  • side point, you should use .equals for string http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Brandon Ling Feb 13 '17 at 19:06
  • 2
    Your code isn't in a method block. I see a constructor, but it's empty, and then just a bunch of loose code outside any method. – Compass Feb 13 '17 at 19:09
  • none of the scanner creation, logic and so forth not inside a main, or a function – RAZ_Muh_Taz Feb 13 '17 at 19:09
  • remove `}` brace just below method. – jack jay Feb 13 '17 at 19:10
  • The IDE is hinting to you what is wrong later on: **Syntax error, insert "}" to complete Block** This is a hint that you have some extra braces somewhere. (Well, not to mention that the second message is telling you that it is not expecting a semi-colon.) –  Feb 13 '17 at 20:38

4 Answers4

0

You don't have your code in a function. It seems like you were trying to create a constructor but didn't separate the code into a separate function

try:

class DecimalBinaryWork {
    public DecimalBinaryWork() {
    }

    public static void main(String[] args) {
        String number = "-1";
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter in type: binary is 0 or decimal which is 10\n");
        number = scan.next();
        if (number != "0" && number != "10") {
            System.out.println("Error: Enter in 0 for binary or 10 for decimal. Try again");
            number = scan.next();
        }
        else if (number == "-1") {
            System.out.println("Enter in some type please");
        }
        else {
            // convert(number);
        }
    }

    public String convert(String aString) {
        return null;
        // fill in later
    }
}
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
0

you may want to write the logic within a main method.

package com.test;

import java.util.Scanner;

class DecimalBinaryWork {
DecimalBinaryWork() {
}

private static String number = "-1";

public static void main(String[] str) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter in type: binary is 0 or decimal which is 10\n");
    number = scan.next();
    if (number != "0" && number != "10") {
        System.out.println("Error: Enter in 0 for binary or 10 for decimal. Try again");
        number = scan.next();
    } else if (number == "-1") {
        System.out.println("Enter in some type please");
    } else {
        convert(number);
    }
}

private static String convert(String aString) {
    // fill in later
}

}

Sneha
  • 39
  • 6
0

I'd create a separate function for your class that will do your logic. You could take the logic and put inside a main or you could create a method for that object that you call the method for your logic like so:

public void DetermineDecimalBinaryWork() 
{
    String number = "";     
    Scanner scan = new Scanner(System.in);
    //ask for input until you get a valid response
    do {
        System.out.println("Enter in type: binary is 0 or decimal which is 10\n");
        number = scan.nextLine();
        if(!number.equals("0") && !number.equals("10"))
            System.out.println("Error: Invalid Input! Try again");

    } while (!number.equals("0") && !number.equals("10"));

    if (number.equals("-1")) {
        System.out.println("Enter in some type please");
    }
    else {
        convert(number);
    }

}

Example of how to use the function with your object

 public static void main(String[] args) {
     DecimalBinaryWork worker = new DecimalBinaryWork();
     worker.DetermineDecimalBinaryWork();
 }
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0

Your System.out.println call is not in a method. Outside of methods but inside of a class body, all you are allowed to do is declare fields and methods, and the prior line's location outside of a method is interpreted as declaring and initializing a field named scan. The println statement is neither an assignment nor a declaration, so it's a syntax error. Put your code into a method, like main.

nitind
  • 19,089
  • 4
  • 34
  • 43