0% found this document useful (0 votes)
34 views3 pages

Import Java - Io. - Public Class Scanner ( - Public

The document reviews a Java code snippet that incorrectly uses a class name conflicting with java.util.Scanner and has logical errors in input handling. It provides corrections, including changing the class name, properly ordering input prompts, and closing resources. The corrected code example demonstrates the proper way to read a user's name and age, ensuring the program functions as intended.

Uploaded by

shaiksarfraj13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Import Java - Io. - Public Class Scanner ( - Public

The document reviews a Java code snippet that incorrectly uses a class name conflicting with java.util.Scanner and has logical errors in input handling. It provides corrections, including changing the class name, properly ordering input prompts, and closing resources. The corrected code example demonstrates the proper way to read a user's name and age, ensuring the program functions as intended.

Uploaded by

shaiksarfraj13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.io.

*;
public class Scanner{
public static void main(String[] args)throws Exception {
System.out.println("Enter your Name");
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(isr);
String s = br.readLine();
int age = Integer.parseInt(s);
System.out.println("Enter your age");

}
}

You have made some changes and are now using BufferedReader for input, but there are
still some logical and structural mistakes in your code. Here’s a detailed review and
corrections:

Problems in Your Code

1. Class Name Conflict

You used public class Scanner, which is still a bad idea because it conflicts with
java.util.Scanner (even if you don't use/import it now, it's still poor practice and may

confuse readers or future code maintenance).

2. Incorrect Input Prompts and Parsing Order

 You prompt to enter your name, but you immediately read that line and try to
parse it as an integer (for age), which doesn't make sense.

o You should first read the name (as a string).

o Then prompt for age, read the age input, and then parse it as an integer.

3. Prompt Before Input


The line System.out.println("Enter your age"); should come before you ask for input for age,
not after parsing the name as age.

4. Unclosed Resources

While not a fatal issue for short-lived programs, closing the BufferedReader is good
practice.

Corrected Code Example

import java.io.*;

public class InputExample {


public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.println("Enter your Name");


String name = br.readLine();

System.out.println("Enter your age");


String ageInput = br.readLine();
int age = Integer.parseInt(ageInput);

System.out.printf("Hey %s, you are %d years old!%n", name, age);

br.close();
}
}

Key Fixes

 Changed class name to InputExample

 First read the name as a string

 Then prompt for age and read age as a string, then convert to int

 Added a final output statement as an example

 Closed the BufferedReader

Summary

 Don't name your class Scanner.

 First read name as string, then prompt for and read age, then parse age as int.

 Prompt before reading input for age.


 Consider closing your BufferedReader at the end.

If you copy-paste the corrected code above, it will work as expected! If you have more
questions or run into any specific errors, feel free to ask!

You might also like