C
Chris
I have a class (code follows below) with a method getValidInput. It
takes an input and checks two things: is the input greater than zero,
and are the digits either 1 or 0. It returns a valid binary number.
However, when i run the code, I have a problem: if the code is given
a valid input the first time, it returns it with no problem. However,
if I give it a bad input and then a good input, it returns a 0.
I'm new to Java, and hopefully it's an easy fix... See anything
obvious? Thanks!
************ BEGINNING OF CLASS **************************
public class getValidInput
{
public static int getBinary()
{
System.out.print("Input a binary number: ");
int justANumber = Util.getInt();
if (justANumber < 0)
{
System.out.println("Input must be >= 0.");
System.out.println("");
justANumber=0;
getValidInput.getBinary();
}
String strBinary="";
strBinary=String.valueOf( justANumber );
for (int i=0; i<=(strBinary.length()-1); i++)
{
char thisChar=strBinary.charAt(i);
int thisCharInt = Integer.parseInt(thisChar+"");
if (thisCharInt > 1)
{
System.out.println("Input must be a Binary number.");
System.out.println("");
justANumber=0;
getValidInput.getBinary();
}
}
return justANumber;
} // METHOD
} // CLASS
****** END OF CLASS *****************
********** CODE FOR Util.java (which is called from my code)
*****************
import java.io.*;
import java.text.*;
import java.util.*;
// Demonstrate Interactive I/O
// Build 3 primitive methods to input a String, int and double.
// getInt() and getDouble() are implemented in terms of getString(),
// since the default stdin read methods input strings
public class Util {
static InputStreamReader reader;
static BufferedReader input;
static public String getString()
{
StringBuffer s = new StringBuffer("");
if(reader == null) {
reader = new InputStreamReader(System.in);
input = new BufferedReader(reader);
}
try {
s.append(input.readLine());
} catch (IOException e) {
System.out.println("ERROR: " + e);
System.exit(1);
}
return s.toString();
}
static public double getDouble()
{
Double num = null;
String s = getString();
StringTokenizer st = new StringTokenizer(s);
try {
num = new Double(
st.hasMoreTokens() ? st.nextToken() : "-1.0");
} catch (Exception e)
{
System.out.println("ERROR: " + e);
System.exit(1);
}
return num.doubleValue();
}
static public int getInt()
{
int num = 0;
String s = getString();
StringTokenizer st = new StringTokenizer(s);
try {
num = Integer.parseInt(
st.hasMoreTokens() ? st.nextToken() : "-1");
} catch (Exception e)
{
System.out.println("ERROR: " + e);
System.exit(1);
}
return num;
}
}
*********************** END ALL **************
*********************** The thing calling my bad code*****
class Solution
{
public static void main(String args[])
{
System.out.println(getValidInput.getBinary());
getValidInput.getBinary();
}
}
*********************** END ALL **************
takes an input and checks two things: is the input greater than zero,
and are the digits either 1 or 0. It returns a valid binary number.
However, when i run the code, I have a problem: if the code is given
a valid input the first time, it returns it with no problem. However,
if I give it a bad input and then a good input, it returns a 0.
I'm new to Java, and hopefully it's an easy fix... See anything
obvious? Thanks!
************ BEGINNING OF CLASS **************************
public class getValidInput
{
public static int getBinary()
{
System.out.print("Input a binary number: ");
int justANumber = Util.getInt();
if (justANumber < 0)
{
System.out.println("Input must be >= 0.");
System.out.println("");
justANumber=0;
getValidInput.getBinary();
}
String strBinary="";
strBinary=String.valueOf( justANumber );
for (int i=0; i<=(strBinary.length()-1); i++)
{
char thisChar=strBinary.charAt(i);
int thisCharInt = Integer.parseInt(thisChar+"");
if (thisCharInt > 1)
{
System.out.println("Input must be a Binary number.");
System.out.println("");
justANumber=0;
getValidInput.getBinary();
}
}
return justANumber;
} // METHOD
} // CLASS
****** END OF CLASS *****************
********** CODE FOR Util.java (which is called from my code)
*****************
import java.io.*;
import java.text.*;
import java.util.*;
// Demonstrate Interactive I/O
// Build 3 primitive methods to input a String, int and double.
// getInt() and getDouble() are implemented in terms of getString(),
// since the default stdin read methods input strings
public class Util {
static InputStreamReader reader;
static BufferedReader input;
static public String getString()
{
StringBuffer s = new StringBuffer("");
if(reader == null) {
reader = new InputStreamReader(System.in);
input = new BufferedReader(reader);
}
try {
s.append(input.readLine());
} catch (IOException e) {
System.out.println("ERROR: " + e);
System.exit(1);
}
return s.toString();
}
static public double getDouble()
{
Double num = null;
String s = getString();
StringTokenizer st = new StringTokenizer(s);
try {
num = new Double(
st.hasMoreTokens() ? st.nextToken() : "-1.0");
} catch (Exception e)
{
System.out.println("ERROR: " + e);
System.exit(1);
}
return num.doubleValue();
}
static public int getInt()
{
int num = 0;
String s = getString();
StringTokenizer st = new StringTokenizer(s);
try {
num = Integer.parseInt(
st.hasMoreTokens() ? st.nextToken() : "-1");
} catch (Exception e)
{
System.out.println("ERROR: " + e);
System.exit(1);
}
return num;
}
}
*********************** END ALL **************
*********************** The thing calling my bad code*****
class Solution
{
public static void main(String args[])
{
System.out.println(getValidInput.getBinary());
getValidInput.getBinary();
}
}
*********************** END ALL **************