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

Dani AI

Generated

Short note tying the replies together and giving a safe, practical fix.

As pointed out, TextIO is not part of the standard JDK; it is a helper class used in some textbooks (and later located a copy). ’s suggestion to use Scanner is a fine standard alternative. ’s BufferedReader idea is also valid, but reader.read() reads a single character code (not a full line), which will produce wrong results for multi-digit numbers.

A concise, robust BufferedReader pattern (reads a whole line and parses it, with error handling):

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class PrintSquare {
  public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
      System.out.print("Please type a number: ");
      String line = br.readLine();
      int n = Integer.parseInt(line.trim());
      System.out.println("The square of that number is " + (n * n));
    } catch (IOException | NumberFormatException e) {
      System.err.println("Invalid input");
    }
  }
}

Troubleshooting notes when using a helper class like TextIO: compile the helper and the program together (or put the helper on the classpath), and ensure package declarations match—classes in different packages require proper package/import usage. From the command line, javac TextIO.java PrintSquare.java then java PrintSquare.

Reference documentation: Scanner and BufferedReader API docs are useful for input details (Scanner, BufferedReader). A final caution: java.io.Console can return null when running inside some IDEs, so prefer Scanner or BufferedReader there.

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


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.