I have been doing research on this for a while, but I'm either not understand what I'm reading, or I am searching for the wrong thing. The program I am writing is to convert a number from base to base (2-36) without using a regex method, and validating that the value is valid for the original base before converting it to the new base. My program runs; however, the validation logic is wrong and says every that every valid is incorrect.
Here is my validation logic
// Contract: returns true if iValue is valid expression in base
public static boolean isValidInteger(String iValue, int iBase){
iValue.toUpperCase();
while(iBase >= 2 && iBase <= 36){
for(char character : VALID){
for(int index = 0; index < iValue.length(); index++){
if(iValue.charAt(index) != character){
System.out.println("Input value is invalid for chosen base!");
System.out.print("Please re-enter the value to be converted: ");
Scanner sc = new Scanner(System.in);
iValue = sc.nextLine();
}
else
return true;
}
}
} // End of outer if statement
return true;
} // End validation method
I'm honestly at a loss as to where my logic went south. I would greatly appreciate any guidance or feedback on this problem.
Here is the complete code as I have it so far.
import java.math.BigInteger;
import java.util.Scanner;
public class BaseConversionCalculator {
public static char[] VALID =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
private static String iValue = "";
private static int iBase = 0;
private static String fValue = "";
private static int fBase = 0;
public static boolean isValidInteger(String iValue, int iBase) {
iValue.toUpperCase();
while (iBase >= 2 && iBase <= 36) {
for (char character : VALID) {
for (int index = 0; index < iValue.length(); index++) {
if (iValue.charAt(index) != character) {
System.out.println("Input value is invalid for chosen base!");
System.out.print("Please re-enter the value to be converted: ");
Scanner sc = new Scanner(System.in);
iValue = sc.nextLine();
} else
return true;
}
}
} // End of outer if statement
return true;
} // End validation method
/**
* Main method
*/
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
// Greeting
System.out.print("Welcome to the Base Conversion Calculator!\n");
System.out.print("This program will ask you for a value to be converted" +
"and the base (2-36) that the value is in," + "\nthen it will ask for" +
"the desired base (2-36), do the conversion and display the new value.\n");
/*
* Input Section
*/
// User Input
System.out.println("Please enter the value to be converted: ");
String iValue = input.nextLine();
System.out.println("Next, please enter the original base (2-36): ");
int iBase = input.nextInt();
System.out.println("Finally, please enter the desired base (2-36):");
int fBase = input.nextInt();
/*
* Conversion Section
*/
// Validate the user input
isValidInteger(iValue, iBase);
// Convert initial input to BigInteger
BigInteger bValue = new BigInteger(iValue, iBase);
System.out.println(iValue + " is " + bValue + " in decimal.");
}
}