Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help me make a code

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me make a code

    I am a java beginner and started learning java last week.

    I am trying to make a program whereby when i key in the roll number 'for example; person 1' and the program output the name, age, race and sex.

    I did it the long way but it is not what I want,

    public static void main(String[] args) {

    Person person1 = new Person();
    person1.name = "Miyuki";
    person1.race = "Asian";
    person1.sex = "Female";
    person1.age = 35;

    System.out.println(person1.name + " " + person1.race + " " + person1.sex + " " + person1.age);


    That is not what I am aiming for.

    I want the whole thing to come out as one when I key in person 1.
    "name : Miyuki";
    "race : Asian";
    "sex : Female";
    "age : 35;

    NB: I plan also to add person 2. person 3 and so on.

    should I use grid? strring arrays or what?
    Have no idea.

    Help me

  2. #2
    Junior Member
    Join Date
    Apr 2019
    Posts
    25
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Help me make a code

    Java Collection Map HashMap Hashtable

    Map map = new HashMap();
    map.put("grace", new User("Grace","111111"));
    map.put("renia", new User("Renia","222222"));
    map.put("david", new User("David","333333"));

    en.verejava.com/?id=19722906432439

  3. #3
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Help me make a code

    How about something like this ?

    NewClass
    public class NewClass {
     
        public static void main(String[] args) {
            Person person1 = new Person("Miyuki","Asian","Female",35);
            String a = person1.call();
            System.out.println(a);
        }
    }

    Person
    class Person {
        String name,race,sex;
        int age;
     
        Person(String name, String race, String sex, int age) {
            this.name = name;
            this.race = race;
            this.sex = sex;
            this.age = age;         
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getRace() {
            return race;
        }
     
        public void setRace(String race) {
            this.race = race;
        }
     
        public String getSex() {
            return sex;
        }
     
        public void setSex(String sex) {
            this.sex = sex;
        }
     
        public int getAge() {
            return age;
        }
     
        public void setAge(int age) {
            this.age = age;
        }
     
        public String call(){
            return "name: "+name+"\n"+"race: "+race+"\n"+"sex : "+sex+"\n"+"age : "+age;
        }
    }
    Whatever you are, be a good one

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me make a code

    Please read this: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] How can I make this code concise?
    By Yoko in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:02 AM
  2. Can I make this code better?
    By hrenfrow in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 23rd, 2013, 10:33 AM
  3. can someone please make this code
    By poldz123 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 19th, 2012, 05:50 PM
  4. I NEED HELP, HOW TO MAKE THIS CODE!!!
    By tahsim in forum Member Introductions
    Replies: 1
    Last Post: March 20th, 2012, 09:05 AM
  5. How to make code more efficient?
    By Apocalypse in forum Java Theory & Questions
    Replies: 2
    Last Post: October 21st, 2011, 09:07 AM