Here is my program... It works it just doesn't do everything I want it to do... I also would like to convert integers into Binary numbers... I have the method but I don't know how to put it in the main method??? I'm very confused... Plz help!!!
import java.util.*;
public class Binary1
{
public static final Scanner CONSOLE = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Lab 8 written by Steven Jones");
System.out.println("");
String[] noOrYes = new String[] {"no", "yes"};
boolean keepGoing = true;
while (keepGoing)
{
String bitString = promptForBitString("Please enter in a binary number: ");
int[] array = bitStringToBitArray(bitString);
int val = convertBitArrayToInt(array);
System.out.printf("The binary number \'%s\' = %d in decimal.%n", bitString, val);
System.out.println("");
int i = promptForChoice("Do you want to continue?", noOrYes);
if (i == 0)
{
keepGoing = false;
}
}
}
public static int convertBitArrayToInt(int[] array)
{
int sum = 0;
int placeValue = 1;
for (int i=0; i < array.length; ++i)
{
if (array[i] == 1)
{
sum += placeValue;
}
placeValue *= 2;
}
return sum;
}
public static int[] convertIntToBitArray(int value) {
// assume value is a positive integer
//first declare an array of 31 element
int[ ] res = new int[31]; //initially all element has the value of 0
int index =0; //initialize index as 0
while (value == 0){
if (value%2 == 1)
res[index] = 1;
else
res[index] = 0;
value = value/2; //divide the value by 2 to get the next bit
index ++;
}
return res;
}
// when you prompt for a non-negative int, you will do something similar
// e.g.,: public static int promptForNonNegative(String prompt)
// - Will use nextInt() instead of next()
// - Will check (input >= 0) instead of isBitString(input)\
// - 'input' will be an int!
// - The error message will also be different
public static String promptForBitString(String prompt)
{
// Donald Knuth's n-1/2 loop
for (;
{
System.out.print(prompt);
String input = CONSOLE.next();
if (isBitString(input)) {
return input;
}
System.err.printf("\"%s\" contains characters other than 0 or 1. Please re-enter.%n", input);
}
}
// should return true if the String only contains 0s and 1s
public static boolean isBitString(String s)
{
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if (c != '0' && c != '1')
{
return false;
}
}
return true;
}
// Prompts the user with the given string 'prompt' and restricts the user's
// input selection to be only one of the strings in the 'choices' array.
// Returns the index of the choice made by the user.
public static int promptForChoice(String prompt, String[] choices)
{
boolean selectionValid = false;
int selection = -1;
while (!selectionValid)
{
// prompt the user and get their selection
String choicesList = separate(choices, ", ");
System.out.printf("%s [%s]: ", prompt, choicesList);
String choiceVerbatim = CONSOLE.next();
String choice = choiceVerbatim.toUpperCase();
// check the selection to see if it's valid
for (int i=0; i<choices.length; ++i)
{
String candidate = choices[i].toUpperCase();
if (candidate.startsWith(choice))
{
selection = i;
selectionValid = true;
break;
}
}
if (!selectionValid)
{
System.err.printf("\"%s\" is not a valid selection, please try again.%n",
choiceVerbatim);
}
}
return selection;
}
// Returns a string containing all of the given strings (in 'list')
// separated by the given 'separator'.
public static String separate(String[] list, String separator)
{
// accumulation pattern
String result = "";
for (int i=0; i<list.length; ++i)
{
result += list[i];
if (i != list.length - 1)
{
result += separator;
}
}
return result;
}
// converts a bit String into an int array
public static int[] bitStringToBitArray(String bitString)
{
int bLength = bitString.length();
int[] bitArray = new int[bLength];
int bIndex = bLength - 1;
for (int i = 0; i < bLength; i++)
{
char c = bitString.charAt(bIndex);
if (c == '0')
{
bitArray[i] = 0;
}
else if (c == '1')
{
bitArray[i] = 1;
}
else
{
throw new IllegalArgumentException
(bitString + " should only contain 0s and 1s");
}
bIndex--;
}
return bitArray;
}
// converts an int array into a bit String
public static String bitArrayToBitString(int[] bitArray) {
int bLength = bitArray.length;
String bitString = "";
for (int i = 0; i < bLength; i++)
{
if (bitArray[i] == 0 || bitArray[i] == 1)
{
bitString = bitArray[i] + bitString;
}
else
{
throw new IllegalArgumentException
(Arrays.toString(bitArray) + " should only contain 0s and 1s");
}
}
// eliminate leading 0s
int firstOne = bitString.indexOf('1');
if (firstOne == -1)
{
// true if bitString is all 0s
bitString = "0";
}
else
{
bitString = bitString.substring(firstOne, bLength);
}
return bitString;
}
}
Bookmarks