what i am trying to do here. is to let the user type a hexadecimal number

http://en.wikipedia.org/wiki/Hexadecimal

and then display it.

import java.io.*;
class Ex23
{
          public static void main(String[] args)
          {
           Console console=System.console();
           System.out.println("please type a hexadecimal number");
           String input;
           input=console.readLine();
           int number;
           int number=Integer.parseInt(input);
           int count=0;
           char c= input.charAt(0);
           c= console.readline(input);           
           String abc= "abcdef";
           
           
           if(c!=number)
             
                while(c==abc.charAt(count))
                 {
                 number2=count+9;
                 count++;}
                      
                System.out.println(number2);
            
           else
            
                System.out.pringln(number);
           
         
         }
}

it tells me that there is an else with no if.. why is that?

Dani AI

Generated

— the compiler message comes from the way your if is structured. As pointed out, your braces are incomplete, and is right: always use braces. In Java an if without braces controls only the single next statement. If that statement is a while block, the else that follows can end up not being paired with the if, which produces the "else without if" error. A common pitfall:

if (cond)
    doA();
    doB();
else
    doC();

Here doB() runs unconditionally and the else is left dangling.

Other concrete problems in the posted code to fix one-by-one:

  • You declare the same variable twice (int number), which is a compile error.
  • number2 is used without a declaration.
  • Method names and cases matter: use console.readLine(...) (capital L); passing input into readLine like that is wrong if you already read input.
  • System.out.pringln is a typo.
  • Comparing a char to an int is usually a logic error; also indexing a fixed string in a while can throw IndexOutOfBounds if you don't check the length.
  • System.console() can be null when running inside many IDEs; use Scanner as a fallback.

Simplest, robust approach: read the whole string, strip an optional 0x/0X prefix, then parse with the hex radix and catch NumberFormatException. Example:

import java.io.Console;

public class HexToDecimal {
    public static void main(String[] args) {
        Console console = System.console();
        if (console == null) { System.err.println("No console; run from a terminal or use Scanner"); return; }
        String input = console.readLine("Enter hex number: ").trim();
        if (input.startsWith("0x") || input.startsWith("0X")) input = input.substring(2);
        try {
            int value = Integer.parseInt(input, 16);
            System.out.println("Decimal: " + value);
        } catch (NumberFormatException e) {
            System.out.println("Invalid hexadecimal: " + input);
        }
    }
}

If you still get errors after these fixes, post the updated code and the exact compiler message.

Recommended Answers

All 2 Replies

Recheck you code. The braces are not complete.

it tells me that there is an else with no if.. why is that?

Always use braces ({}) with your if,else, while, for ... statements as mentioned by the Sun Java coding conventions and you will be able to spot your error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.