Java Array Problem: Wizard/Elf Character with Random Health and Gold Points

  • Comp Sci
  • Thread starter iamjon.smith
  • Start date
  • Tags
    Array Java
In summary: Here is the final output:Welcome to the Video Game Character ApplicationEnter customer type (Wizard/Elf): ElfYou have chosen: ElfEnter Character Name: JonYour Character's Name is: JonHealth: 10
  • #1
iamjon.smith
117
3
Ok, I have a java app that allows you to choose a class (wizard/elf) and is supposed to give a random amount of health (an array with 10 values) and 25 points of health. When I run the program it gives me the memory location from the array and not the value. I know I am missing something simple, but can't figure it out. Please help!


Code:
  /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Welcome to the Video Game Character Applcation");
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        
        while (!choice.equalsIgnoreCase("n"))
        {
            // get the input from the user
           // System.out.print("Enter customer type (r/c): ");
            String characterType = Validator.getString(sc,
                "Enter customer type (Wizard/Elf):   ");
            //String characterType =  getValidCharacterType(sc, "Enter customer type (Wizard/Elf): "); // TODO code application logic here
            System.out.println("You have chosen: "+ characterType);
            System.out.println("Enter your Character's Name: ");
        
        String[] charhealth = new String[10];
        charhealth[0] = "1";
        charhealth[1] = "2";
        charhealth[2] = "3";
        charhealth[3] = "4";
        charhealth[4] = "5";
        charhealth[5] = "6";
        charhealth[6] = "7";
        charhealth[7] = "8";
        charhealth[8] = "9";
        charhealth[9] = "10";
                
        for (int i = 10; i < charhealth.length; i = i++) {
     System.out.print("Health: " + charhealth[i]);
        }

OUTPUT:

Welcome to the Video Game Character Applcation
Enter customer type (Wizard/Elf): wizard
You have chosen: wizard
Enter your Character's Name:

Character Type: wizard
Health: [Ljava.lang.String;@4c56291a
Gold Amount: 25.0

Continue? (y/n):


PROBLEM IS IN RED. PLEASE ADVISE :D
 
Physics news on Phys.org
  • #2
How about something like this?
Code:
System.out.print("Health: " + charhealth[i].toString());
 
  • #3
OUTPUT with changes:

run:
Welcome to the Video Game Character Applcation
Enter customer type (Wizard/Elf): elf
You have chosen: elf
Enter your Character's Name:

Character Type: elf
Health: [Ljava.lang.String;@4c56291a
Gold Amount: 25.0

Continue? (y/n):

Same output, but still no value??
 
Last edited:
  • #4
Ok, originally it was skipping over the "Enter Character Name" line and going straight through the rest of the program.

I changed the code so that I inserted the values at array declaration:

Code:
 public static void main(String[] args) {
        System.out.println("Welcome to the Video Game Character Application");
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        
        while (!choice.equalsIgnoreCase("n"))
        {
            // get the input from the user
           // System.out.print("Enter customer type (r/c): ");
            String characterType = Validator.getString(sc,
                "Enter customer type (Wizard/Elf):   ");
            //String characterType =  getValidCharacterType(sc, "Enter customer type (Wizard/Elf): "); // TODO code application logic here
            System.out.println("You have chosen: "+ characterType);
            String characterName = Validator.getString(sc,
                "Enter Character Name:   ");
            System.out.println("Your Character's Name is: " + characterName);
        
        String[] charhealth = new String[]{"0","1","2","3","4","5","6","7","8","9","10"}; //Simply inserted the values at declaration
      /**  charhealth[0] = "1";
        charhealth[1] = "2";
        charhealth[2] = "3";
        charhealth[3] = "4";
        charhealth[4] = "5";
        charhealth[5] = "6";
        charhealth[6] = "7";
        charhealth[7] = "8";
        charhealth[8] = "9";
        charhealth[9] = "10"; */
                
        for (int i = 10; i < charhealth.length; i = i++) {
     System.out.print("Health: " + charhealth[i].toString());
        }

Now it asks for the character type, and character name, but then it hangs and repeats over and over without stopping...

New Output:

Welcome to the Video Game Character Application
Enter customer type (Wizard/Elf): Elf
You have chosen: Elf
Enter Character Name: Jon
Your Character's Name is: Jon

Health: 10Health: 10Health: 10Health...Line is too long, switch to wrapped mode...
 
  • #5
Thanks for the pointer Mark44, I got it!
 

What is a simple array in Java?

A simple array in Java is a data structure that stores a fixed number of elements of the same data type in a sequential manner. It is also known as a one-dimensional array.

How do you declare a simple array in Java?

You can declare a simple array in Java by specifying the data type of the elements, followed by square brackets and the name of the array. For example, int[] numbers;

How do you initialize a simple array in Java?

You can initialize a simple array in Java by assigning values to the elements using curly brackets and separating them with commas. For example, int[] numbers = {1, 2, 3, 4, 5};

How do you access elements in a simple array in Java?

You can access elements in a simple array in Java by using the index of the element within square brackets after the name of the array. The index starts from 0, so the first element in the array would be numbers[0].

What are some common problems with simple arrays in Java?

Some common problems with simple arrays in Java include going out of bounds while accessing elements, not initializing the array before using it, and not using the correct data type for the elements in the array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
8K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top