Reputation: 11
I was trying to compile a Java program, but it was generating an error.
Can someone tell me where I have made an error and how to fix it?
import java.io.*;
import java.util.*;
class detail
{
String name;
int age;
float salary;
void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter name: ");
name=new sc.next();
System.out.println("enter age: ");
age=new sc.nextInt();
System.out.println("enter salary: ");
salary=new sc.nextFloat();
}
void display()
{
System.out.println("name: "+name);
System.out.println("age: "+age);
System.out.println("salary: "+salary);
}
}
class person
{
public static void main(String arr[])
{
detail p=new detail();
p.getdata();
p.display();
}
}
Upvotes: 0
Views: 5433
Reputation: 16
Just remove the new from below lines and it will work
name=new sc.next();
System.out.println("enter age: ");
age=new sc.nextInt();
System.out.println("enter salary: ");
salary=new sc.nextFloat();
Upvotes: 0
Reputation: 116179
The issue is on lines like name=new sc.next();
. You already used new
to create an instance of Scanner
called sc
. You just need to call the instance methods on it:
import java.io.*;
import java.util.*;
class detail {
String name;
int age;
float salary;
void getdata() {
Scanner sc = new Scanner(System.in);
System.out.println("enter name: ");
name = sc.next();
System.out.println("enter age: ");
age = sc.nextInt();
System.out.println("enter salary: ");
salary = sc.nextFloat();
}
void display() {
System.out.println("name: " + name);
System.out.println("age: " + age);
System.out.println("salary: " + salary);
}
}
class person {
public static void main(String arr[]) {
detail p = new detail();
p.getdata();
p.display();
}
}
You do have a few other issues with your code, though, that are mainly stylistic:
// Java Code Conventions: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
// Removed import java.io.* because it wasn't used
import java.util.Scanner; // I prefer explicit imports, not *
class Detail { // Class names are typically uppercase
// You don't have any visibility modifiers
// https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
private String name;
private int age;
private float salary;
// I would recommend a different name. Methods of the format
// getX() usually indicate that they return a value. These are
// called accessor methods
public void getData() { // Method names are usually camel case
Scanner sc = new Scanner(System.in);
System.out.println("enter name: ");
name = sc.next();
System.out.println("enter age: ");
age = sc.nextInt();
System.out.println("enter salary: ");
salary = sc.nextFloat();
// You should close input methods
sc.close();
}
public void display() {
System.out.println("name: " + name);
System.out.println("age: " + age);
System.out.println("salary: " + salary);
}
}
class Person { // Classes are usually uppercase names
public static void main(String arr[]) {
Detail p = new Detail();
p.getData();
p.display();
}
}
Upvotes: 0
Reputation: 4465
You dont need to call new sc.nextInt()
. You use "new" to create new objects. Your object Scanner
is created by the variable sc
, so you already can use their methods.
Like that:
age = sc.nextInt();
Upvotes: 1
Reputation: 82461
You have 3 lines that contain
new sc
Remove new
since sc
is not a class, but a variable and you already created the Scanner
object in this line:
Scanner sc=new Scanner(System.in);
The new
keyword is only allowed, if a constructor call follows or in java 8 in method references, which is not the case, if you type new sc
.
Upvotes: 2