5 Replies - 251 Views - Last Post: 04 February 2018 - 02:24 PM Rate Topic: -----

#1 3man75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 13-November 17

practice making objects

Posted 03 February 2018 - 11:39 PM

So i'm making a simple programmer to practice making objects. I got it to work when I set my values which are 2 strings and 1 int variable. I slapped in toString() and now I can have my character displayed as an object which is kinda awesome. But when I try to make a created character later in the program via input it just shuts down on me.

import java.util.Scanner;
public class Executable {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		String Charactername = "Javier";
		String Gender = "Male";
		int CharacterAge = 24;
		
		ObjectDepository Character = new ObjectDepository(Charactername,CharacterAge, Gender);
		
		System.out.println(Character); //FROM HERE EVERYTHING WORKS AS NORMAL.
		
		String CreatedCharactername = null;
		String CreatedGender = null;
		int CreatedCharacterAge = 0;
		
		System.out.println("Hello Adventurer! Please give me your name."); //AND HERE ALSO BECAUSE IT DISPLAYS FINE. AFTER 
                                                                                   //THIS POINT NOTHING GOOD HAPPENS.
		
		input.hasNext(CreatedCharactername);
		
		System.out.println("What is your gender brave one?");
		
		input.hasNext(CreatedGender);
		
		System.out.println("Aha! An how many years have you accrued?");
		
		input.hasNextInt(CreatedCharacterAge);
		
		ObjectDepository CreatedCharacter = new ObjectDepository(Charactername,
				CharacterAge, Gender);
		
		System.out.println("An so finally I know all I need to know about you: "
		+ CreatedCharacter);

	}

}



Below is the objects class and constructor in case anyone wants to see it.

private String name;
	private String gender;
	private int age;
	
	public ObjectDepository(String Charactername,int Characterage, String CharacterGender) {
		
		name = Charactername;
		age = Characterage;
		gender = CharacterGender;
	}

	

	@Override
	public String toString() {
		return "Character " + name + " \nAge " + age + " \n" + gender ;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: practice making objects

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6500
  • View blog
  • Posts: 26,286
  • Joined: 12-December 12

Re: practice making objects

Posted 03 February 2018 - 11:44 PM

What are you attempting with line 22?

" i'm making a simple programmer" ?!
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Beginner
  • member icon


Reputation: 10886
  • View blog
  • Posts: 18,581
  • Joined: 19-March 11

Re: practice making objects

Posted 03 February 2018 - 11:46 PM

It's very helpful if you include the error messages that you're seeing, or if you're not seeing any error messages from the compiler, if you tell us what you're seeing that tells you something's wrong.

In this case, I suspect that the problem is here:

input.hasNext(CreatedCharactername);



The hasNext method does not take any parameters. What are you trying to do here?
Was This Post Helpful? 1
  • +
  • -

#4 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 703
  • View blog
  • Posts: 2,877
  • Joined: 13-June 14

Re: practice making objects

Posted 03 February 2018 - 11:59 PM

Also, ObjectDepository doesn't seem like a good name for that class. Why isn't it just called Character?
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,838
  • Joined: 20-September 08

Re: practice making objects

Posted 04 February 2018 - 08:22 AM

Your naming is generally wrong
http://technojeeves....yle-conventions

The whole thing can be much less wordy:

name = input.next();
...
gender = input.next();
...
age = input.nextInt();
GameCharacter c = new GameCharacter(name, gender, age); // Avoid name clash with java.lang.Character

This post has been edited by g00se: 04 February 2018 - 08:34 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6500
  • View blog
  • Posts: 26,286
  • Joined: 12-December 12

Re: practice making objects

Posted 04 February 2018 - 02:24 PM

Do not use a meaningless title, "Why won't this work?", I have changed it for you on this occasion.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1