#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    Test string if binary or not?


    Here's what the program needs to do:
    Write a Java program that can take as input binary and hexadecimal numbers stored in a file, and output (to the screen) their equivalent values in all 3 bases (binary, hexadecimal and decimal). The program should read each input value as a String, then evaluate the String to determine if it contains only 1s and 0s (which means it is binary) or other characters (which means it is hexadecimal). Once the nature of the String is determined, the program should compute the equivalent values in the two other bases and print out a table of values
    I can not use the wrapper classes (Integer, Double, Character, etc.)

    I need help on testing the input from file, and figuring out if its binary or not.

    Heres my code:
    Code:
    import java.io.*; 
    import java.util.*; 
    public class InAndOut
    {
        public static void main (String [] args) 
        {
            boolean fileNotOpen = false;
            Scanner kb = new Scanner(System.in);
            String input;       
            Scanner inFile;     
            PrintWriter outFile;
            FileInputStream in = null;
            
            while (!fileNotOpen)
            {
                System.out.println("Enter full path name of input file below:");
                input = kb.nextLine();
                try
                {
                    in = new FileInputStream(input);
                    fileNotOpen = true;
                }
                catch (FileNotFoundException e)
                {
                    System.out.println(input + " is not valid.  Please try again.");
                    System.out.println("Could not open file; program exiting");
                    System.exit(1);
                }
            }
    
            inFile = new Scanner(in);
            
           
            String binary = inFile.next();
     
           
                for (int x=0; x<25; x++)
                {
                    char value;
                    int a = 0;
                while( a < binary.length()) 
                {
                    value = binary.charAt(a);
                    if (value == '1' || value == '0')
                    {
                        a++; 
                    }
                    else
                    {
                        a--;
                        break;
                    }
    
                }
                if(a==binary.length())
                    System.out.println("You Entered A Binary Number");
                else
                {
                    System.out.println("Not Binary Number");
                }       
            }
            
            inFile.close();
            
        }
    }
    My out put reads this:

    Enter full path name of input file below:
    file1
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Not Binary Number
    Last edited by blyons626; April 15th, 2013 at 03:56 PM. Reason: Updated original code
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    need help on testing each input from a file of 25 unknown binary
    Not sure what the problem is. Use an editor to create a file and add to it 25 different Strings with different combinations of digits.

    Please edit your post and wrap the code in code tags:
    [code]
    >>>>>>>>>>CODE HERE<<<<<<<<
    [/code]

IMN logo majestic logo threadwatch logo seochat tools logo