I decided to do a program that will do binary to decimal, binary to hex, and hex to ascii for a project related to a java programming course, which only needs to perform features from chapters 1-6 and 8 of Tony Gaddis's book. The functions work fine as their own main programs out side of this combined effort, so can anyone help me determine why I get the following 41 errrors saying: class, interface, or enum expected as well as any other errors that may show up afterwards because I'm stumped. My flowcharts, which have to be revised after discovering that my previous function were logically incorrect after running them in their own main are attached below as the spec sheet.

 

 

My code is as follows and I hope you don't mind the commented lines of unused code because I'm not sure where I want things and what I want at the moment:

 

import java.util.Scanner;
import java.io.*;
import java.lang.*;


public class BintoDectoHextoAscii
{ 
   public static void main(String[] args)throws IOException
    {
      Scanner input = new Scanner(System.in);
      System.out.println("Enter a binary number: ");
      String binary = input.nextLine(); // store input from user
      
     if (binary == input.nextLine())
     {
      //int i= Integer.parseInt(hex,2);
      //String hexString = Integer.toHexString(i);
      //System.out.println("Hexa decimal: " + hexString);
      //int finaldecimalvalue = binaryToDecimal(hexString);
      int finaldecimalvalue = binaryToDecimal(hexString);
     }

     if (binary != input.nextLine())
     {
      String hexInput; // The variable Bin Input declared as the datatype int to store the Binary value   
      // Create a Scanner object for keyboard input.
      //Scanner keyboard = new Scanner(System.in);
      
      // Get the number of binary files.
      System.out.print("Enter the Hex value: ");
      hexInput = keyboard.nextLine();
   
   
      System.out.println("Original String: "+ hexInput);
       
      //String hexEquivalent = asciiToHex(demoString);
      String hexEquivalent = asciiToHex(hexInput);
      //Hex value of original String
      System.out.println("Hex String: "+ hexEquivalent);
       
      String asciiEquivalent = hexToASCII(hexEquivalent);
      //ASCII value obtained from Hex value
      System.out.println("Ascii String: "+ asciiEquivalent);String finalhexOutput = HextoAsciiConverter(hexEquivalent);
     }
      
      
     if (binary != input.nextLine() && hexInput != keyboard.nextLine())
     {
         BufferedReader binInput = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter the Binary number:");
         String hex = binInput.readLine();
         //String finaldecimalvalue = binaryToDecimal(decimal);
         //long finalhexvalue = BinaryToHexadecimal(num);
         long finalhexvalue = BinaryToHexadecimal(); 
         
     }


   }
}  
      
      
      
      
      
      
   public static String BinaryToHexadecimal(String hex)
   {
      //public static void main(String[] args)throws IOException
      //{
         //BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
         //System.out.println("Enter the Binary number:");
         //String hex = binInput.readLine();
         long num = Long.parseLong(hex);
         long rem;
         while(num > 0)
         {
         rem = num % 10;
         num = num / 10;
         if(rem != 0 && rem != 1)
         {
         System.out.println("This is not a binary number.");
         System.out.println("Please try once again.");
         
         System.exit(0);
         }
         }
         int i= Integer.parseInt(hex,2);
         String hexString = Integer.toHexString(i);
         System.out.println("Hexa decimal: " + hexString);
      //}
      return num.tolong();
   }


  //int i= Integer.parseInt(hex,2);
  //String hexString = Integer.toHexString(i);
  //System.out.println("Hexa decimal: " + hexString);
  //}
//}   
      
   //}      
//} // end BintoDectoHextoAsciil
      
   //public static String HexAsciiConverter(String hextInput)
   //{


      // Get the number of binary files.
      //System.out.print("Enter the Hex value: ");
      //hexInput = keyboard.nextLine();
   
   
      //System.out.println("Original String: "+ hexInput);
       
      //String hexEquivalent = asciiToHex(demoString);
      //String hexEquivalent = asciiToHex(hexInput);
      //Hex value of original String
      //System.out.println("Hex String: "+ hexEquivalent);
       
      //String asciiEquivalent = hexToASCII(hexEquivalent);
      //ASCII value obtained from Hex value
      //System.out.println("Ascii String: "+ asciiEquivalent);
   //} // End function   
   private static String asciiToHex(String asciiValue)
   {
      char[] chars = asciiValue.toCharArray();
      StringBuffer hex = new StringBuffer();
      for (int i = 0; i < chars.length; i++)
      {
         hex.append(Integer.toHexString((int) chars[i]));
      }
      return hex.toString();
   }

   private static String hexToASCII(String hexValue)
   {
      StringBuilder output = new StringBuilder("");
      for (int i = 0; i < hexValue.length(); i += 2)
      {
         String str = hexValue.substring(i, i + 2);
         output.append((char) Integer.parseInt(str, 16));
      }
      return output.toString();
   }




   public static String binaryToDecimal(String binary)
   {
        //Scanner input = new Scanner(System.in);
        //System.out.println("Enter a binary number: ");
        //String binary = input.nextLine(); // store input from user
        int[] powers = new int[16]; // contains powers of 2
        int powersIndex = 0; // keep track of the index
        int decimal = 0; // will contain decimals
        boolean isCorrect = true; // flag if incorrect input


       // populate the powers array with powers of 2
        for(int i = 0; i < powers.length; i++)
        //{
            powers[i] = (int) Math.pow(2, i);
         
        for(int i = binary.length() - 1; i >= 0; i--)
        {
            // if 1 add to decimal to calculate
            if(binary.charAt(i) == '1')
            //{
                decimal = decimal + powers[powersIndex]; // calc the decimal
            //}
            else if(binary.charAt(i) != '0' & binary.charAt(i) != '1')
            {
                isCorrect = false; // flag the wrong input
                break; // break from loop due to wrong input
            } // end else if


            // keeps track of which power we are on
            powersIndex++; // counts from zero up to combat the loop counting down to zero
        } // end for


        if(isCorrect) // print decimal output
        //{
            System.out.println(binary + " converted to base 10 is: " + decimal);
        //}
        else // print incorrect input message
        //{
            System.out.println("Wrong input! It is binary... 0 and 1's like.....!");
        //}
        //} 
        return decimal.toint();
   } // end function
    

 

 

