Results 1 to 12 of 12
  1. #1
    tomtrom77 is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default 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!

    Java 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 )

    noob: I am missing the logic of my program!  Help please!-javaquestion2.jpg

    I greatly appreciate your time and your insights!

    -Tom
    Last edited by tomtrom77; 02-01-2013 at 05:31 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default 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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JunkerMartin is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default 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)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    JunkerMartin is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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.

    Java 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 View Post
    @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

  7. #7
    JunkerMartin is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Ulrich is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default 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.

  11. #11
    tomtrom77 is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

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

    Quote Originally Posted by Norm View Post
    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

  12. #12
    tomtrom77 is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default 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

Similar Threads

  1. Replies: 2
    Last Post: 09-10-2012, 07:28 AM
  2. how is the logic this program???
    By riris in forum New To Java
    Replies: 4
    Last Post: 03-16-2012, 06:38 PM
  3. Need help on this program logic
    By cotemaria in forum New To Java
    Replies: 1
    Last Post: 11-12-2011, 10:14 PM
  4. need help in merging GUI and logic main program
    By lavender88 in forum NetBeans
    Replies: 2
    Last Post: 04-02-2011, 02:28 PM
  5. Separating program logic and GUI
    By loopingman in forum AWT / Swing
    Replies: 0
    Last Post: 12-08-2008, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •