## A Bit of Java Help?

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

### A Bit of Java Help?

Hello friends,

Today I was struck with the desire to create a program which computes the value of a quadratic(x^2 + 5x + 25 or the like). Unfortunately, I was hampered by my somewhat limited knowledge of Java. Nevertheless, I managed to come up with the following code:

Code: Select all
`class Function {   public static void main(String args[]) throws java.io.IOException {            int x, numco, exp, numco2, constant;      int result;            int loop;      //introductory minutia             System.out.println("Hello. This program will calculate the value of a quadratic.");      System.out.println("An example of a quadratic is: ");      System.out.println("2x^2 + 5x + 25");            //assigning of the values      System.out.println("Please input the value of x.");      x = (int) System.in.read(); //assigns the input to x            System.out.println("Please input the numerical coefficient('2') for the exponential exponent.");      numco = (int) System.in.read(); //assigns the numerical coefficient of the first term            System.out.println("Now, the power of the exponent('^2').");      exp = (int) System.in.read();             System.out.println("The coeficcient of the second term.");      numco2 = (int) System.in.read();            System.out.println("And finally, the constant.");      constant = (int) System.in.read();      //the multiplication      exp = exp - 1; //since it starts from 0, it is neccessary to subtract one         for(loop = 0; loop == exp; loop++) {            result = x * x;            }         /*Calculating the value of the exponent*/            result = result * numco ; //calculating the value of the whole first term, e.g. 2x^2      numco2 = numco2 * x ; //calculating the value of the second term      result = numco2 + result ; //adding the first and second terms      result = result + constant ; //adding the first, second, and third terms      System.out.println("The value of your quadratic is: " + result);   }}`

While I realize this code is a model of imperfect efficiency, that is not my problem. After successfully compiling this program, I am hit with a flaw in the code. The program works as planned until after the input of the value of x. After that, the next three printlns execute. I assume there is a way to manipulate the program to wait for user input after each. If you could so kindly deign to enlighten me, that would be great.

Thanks!

-MrJoe

P.S. Also, if I've forgotten any of and trespassed on the rules, please forgive me.
MrJoe223
New User

Posts: 2
Joined: Wed Feb 27, 2013 1:43 pm
Blog: View Blog (0)

### Re: A Bit of Java Help?

Welcome to HTS Joe,

I hate to annoy people that are new to programming, but there are somethings that you really should fix and then know.
First error: it must always be 'public class Function {' not just 'class Function {'. although Java assumes you're making it public (how else would you use it?), it's still best to always put public there.
Second error: eclipse caught this; you never initialize result. you assume that it will always be initialized when you do 'result = x * x;', but what if the for loop is skipped over? then result have some kind of junk value in it and that might cause some very erroneous results.

Now lets talk about real errors:
first, what's with the input? lets println() and find out:
i added System.out.println(" " + x + " " + numco + " " + exp + " " + numco2 + " " + constant); right before the for loop, lets see what happens
Code: Select all
`Hello. This program will calculate the value of a quadratic.An example of a quadratic is: 2x^2 + 5x + 25Please input the value of x.2Please input the numerical coefficient('2') for the exponential exponent.Now, the power of the exponent('^2').2The coeficcient of the second term.And finally, the constant.2 50 10 50 10 50The value of your quadratic is: 560`

yikes, i didn't enter 50 10 50 10 50? i entered 2 three times... wtf? (on a windows system you might even get something like 50 13 10 50 13)
well, lets read a little bit about the read() method which you are using on System.in (an InputStream)

Code: Select all
`int java.io.InputStream.read() throws IOExceptionreadpublic abstract int read()throws IOExceptionReads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. A subclass must provide an implementation of this method. Returns:   the next byte of data, or -1 if the end of the stream is reached.Throws:   IOException - if an I/O error occurs.`

hmmm. lets see, 'reads the next byte of data'? what's a byte of data?
A byte is an 8 bit value which can often denote 1 character in text. now lets look at the ASCII table and see something interesting; http://www.asciitable.com/index/asciifull.gif . looking up the character with a Dec value of 50, we find the character 2. and what's 10? that's the line feed character.
In short the read() is working properly, its reading the character that you typed as is. unfortunately, you though that somehow you can convert that straight to an int. although the system doesn't complain of it, that is terribly incorrect.

