-2

I am currently taking my first programming class in college and so far have been very good with understanding how to code basic things. This latest assignment has me confused and stumped.

My assignment is to:

  1. Accept numeric input from the user (integer value)
  2. Print out the square and cube of the number entered.
  3. Make sure that the number is > 0.
  4. Repeat the above three times.
  5. If the number entered is <= 0 then end the program, telling the user why.

My issue is that I am unsure how the variables are to be set for this and how exactly to add the loop to repeat the process 3 times.

This is all I have so far and do not know where to go from here, any help would be appreciated. Thank you

import java.io.*;

public class Assignment3 //class name here, same as file name
{
    public Assignment3() throws IOException{ //constructor, place class name here
        // use BufferedReader class to input from the keyboard
        // declare a variable of type BufferedReader
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        //declare variable for input
        String inputString;

        // houseKeeping()
        String yourNumber;
        int number;
        int  totalSquare = 0;
        int totalCube = 0;
        int count;
        int badNumber=0;  
        String squareCube = " Your number squared is" +square +"your number cubed is"+cube;


        System.out.print("Enter a number: ");
        inputString = input.readLine();
        yourNumber = inputString;
    }//end constructor
}

public static void main(String [] args) throws IOException
{
    new Assignment3(); //class constructor name
} 
dguay
  • 1,635
  • 15
  • 24
user5495500
  • 9
  • 1
  • 1
  • 1
  • you don't have to use a loop. just call `new Assignment3()` 3 times – Paul Nikonowicz Oct 27 '15 at 20:41
  • If you want to convert the string `yourNumber` to an integer, so you can square it and cube it, use `Integer.parseInt` http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String) – djhworld Oct 27 '15 at 20:43
  • @PaulNikonowicz And what do you want to do if the user enters a wrong number? – Tom Oct 27 '15 at 20:44

2 Answers2

1

I could just post the answer here, but then that wouldn't help you learn :)

Firstly, consider using a Scanner to get your inputs. Using BufferedReader will read input data as String. It is possible to convert String to int using Integer.parseInt() but it's more convenient to use Scanner.nextInt().

Secondly, about looping:

Since you want to loop 3 times, the following form of loop seems appropriate:

for (int i=0; i<3; i++) {
  //read input, calculate square & cube
}

Inside the loop, you'll want to check for invalid entry (number <= 0), and if so, use break to jump out of the loop early and end the program.

NickJ
  • 9,380
  • 9
  • 51
  • 74
  • Thank you for the reply NickJ, I will spend most of the evening working this and will post my final code here. I know I can do it, my brain just isnt seeing it just yet. – user5495500 Oct 27 '15 at 21:29
0

Sorry for the bad format.. I tried too long to fix it on the site...

    package com.ispecsoft.porkypig;

    import java.io.*;

    public class Assignment3 // class name here, same as file name
    {
        public Assignment3() throws Exception
        {

        }

        public static void DoIt()
        {

            try
            {
                int xIn = 0;
                BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

            for (int x = 0; x < 3; x++)
            {                
                 System.out.print("Enter a number: ");
                 xIn = Integer.parseInt(input.readLine());

                 int iSquare = xIn * xIn;
                 int iCube = iSquare * xIn;

                 System.out.println(x == 0 ? " Your number squared is ("+ iSquare + ") your number cubed is (" + iCube: ") Your number squared is (" + iSquare+ ") your number cubed is (" + iCube + ")");
            }

            }
            catch (NumberFormatException e)
            {
                System.out.print("The character's you entered are not integers.. this application will now close. ");
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                System.out.print("There was an IOException.. this application will now close. ");
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        public static void main(String[] args) throws Exception
        {
        Assignment3.DoIt();
        }
    }
CA Martin
  • 307
  • 2
  • 7
  • Thank you, this was pretty much my thinking as well, we just hadnt actually learned about xIn yet – user5495500 Oct 29 '15 at 17:26
  • xIn is just a variable name! – NickJ Oct 29 '15 at 19:43
  • can you at least vote this up if you liked it? I had someone vote me down when I solved their problem the other day... not sure actually who voted it down, but that doesn't give me much incentive to do this work if "when I fix something for someone.. there's no credit given??" – CA Martin Nov 03 '15 at 05:38