My code is not compile and run Code is Below

import java.util.Scanner;
class Input
{
    public static void main(String arg[])
    {
        int rno;
        System.out.print("Enter Your Name = ");
        System.in.nextInt(rno);
    }
}

i think if System.out.println() is run then upper code should have to run but not run please solve this problem

Recommended Answers

All 10 Replies

Line 8 is wrong. There is no nextInt method in System.in
You must create a Scanner using System.in (just like your previous post)
Then you can use the Scanner's nextInt method to read an int value that you can assign to rno

ps: If you have the answer for your previous post then please mark it "solved"

also: you can't use rno as a parameter since it has no value. local variables don't get default values.

stultuske ok i agree with you that we can not use rno as a parameter then is this Correct

rno=System.in.nextInt();

no. did you read James' reply?
you need to pass System.in as parameter to the constructor of scanner, and use the instance you created to call nextInt();

or nextInt(rno), after you have initialized rno.

But This code is run properly

import java.io.PrintStream;
class Check{

    public static void main(String arg[]){

    PrintStream ob=new PrintStream(System.out);
    ob.print("Hello World");
    }
}

And upper code is same as

System.out.print(Hello World);

and Scanner is working with code below

        Scanner input=new Scanner(System.in);
        System.out.print("Enter Your Name = ");
        n=input.nextInt();

Scanner is not working with this code

System.out.print("Enter Your Name = ");
n=System.in.nextInt();

Whyyyyyyyyy
it is same as

System.out.print(""Hello World);

whyyyyyyyyyyyyyyyyyyyyyyy

actually my english is very bad but i am trying my best to explain my Question i hope you understand my question

Because there isn't any System.in.nextInt() method; just System.in (stream) --that is sent into--> Scanner --that is asked by--> .nextInt()

n=input.nextInt();
works because input is a Scanner

n=System.in.nextInt();
does not work because System.in is NOT a Scanner

you really don't see the difference between:

Scanner input=new Scanner(System.in);
n=input.nextInt();

and

n=System.in.nextInt();

in the first you have an instance of Scanner, in the second, you don't.

also:

PrintStream ob=new PrintStream(System.out);
ob.print("Hello World");

is not the same as

System.out.print(Hello World);

it's behaviour is just the same in this case.

PrintStream ob=new PrintStream(System.out);
ob.print("Hello World");

Good point stultuske

System.out is a PrintStream, so new PrintStream(System.out) just creates another PrintStream object which is just the same oas the original. SO there's no point doing that.

ok thanks to allllllllllllllllllll

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.