Error in simple binary-checking class

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 **************
 
S

Simon Righarts

Chris said:
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!

<snip>

A: format your code neatly .... makes it one heck of a lot easier to read.
B: nitpick: Class names should be nouns, not verbs (getValidInput = verb,
InputValidator = noun).
C: getBinary returns an int ..... but you're totally ignoring that return
value. That's bad (actually, I didn't know Java let you get away with that -
which JDK are you running?).
D: Also, you're calling getBinary recursively (i.e. getBinary is calling
itself) .... also bad. What you should be doing is using a loop, like:

while(until the cows come home){

get input from the user
attempt to parse it
print out the results
}
 
V

VisionSet

C: getBinary returns an int ..... but you're totally ignoring that return
value. That's bad

Why?
There are loads of methods that return a value, and it probably doesn't make
any sense to do anything with it.

eg

Map.remove(Object key) returns the corresponding value.
Do you ever need this? Sometimes, not always.
I think the general rule is if something that may be of use can be returned,
return it, you can always ignore it.
Someone put me straight if I'm talking the proverbial.
(actually, I didn't know Java let you get away with that -
Yep, no problem.

Not looked at the OP question, I'm sure you're right in that case.
 
J

John Smith

K. I'll give it a shot. I'll learn this stuff sooner or later.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top