hi, i'm new to java (being studying for a few days now) and i'm stuck on the following program. it's supposed to bring up a screen and ask me to input a number...but it always say there's something wrong with the line userInput = TextIO.getInt(); . can anyone help me out...thanks

here it is;

public class PrintSquare {

                  public static void main(String[] args) {

                     // A program that computes and prints the square
                     // of a number input by the user.

                     int userInput;  // the number input by the user
                     int square;     // the userInput, multiplied by itself
                     
                     System.out.print("Please type a number: ");
                     userInput = TextIO.getInt();
                     square = userInput * userInput;
                     System.out.print("The square of that number is ");
                     System.out.println(square);
                     
                  } // end of main()
                  
                } //end of class PrintSquare

thanks for the reply. i read in the appendix of the book i'm using about adding TextIO, but it doesnt say where i should add it. can you (or anyone) explain what i'm supposed to do step by step.

thanks for your patience

Hi,
i can sugggest that for reading use input from console u may try using BufferedReader from java.io. Like the one below:

public class PrintSquare {

                  public static void main(String[] args) {

                     // A program that computes and prints the square
                     // of a number input by the user.

                     int userInput=0;  // the number input by the user
                     int square;     // the userInput, multiplied by itself
                     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

                     System.out.print("Please type a number: ");
                     //userInput = TextIO.getInt();
                     try{
                     userInput=reader.read();
             		}catch(IOException e){};
                     square = userInput * userInput;
                     System.out.print("The square of that number is ");
                     System.out.println(square);
                     
                  } // end of main()
                  
                } //end of class PrintSquare

Please correct me if i am wrong. Suggestions abt other alternatives are most welcome.

hi, i'm new to java (being studying for a few days now) and i'm stuck on the following program. it's supposed to bring up a screen and ask me to input a number...but it always say there's something wrong with the line userInput = TextIO.getInt(); . can anyone help me out...thanks

here it is;

public class PrintSquare {
 
                  public static void main(String[] args) {
 
                     // A program that computes and prints the square
                     // of a number input by the user.
 
                     int userInput;  // the number input by the user
                     int square;     // the userInput, multiplied by itself
 
                     System.out.print("Please type a number: ");
                     userInput = TextIO.getInt();
                     square = userInput * userInput;
                     System.out.print("The square of that number is ");
                     System.out.println(square);
 
                  } // end of main()
 
                } //end of class PrintSquare

Here is what your looking for:
Highlighted show's your missing information.

import java.util.Scanner;
public class PrintSquare {
                  public static void main(String[] args) {
// creates scanner to obtain input from command window
    Scanner input = new Scanner(System.in); 
                     // A program that computes and prints the square
                     // of a number input by the user.
                     int userInput;  // the number input by the user
                     int square;     // the userInput, multiplied by itself
                     
                     System.out.print("Please type a number: ");
                     userInput = input.nextInt();
                     square = userInput * userInput;
                     System.out.print("The square of that number is ");
                     System.out.println(square);
                     
                  } // end of main()
                  
                } //end of class PrintSquare
This article has been dead for over six months. Start a new discussion instead.