In: Computer Science
Java
You entered 111 Its base 10 equivalent is 7 |
---|
SPECIFIC RESTRICTIONS FOR THIS ACTIVITY
You should develop your own method, do not use Integer.parseInt (String s, int radix)
*********************************************************************************************************************************
This is my code so far, it works but once I ask the user to try another binary. The Binary doesn't come out right
***********************
import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for File I/O classes
public class Binary
{
public static void main(String[] args) throws IOException
{
String userEntry; // To hold the userEntry
String again = "y";
int binary = 0;
int bin = 0;
int decimal = 0;
int i = 0;
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
PrintWriter outputFile = new PrintWriter("outDataFile.txt");
while (again.equalsIgnoreCase("y"))
{
// Get the user's name.
System.out.println("Enter a Binary String: ");
userEntry =
keyboard.nextLine();
System.out.println("\nYou entered the number: " + userEntry
);
outputFile.println("You entered the number: " + userEntry);
//validate
userName
userEntry =
checkUserEntry (userEntry);
binary =
Integer.parseInt(userEntry);
boolean result =
isBinary(binary);
if(result ==
true)
{
bin =
binary;
while(binary
!= 0)
{
decimal +=
(binary%10)*Math.pow(2, i++);
binary = binary
/10;
}
System.out.println("its base 10 equivalent of " + bin +" is " +
decimal );
outputFile.println("its base 10 equivalent of " + bin +" is " +
decimal );
}
else
{
System.out.println("This is not a Binary Number, try again");
}
System.out.println("\n\nWould
you like to try another number? (y/Y). Anything else will
quit");
again = keyboard.nextLine();
}
outputFile.close();
System.out.println("Goodbye");
}//end main
public static boolean isBinary(int binary)
{
int Input = binary;
while (Input !=
0)
{
if (Input % 10 > 1)
{
return
false;
}
Input = Input /
10;
}
return
true;
}
public static String checkUserEntry (String userAnswer)
{
int userAnswerLength = userAnswer.length();
int counter = 0;//to iterate through the string
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
while(userAnswerLength==0)
{
System.out.println("That is not a number >=
0, try again");
userAnswer = keyboard.nextLine();
userAnswerLength = userAnswer.length();
}
while(counter < userAnswerLength)
{
if(!Character.isDigit(userAnswer.charAt(counter)))
{
System.out.println("That is not a number >=
0, try again ");
userAnswer = keyboard.nextLine();
userAnswerLength = userAnswer.length();
counter = 0;
}
else
{
counter++;
}
while(userAnswerLength==0)
{
System.out.println("That is not a number >=
0, try again");
userAnswer =
keyboard.nextLine();
userAnswerLength =
userAnswer.length();
}
}
return userAnswer;
}
}//end class ValidateUserName
SOLUTION:
There were 2 problems in the following code:
if(result == true)
{
bin = binary;
while(binary != 0)
{
decimal += (binary%10)*Math.pow(2, i++);
binary = binary /10;
}
decimal variable should be reset to 0 before starting next binary number. Similarly i should be reset to 0 before starting conversion.
With fix code will be:
if(result == true)
{
bin = binary;
decimal = 0;
i = 0;
while(binary != 0)
{
decimal += (binary%10)*Math.pow(2, i++);
binary = binary /10;
}
Other thing I noticed there is a note given for not to use parseInt method, but your code still uses it.
To avoid parseInt method use, changed the code to use String input only.
Solution is to parse the string character by character and to convert character to its equivalent integer value subtract '0' from character. The result will be the digit.
Changed the isBinary() method to use String input only to validate if the number entered is valid binary input or not.
CODE:
import java.util.Scanner; // Needed for the Scanner class
import java.io.*; // Needed for File I/O classes
public class Binary
{
public static void main(String[] args) throws IOException
{
String userEntry; // To hold the userEntry
String again = "y";
int binary = 0;
int bin = 0;
int decimal = 0;
int i = 0;
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
PrintWriter outputFile = new PrintWriter("outDataFile.txt");
while (again.equalsIgnoreCase("y"))
{
// Get the user's name.
System.out.println("Enter a Binary String: ");
userEntry = keyboard.nextLine();
System.out.println("\nYou entered the number: " + userEntry
);
outputFile.println("You entered the number: " + userEntry);
//validate userName
userEntry = checkUserEntry (userEntry);
boolean result = isBinary(userEntry); // pass the string
only
int length = userEntry.length();
int index = 0;
if(result == true)
{
bin = binary;
decimal = 0; //reset decimal
i = 0; // reset i to 0
index = length - 1;
while(length-- != 0) // loop till length becomes 0
{
decimal += ((userEntry.charAt(index--) - '0') % 10) * Math.pow(2,
i++); // subtract '0' to get integer value
}
System.out.println("its base 10 equivalent of " + userEntry +" is "
+ decimal );
outputFile.println("its base 10 equivalent of " + userEntry +" is "
+ decimal );
}
else
{
System.out.println("This is not a Binary Number, try again");
}
System.out.println("\n\nWould you like to try another number?
(y/Y). Anything else will quit");
again = keyboard.nextLine();
}
outputFile.close();
System.out.println("Goodbye");
}//end main
public static boolean isBinary(String binary)
{
int length = binary.length();
int index = length - 1;
while (length-- != 0) // loop till length becomes 0
{
if ((binary.charAt(index--) - '0') % 10 > 1) // extract
character and subtract '0' to get integer value
{
return false;
}
}
return true;
}
public static String checkUserEntry (String userAnswer)
{
int userAnswerLength = userAnswer.length();
int counter = 0;//to iterate through the string
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
while(userAnswerLength==0)
{
System.out.println("That is not a number >= 0, try
again");
userAnswer = keyboard.nextLine();
userAnswerLength = userAnswer.length();
}
while(counter < userAnswerLength)
{
if(!Character.isDigit(userAnswer.charAt(counter)))
{
System.out.println("That is not a number >= 0, try again
");
userAnswer = keyboard.nextLine();
userAnswerLength = userAnswer.length();
counter = 0;
}
else
{
counter++;
}
while(userAnswerLength==0)
{
System.out.println("That is not a number >= 0, try
again");
userAnswer = keyboard.nextLine();
userAnswerLength = userAnswer.length();
}
}
return userAnswer;
}
}//end class ValidateUserName
OUTPUT:
outDataFile.txt