Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please solve the errors in the program coming

0 votes
asked Jun 22 by JAHANVI GIRIYA_042 (230 points)

import java.util.Scanner;

class student{

    int enrollment_number;

    String student_name;

    char gender;

}

class Student_Info

{

    public static void main(String args[])

    {

        system.out.println("Enter student_name");

        

        String name=sc.nextLine();

        

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

        

        int roll=sc.nextInt();

        

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

        int ge=sc.nextLine();

        

        System.out.println("Name - "+name);

        System.out.println("Roll No. - "+roll);

        System.out.println("Class - "+ gender);

    }

}

4 Answers

0 votes
answered Jun 22 by palgovind (140 points)
/******************************************************************************/

import java.util.Scanner;

class student{

    int enrollment_number;

    String student_name;

    char gender;

}

 class Main

{

    public static void main(String args[])

    {  
         
         Scanner sc = new Scanner(System.in);

        System.out.println("Enter student_name");
        String name=sc.nextLine();

        System.out.println("Enter your enrollment_number.");
        int roll=sc.nextInt();

        System.out.println("Enter your gender");
        char ge=sc.next().charAt(0);

        

        System.out.println("Name - "+name);

        System.out.println("Roll No. - "+roll);

        System.out.println("Class - "+ ge);

    }

}
0 votes
answered Jun 23 by Jeet Narayan Chakraborty (140 points)
I have a question, why have you declared extra variables in main function when there are class varibles ?

Scanner class won't help in taking string input, use java.util.*

most importantly you have to make class object to use class.

import java.util.*;

class student{

    int enrollment_number;

    String student_name;

    char gender;

}

public class Student_Info

{

public static void main(String[] args)

{

    Scanner sc=new Scanner(System.in);

    

    student s=new student();

    

    System.out.print("Enter student_name :");

        s.student_name=sc.nextLine();

        

        System.out.print("Enter your enrollment_number :");

        s.enrollment_number=sc.nextInt();

     

        System.out.print("Enter your gender :");

        s.gender=sc.next().charAt(0);

        

        System.out.println();

        

        System.out.println("Name - "+ s.student_name);

        System.out.println("Enrollment Number. - "+ s.enrollment_number);

        System.out.println("Gender - "+ s.gender);

}

}
–1 vote
answered Jun 23 by Peter Minarik (34,450 points)

Your code has plenty of problems (wrong name used, undeclared identifiers, mixed way of reading input, wrong class name used), so I just rewrote everything. Please, read why you shouldn't mix nextLine() and (nextInt() or nextDouble()) here.

import java.util.Scanner;

class Student
{
    private String _name;
    private int _enrollmentNumber;
    private char _gender;
    
    public String getName() { return _name; }
    public int getEnrolmentNumber() { return _enrollmentNumber; }
    public char getGender() { return _gender; }
    
    public Student(String name, int enrolmentNumber, char gender)
    {
        _name = name;
        _enrollmentNumber = enrolmentNumber;
        _gender = gender;
    }
    
    public void print()
    {
        System.out.println("Name: " + _name);
        System.out.println("Enrolment number: " + _enrollmentNumber);
        System.out.println("Gender: " + _gender);
    }
}

class Main
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Student name: ");
        String name = scanner.nextLine();

        System.out.print("Enrollment number: ");
        int number = Integer.valueOf(scanner.nextLine());
        
        System.out.print("Gender (m/f): ");
        char gender = scanner.nextLine().charAt(0);
        
        Student student = new Student(name, number, gender);
        student.print();
    }
}
0 votes
answered Jun 28 by Jack Jup (150 points)

There is a lot of errors showing you didn't try to find them.

Follow is the basic correction.

Please do your self more and more before ask.
 


import java.util.Scanner;

class student{

    int enrollment_number;

    String student_name;

    char gender;

}

public class Student_Info

{

    public static void main(String args[])

    {

        System.out.println("Enter student_name");

        Scanner sc = new Scanner(System.in); //############

        String name=sc.nextLine();

        

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

        

        int roll=sc.nextInt();

        

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

        int gender=sc.nextInt(); //############

        

        System.out.println("Name - "+name);

        System.out.println("Roll No. - "+roll);

        System.out.println("Class - "+ gender);

    }

}


Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...