# noob: I am missing the logic of my program! Help please!

Printable View

• 02-01-2013, 05:29 PM
tomtrom77
noob: I am missing the logic of my program! Help please!
I set out to use my basic knowledge of Data Types, Exception Handling, and Console IO to create a calculator that will calculate single digits. Everything seemed to be working until I added a "while" loop for persistence. Now the program will function properly the first time, but fail in an infinite loop on any subsequent iteration.

I have researched and studied what I have done without any results. Would you be willing to take a look and enlighten me as to how my logic is faulty? PLEASE DON'T FIX THE CODE! I will come up with a fix, hopefully, as a result of being enlightened as to the error of my ways!

Code:

```/*         "Single Digit Calculator" v1.0                 The purpose of this program is to         practice manipulating certain data         types and to practice console IO.                 Error checking of any kind is         not included yet.                 Division is not fully operational. */ class PracticeIO1 {         public static int performOperation( int numberOne, int numberTwo, int mathOperator )         {                 int result = 0;                                 if( mathOperator == 43 ) result = addTwoNumbers( numberOne, numberTwo );                 else if ( mathOperator == 45 ) result = subtractTwoNumbers( numberOne, numberTwo );                 else if ( mathOperator == 42 ) result = multiplyTwoNumbers( numberOne, numberTwo );                 else if ( mathOperator == 47 ) result = divideTwoNumbers( numberOne, numberTwo );                 else { System.out.println( "The calculator cannot perform your operation." ); }                                 return result;         }                 public static int addTwoNumbers( int numberOne, int numberTwo )         {                 int result = ( numberOne - 48 ) + ( numberTwo - 48 );                 return result;         }                 public static int subtractTwoNumbers( int numberOne, int numberTwo )         {                 int result = ( numberOne - 48 ) - ( numberTwo - 48 );                 return result;         }                 public static int multiplyTwoNumbers( int numberOne, int numberTwo )         {                 int result = ( numberOne - 48 ) * ( numberTwo - 48 );                 return result;         }                 public static int divideTwoNumbers( int numberOne, int numberTwo )         {                 int result = ( numberOne - 48 ) / ( numberTwo - 48 );                 return result;         }                 public static void main( String[] args )         {                 int numberOne = 0;                 int numberTwo = 0;                 int mathOperator = 0;                 int result = 0;                                 try                 {                         while( numberOne != 101 /* 101 is ASCI 'e' */ )                         {                                 System.out.println( "\"Single Digit Calculator\" v1.0" );                                 System.out.print( "Enter you problem as: 1+4. Type 'e' to exit. :> " );                                 numberOne = System.in.read();                                 mathOperator = System.in.read();                                 numberTwo = System.in.read();                                 result = performOperation( numberOne, numberTwo, mathOperator );                                 System.out.println( "Your result: " + result + "\n" );                         }                 }                         catch( Exception ex )                         {                                 System.err.println( ex.getMessage() );                         }         } }```
And here is the program running in the console: ( green = running as expected, red = not running as expected )

Attachment 4464

I greatly appreciate your time and your insights!

-Tom
• 02-01-2013, 05:51 PM
KevinWorkman
Re: noob: I am missing the logic of my program! Help please!
What are the values you're reading in? Are they what you think they are? Step through this with a debugger, or at least add some print statements, to test your assumptions.

Hint: How many keystrokes do you make each time you enter a line?
• 02-01-2013, 06:31 PM
JunkerMartin
Re: noob: I am missing the logic of my program! Help please!
Your problem is the length of the input. You issue only three times System.in.read() but you muss the "Carriage Return 0x0d" and "Line Feed 0x0A". So next time you enter the while-clause you get these chars.
You have either to add two additional System.in.read()-lines:
numberOne = System.in.read();
mathOperator = System.in.read();
numberTwo = System.in.read();
int cr = System.in.read();
int lf = System.in.read();

or better read the whole stuff in one go into a buffer:
byte buffer[] = new byte[80];
int read = System.in.read(buffer);
numberOne=buffer[0];
mathOperator=buffer[1];
numberTwo=buffer[2];

This ensures you that you retrieved the whole line.
Ulrich
• 02-01-2013, 06:37 PM
Norm
Re: noob: I am missing the logic of my program! Help please!
@JunkerMartin
Did you compile and execute the code you posted? What values were in the number variables after the assignment statements get the contents of the array? Were they what you expected?

If you didn't test the code, why not? It's better to post tested code and not just anything you think might work.
• 02-01-2013, 07:23 PM
KevinWorkman
Re: noob: I am missing the logic of my program! Help please!
Yeah, JunkerMartin is on the right track, but I wouldn't take what he said as really true. This is one of the reasons we advise against spoonfeeding: most of the time, spoonfed answers are downright wrong.

Instead, I'd recommend you go through this tutorial, particularly the bit about scanning: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
• 02-01-2013, 09:21 PM
JunkerMartin
Re: noob: I am missing the logic of my program! Help please!
Before you got to tell me words you could have tested it yourself - and you would have seen that it works. But especially for you I will post the complete code. And you can add the lines tp verify my explanation.
System.out.println("Number of bytes entered: " + read);
System.out.println("Carriage Return: " + buffer[3]);
System.out.println("Line Feed: " + buffer[4]);
This will work for Windows Systems. At Linux or Unix there is probably only a Line Feed.

