Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java which asks the user to enter a binary #. -It validates t

ID: 3861723 • Letter: W

Question

Write a program in Java which asks the user to enter a binary #.

-It validates the entry as a binary number, only a binary number is allowed.

-the program converts and displays the binary number to its base 10 equivalent

-the user must be asked if they want to continue entering numbers or quit

-keeps a record in a txt file named outDataFile.txt with the history of all numbers entered and the associated results in the following format:

You entered 111 Its base 10 equivalent is 7 You entered 1010101 Its base 10 equivalent is 85

Restrictions: No infinite loops, or break statements to exit loops.

Explanation / Answer

code.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.Scanner;

public class code {

   private static final String FILENAME = "outDataFile.txt";
   public static void main(String[] args) {

       try
       {
           BufferedWriter bw = null;
           FileWriter fw = null;
           fw = new FileWriter(FILENAME);
           bw = new BufferedWriter(fw);
           while(true)
           {
               String n;
                Scanner scanner = new Scanner(System.in);
                System.out.println("Enter Binary number: ");
                n = scanner.next();
                int dec = 0;
                int i = 0;

                int j = n.length() - 1;
                boolean flag = true;
                while(j >= 0)
                {
                   if(n.charAt(j) != '1' && n.charAt(j) != '0')
                   {
                       flag = false;
                   }
                   j--;
                }
                if(flag == true)
                {
                    j = n.length() - 1;
                    while(j >= 0)
                    {
                       int r = (Integer.parseInt(String.valueOf(n.charAt(j))));
                       dec = dec + r*(int)(Math.pow(2,i));
                       i++;
                       j--;
                    }

                   fw.write("You entered " + n + " Its base 10 equivalent is " + dec );
                   fw.write(" ");
               }
               else
               {
                    System.out.println("Not a binary number!");                  
               }
               System.out.println("Enter 1 to continue and 2 to exit");
               int c = scanner.nextInt();
               if(c == 2)
               {
                   fw.close();
                   System.exit(0);
               }
           }


       }
       catch (IOException e)
       {
           e.printStackTrace();
       }

   }
}

Sample input:

Enter Binary number:
11
Enter 1 to continue and 2 to exit
1
Enter Binary number:
111
Enter 1 to continue and 2 to exit
1
Enter Binary number:
1111
Enter 1 to continue and 2 to exit
1
Enter Binary number:
1010
Enter 1 to continue and 2 to exit
1
Enter Binary number:
11111
Enter 1 to continue and 2 to exit
2

Sample outDataFile.txt :

You entered 11 Its base 10 equivalent is 3
You entered 111 Its base 10 equivalent is 7
You entered 1111 Its base 10 equivalent is 15
You entered 1010 Its base 10 equivalent is 10
You entered 11111 Its base 10 equivalent is 31