My fix of the program:
Code: Select all
`import java.io.BufferedReader;import java.io.InputStreamReader;public class Function {       public static void main(String []args) throws java.io.IOException {                    int x, numco, exp, numco2, constant;          int result;                    int loop;                    BufferedReader xSTDIN = new BufferedReader(new InputStreamReader(System.in));          //introductory minutia                    System.out.println("Hello. This program will calculate the value of a quadratic.");          System.out.println("An example of a quadratic is: ");          System.out.println("2x^2 + 5x + 25");          //assigning of the values          System.out.println("Please input the value of x.");          x = Integer.parseInt(xSTDIN.readLine()); //assigns the input to x           System.out.println("Please input the numerical coefficient('2') for the exponential exponent.");          numco = Integer.parseInt(xSTDIN.readLine()); //assigns the numerical coefficient of the first term                    System.out.println("Now, the power of the exponent('^2').");          exp = Integer.parseInt(xSTDIN.readLine());                    System.out.println("The coeficcient of the second term.");          numco2 = Integer.parseInt(xSTDIN.readLine());                    System.out.println("And finally, the constant.");          constant = Integer.parseInt(xSTDIN.readLine());          //the multiplication          exp = exp - 1; //since it starts from 0, it is neccessary to subtract one                    result = x;                    for(loop = 0; loop < exp; loop++) {               result = x * x;          }             /*Calculating the value of the exponent*/                result = result * numco ; //calculating the value of the whole first term, e.g. 2x^2          numco2 = numco2 * x ; //calculating the value of the second term          result = numco2 + result ; //adding the first and second terms          result = result + constant ; //adding the first, second, and third terms          System.out.println("The value of your quadratic is: " + result);     }}`

Sorry for being so critical

Contributor

Posts: 686
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)

### Re: A Bit of Java Help?

Thank you for the help! If I didn't want to learn, I wouldn't have come here.

I'll toy around with constructors, and some other things, in making this work. But anyways, thanks!
MrJoe223
New User

Posts: 2
Joined: Wed Feb 27, 2013 1:43 pm
Blog: View Blog (0)

### Re: A Bit of Java Help?

Looking back at this, i made one small mistake in the code that i didn't notice cause i only test ran it twice. it shouldn't be 'result = x * x;' it should be 'result = result * x;'

fixed code:
Code: Select all
`import java.io.BufferedReader;import java.io.InputStreamReader;public class Function {       public static void main(String []args) throws java.io.IOException {                    int x, numco, exp, numco2, constant;          int result;                    int loop;                    BufferedReader xSTDIN = new BufferedReader(new InputStreamReader(System.in));          //introductory minutia                    System.out.println("Hello. This program will calculate the value of a quadratic.");          System.out.println("An example of a quadratic is: ");          System.out.println("2x^2 + 5x + 25");          //assigning of the values          System.out.println("Please input the value of x.");          x = Integer.parseInt(xSTDIN.readLine()); //assigns the input to x           System.out.println("Please input the numerical coefficient('2') for the exponential exponent.");          numco = Integer.parseInt(xSTDIN.readLine()); //assigns the numerical coefficient of the first term                    System.out.println("Now, the power of the exponent('^2').");          exp = Integer.parseInt(xSTDIN.readLine());                    System.out.println("The coeficcient of the second term.");          numco2 = Integer.parseInt(xSTDIN.readLine());                    System.out.println("And finally, the constant.");          constant = Integer.parseInt(xSTDIN.readLine());          //the multiplication          exp = exp - 1; //since it starts from 0, it is neccessary to subtract one                    result = x;                    for(loop = 0; loop < exp; loop++) {               result = result * x;          }             /*Calculating the value of the exponent*/                result = result * numco ; //calculating the value of the whole first term, e.g. 2x^2          numco2 = numco2 * x ; //calculating the value of the second term          result = numco2 + result ; //adding the first and second terms          result = result + constant ; //adding the first, second, and third terms          System.out.println("The value of your quadratic is: " + result);     }}`