Code:

```/*     "Single Digit Calculator" v1.0         The purpose of this program is to     practice manipulating certain data     types and to practice console IO.         Error checking of any kind is     not included yet.         Division is not fully operational. */   public class PracticeIO1 {     public static int performOperation( int numberOne, int numberTwo, int mathOperator )     {         int result = 0;                 if( mathOperator == 43 ) result = addTwoNumbers( numberOne, numberTwo );         else if ( mathOperator == 45 ) result = subtractTwoNumbers( numberOne, numberTwo );         else if ( mathOperator == 42 ) result = multiplyTwoNumbers( numberOne, numberTwo );         else if ( mathOperator == 47 ) result = divideTwoNumbers( numberOne, numberTwo );         else { System.out.println( "The calculator cannot perform your operation." ); }                 return result;     }         public static int addTwoNumbers( int numberOne, int numberTwo )     {         int result = ( numberOne - 48 ) + ( numberTwo - 48 );         return result;     }         public static int subtractTwoNumbers( int numberOne, int numberTwo )     {         int result = ( numberOne - 48 ) - ( numberTwo - 48 );         return result;     }         public static int multiplyTwoNumbers( int numberOne, int numberTwo )     {         int result = ( numberOne - 48 ) * ( numberTwo - 48 );         return result;     }         public static int divideTwoNumbers( int numberOne, int numberTwo )     {         int result = ( numberOne - 48 ) / ( numberTwo - 48 );         return result;     }         public static void main( String[] args )     {         int numberOne = 0;         int numberTwo = 0;         int mathOperator = 0;         int result = 0;         byte buffer[] = new byte[80];         int read = 0;                 try         {             while( numberOne != 101 /* 101 is ASCI 'e' */ )             {                 System.out.println( "\"Single Digit Calculator\" v1.0" );                 System.out.print( "Enter you problem as: 1+4. Type 'e' to exit. :> " );                 read = System.in.read(buffer);                 numberOne = buffer[0];                 mathOperator = buffer[1];                 numberTwo = buffer[2];                 result = performOperation( numberOne, numberTwo, mathOperator );                 System.out.println( "Your result: " + result + "\n" );             }         }             catch( Exception ex )             {                 System.err.println( ex.getMessage() );             }     } }```
Quote:

Originally Posted by Norm
@JunkerMartin
Did you compile and execute the code you posted? What values were in the number variables after the assignment statements get the contents of the array? Were they what you expected?

If you didn't test the code, why not? It's better to post tested code and not just anything you think might work.

Ulrich
• 02-01-2013, 09:36 PM
JunkerMartin
Re: noob: I am missing the logic of my program! Help please!
Please tell me where I was wrong instead of just calling me a fool. I've answered an exact question to a person just starting with programming. This isn't spoonfeeding, I would call it polite.
• 02-01-2013, 09:37 PM
Norm
Re: noob: I am missing the logic of my program! Help please!
Why all the hard coded magic numbers like 48, 43, 101 etc? Why not improve the code by using the char values like: '0' so the code is readable.
• 02-01-2013, 09:47 PM
Norm
Re: noob: I am missing the logic of my program! Help please!
Instead of solving the problem and providing the code, another approach would have been to help the OP debug the code to see what the code was doing and what pressing Enter sent to the read() method. I prefer teaching debugging techniques to providing code.
• 02-01-2013, 10:38 PM
Ulrich
Re: noob: I am missing the logic of my program! Help please!
I normally prefer hints where I can dig deeper and improve my knowledge. But then I ask for it. Sometimes I just need to get my program running, then I prefer a direct answer. It's the one who asks who only can decide what he needs. And what's more: When reading a foreign discussion, I personally like it, when it comes to a solution. Sometimes I only pass by and would not like to be forwarded to another source of wisdom.
KevinWorkman had told him how to debug the program, so he got it all; The result and advice how help himself. He probably didn't even see our discussion any more; at least I saw no more posts from him here.
Anyway, it's ok now. I think, we are not this far from each other.
• 02-02-2013, 12:44 AM
tomtrom77
Re: noob: I am missing the logic of my program! Help please!
Quote:

Originally Posted by Norm
Why all the hard coded magic numbers like 48, 43, 101 etc? Why not improve the code by using the char values like: '0' so the code is readable.

Hi Norm,

I started with declaring int's, then used System.in.read() to take user input into those int's. When I output them to the console, the ASCI values are what I got.

I'm with you: I prefer a decimal representation of an integer. I just hadn't yet gotten as far as correcting that before I broke the program. I figured I try to fix one thing at a time!

-Tom
• 02-02-2013, 12:47 AM
tomtrom77
Re: noob: I am missing the logic of my program! Help please!
Hi All,

I beat my head against the problem for a while this morning, then I had to get to work. I appreciate all of your replies. I will be studying and learning about your inputs and suggestions and will return when I have thoroughly understood my cause and solution.

Thanks again for your time!

-Tom