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

Recommended Answers

All 5 Replies

You're missing a class "TextIO" and the import statement for it.

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.

i've figured it out. i found a site where i can copy the missing TextIO class. i saved it in the same folder as all my java programs.

here's the site incase anyone needs it

http://math.hws.edu/javanotes/source/TextIO.java
Thanks for the help

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.