Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:13:49+00:00 2026-05-26T13:13:49+00:00

I am learning java and started with classes and I am now having some

  • 0

I am learning java and started with classes and I am now having some doubts.

I’m going to make a program with data from students, classes and chairs. For example information from the student data will be used in classrooms and chairs. For now, I will restrict myself to the class student.

Each student will have a name and birth date.
I thought I’d create a class with student data and then save the data of students in two arrays. One-dimensional array of strings for the names and an array with three columns for the dates (year, month, day).

I started by creating the Student class with the code:

    public class Stundent{
        private String nameStudent;
        private int yearBirth;
        private int monthBirth;
        private int dayBirth;

        public void setName(String name){
            System.out.println("Insert student's name:");
            nameStudent = name;

        }
        public String getName(){
            return nameStudent;
        }
        public void setYear(int year){
            System.out.println("Insert student's year of birth:");
            yearBirth = year;
        }
        public int getYear(){
            return yearBirth;
        }
        public void setMonth(int month){
            System.out.println("Insert student's month of birth:");
            monthBirth = month;
        }
        public int getMonth(){
            return monthBirth;
        }
        public void setDay(int day){
            System.out.println("Insert student's day of birth:");
            dayBirth = day;
        }
        public int getDay(){
            return dayBirth;
        }
    }

Then in the main file had thought this way:

            Student person = new Student();
            String[] nameStudents = new String[100];
            int[][] birthdayStudents = new int[100][3];
            for (int i = 0; i < 3; i++){

                person.setName(sc.nextLine());
                nameStudents[i] = person.getName();

                person.setAno(sc.nextInt());
                birthdayStudents[i][1] = person.getAno();

                person.setMes(sc.nextInt());
                birthdayStudents[i][1] = person.getMes();

                person.setDia(sc.nextInt());
                birthdayStudents[i][2] = person.getDia();

            }

First question:

Forgetting that this not working properly because the scanner, is this the correct way of thinking?

Second question:

If I run this code I have the following situation:
It will be presented to me “Insert student’s name:” but if I place Maria InputMismatchException will appear. I believe he is storing Maria in the year.
Why does this happen? In the same for cycle I can’t have more than one call to the scanner?
If I only put in the code:

person.setName(sc.nextLine());
nameStudents[i] = person.getName();

and remove everything else concerning the birthday it will work OK.
Can anyone enlighten me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:13:49+00:00Added an answer on May 26, 2026 at 1:13 pm

    I think it is not the right way you do it. You mix up the entity Student with input related information. I’d redesign it as follows (a slightly reduced Student):

    /* just an entity, no logic at all */
    public class Student {
        private String name = null;
        private Integer year = null;
    
        public void setName(String name){
            this.name = name;
        }
    
        public void setYear(Integer year){
            this.year = year;
        }
    }
    

    And the logic to obtain user input:

    List<Student> students = new ArrayList<Student>();
    int numberOfStudents = 3;
    
    for (int i = 0; i < numberOfStudents; i++){
        System.out.println("Please enter name:");
        String name = sc.nextLine();
    
        System.out.println("Please enter year:");
        Integer year = sc.nextInt();
    
        /* after gathering the input create student */
        Student s = new Student();
        s.setName(name);
        s.setYear(year);
    
        students.add(s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning C++ (coming from Java ) and am having some serious
Recently, I started learning some Java. From what I've already learned about JVM it
When I was learning Java coming from a background of some 20 years of
I started learning Java and how to program for Android last night :) So
I just started learning Ruby coming from Java. In Java you would use packages
I'm learning Java and just started playing around with mysql and am having a
Java script has many falsy values as I started learning. I have a program
I have recently started learning Java EE 6 and I could need some help.
I've recently started learning and exploring the basics of GUI programming in Java. Having
I started learning Java. When would I use a HashMap over a TreeMap?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.