The errors are as follows:

 

 ----jGRASP exec: javac BintoDectoHextoAscii.java


BintoDectoHextoAscii.java:65: error: class, interface, or enum expected
   public static String BinaryToHexadecimal(String hex)
                 ^
BintoDectoHextoAscii.java:73: error: class, interface, or enum expected
         long rem;
         ^
BintoDectoHextoAscii.java:74: error: class, interface, or enum expected
         while(num > 0)
         ^
BintoDectoHextoAscii.java:77: error: class, interface, or enum expected
         num = num / 10;
         ^
BintoDectoHextoAscii.java:78: error: class, interface, or enum expected
         if(rem != 0 && rem != 1)
         ^
BintoDectoHextoAscii.java:81: error: class, interface, or enum expected
         System.out.println("Please try once again.");
         ^
BintoDectoHextoAscii.java:83: error: class, interface, or enum expected
         System.exit(0);
         ^
BintoDectoHextoAscii.java:84: error: class, interface, or enum expected
         }
         ^
BintoDectoHextoAscii.java:87: error: class, interface, or enum expected
         String hexString = Integer.toHexString(i);
         ^
BintoDectoHextoAscii.java:88: error: class, interface, or enum expected
         System.out.println("Hexa decimal: " + hexString);
         ^
BintoDectoHextoAscii.java:90: error: class, interface, or enum expected
      return num.tolong();
      ^
BintoDectoHextoAscii.java:91: error: class, interface, or enum expected
   }
   ^
BintoDectoHextoAscii.java:124: error: class, interface, or enum expected
      StringBuffer hex = new StringBuffer();
      ^
BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
      for (int i = 0; i < chars.length; i++)
      ^
BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
      for (int i = 0; i < chars.length; i++)
                      ^
BintoDectoHextoAscii.java:125: error: class, interface, or enum expected
      for (int i = 0; i < chars.length; i++)
                                        ^
BintoDectoHextoAscii.java:128: error: class, interface, or enum expected
      }
      ^
BintoDectoHextoAscii.java:130: error: class, interface, or enum expected
   }
   ^
BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
      for (int i = 0; i < hexValue.length(); i += 2)
      ^
BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
      for (int i = 0; i < hexValue.length(); i += 2)
                      ^
BintoDectoHextoAscii.java:135: error: class, interface, or enum expected
      for (int i = 0; i < hexValue.length(); i += 2)
                                             ^
BintoDectoHextoAscii.java:138: error: class, interface, or enum expected
         output.append((char) Integer.parseInt(str, 16));
         ^
BintoDectoHextoAscii.java:139: error: class, interface, or enum expected
      }
      ^
BintoDectoHextoAscii.java:141: error: class, interface, or enum expected
   }
   ^
BintoDectoHextoAscii.java:144: error: class, interface, or enum expected
   public static String binaryToDecimal(String binary)
                 ^
BintoDectoHextoAscii.java:150: error: class, interface, or enum expected
        int powersIndex = 0; // keep track of the index
        ^
BintoDectoHextoAscii.java:151: error: class, interface, or enum expected
        int decimal = 0; // will contain decimals
        ^
BintoDectoHextoAscii.java:152: error: class, interface, or enum expected
        boolean isCorrect = true; // flag if incorrect input
        ^
BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
        for(int i = 0; i < powers.length; i++)
        ^
BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
        for(int i = 0; i < powers.length; i++)
                       ^
BintoDectoHextoAscii.java:155: error: class, interface, or enum expected
        for(int i = 0; i < powers.length; i++)
                                          ^
BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
        for(int i = binary.length() - 1; i >= 0; i--)
        ^
BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
        for(int i = binary.length() - 1; i >= 0; i--)
                                         ^
BintoDectoHextoAscii.java:159: error: class, interface, or enum expected
        for(int i = binary.length() - 1; i >= 0; i--)
                                                 ^
BintoDectoHextoAscii.java:166: error: class, interface, or enum expected
            else if(binary.charAt(i) != '0' & binary.charAt(i) != '1')
            ^
BintoDectoHextoAscii.java:169: error: class, interface, or enum expected
                break; // break from loop due to wrong input
                ^
BintoDectoHextoAscii.java:170: error: class, interface, or enum expected
            } // end else if
            ^
BintoDectoHextoAscii.java:174: error: class, interface, or enum expected
        } // end for
        ^
BintoDectoHextoAscii.java:180: error: class, interface, or enum expected
        else // print incorrect input message
        ^
BintoDectoHextoAscii.java:185: error: class, interface, or enum expected
        return decimal.toint();
        ^
BintoDectoHextoAscii.java:186: error: class, interface, or enum expected
   } // end function
   ^
41 errors


 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners