Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

How to print out functions from a loop of an array of objects in java

How can I be able to print out the objects that I gave the program in case 2

What I'm trying to implement is case 2 giving me all the info about the stuff I put into case 1

At first I tried to just use the setters and getters but for some reason I was having a NullPointerException when I used the do-while method. So I decided to use the constructor but at the same time it gave me an error when trying to implement case 2. so any help would be appreciated.

import java.util.Scanner;

public class GameProject {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice;
        do{
            GameSale[] game;
            
            System.out.println("Hello! welcome to the menu!");
            System.out.println("Please Choose your option!");
            System.out.println(" 1-Enter a new game's Info 
 2-Check a game's Information 
 3- Check a game's sales 
 0-Exit");
            choice = input.nextInt();
            switch (choice){
            case 0:
                break;
            case 1:
                System.out.println("");
                System.out.print("How many games do you want to add? : ");
                    int size = input.nextInt();
                    game = new GameSale[size];
                    for (int i = 0; i < size; i++) {      
                    System.out.print("Enter Game "+(i+1) + "'s Name!: 
");
                    String tempname = input.next();
                    System.out.print("Enter the ID of the Game : 
");
                    int tempid = input.nextInt();
                    System.out.print("Enter the Game Type : 
");
                    String tempgametype=input.next();
                    System.out.print("Enter the development Company's Name : 
");
                    String tempgamecomp = input.next();  
                    System.out.print("Enter the Release Sale : 
");
                    double temprelsale = input.nextDouble();
                    System.out.print("Enter the total Sales : 
");
                    double temptotsale = input.nextDouble();
                    game[i]=new GameSale(temprelsale,temptotsale,tempid,tempname,tempgamecomp,tempgametype);
                        }break;
         case 2: 
                    for(int i = 0; i < game.length; i++){
                    System.out.println(game[i].toString());
                 }
                }while(choice!=0);
            }
        }

i got a class that has the setters and getters for some of the regular info

public class GameInfo {

    protected int ID;
    protected String GameName;
    protected String DevelopmentCompany;
    protected String GameType;
    
    public GameInfo(int ID, String GameName, String DevelopmentCompany, String GameType) {
        
        this.ID = ID;
        this.GameName = GameName;
        this.DevelopmentCompany = DevelopmentCompany;
        this.GameType = GameType;
    }
        
    public void SetID(int id) {
        this.ID = id;
    }

    public int GetID() {
        System.out.println("The ID is " + ID);
        return ID;
    }

    public void SetName(String Name) {
        this.GameName = Name;
        
    }

    public String GetName() {
        System.out.println("The Name of the Game Is " + GameName);
        return GameName;

    }

    public void SetCompanyName(String CompanyName) {
        this.DevelopmentCompany = CompanyName;
        
    }

    public String GetCompanyName() {
        System.out.println("The Name Of the Development company Is " + DevelopmentCompany);
        return DevelopmentCompany;
    }
    public void SetGameType(String GameType){
        this.GameType=GameType;
        
    }
    public String GetGameType(){
        System.out.println("The Game Type is : "+GameType);
        return GameType;
    }
}

and a super class with the sales and a constructor for the sales

public class GameSale extends GameInfo {
    protected double ReleaseSales;
    protected double TotalSales;

    public GameSale(double ReleaseSales, double TotalSales, int ID, String GameName, String DevelopmentCompany, String GameType) {
        super(ID, GameName, DevelopmentCompany, GameType);
        this.ReleaseSales = ReleaseSales;
        this.TotalSales = TotalSales;
    }

    public void SetReleaseSales(double RelSale){
        this.ReleaseSales=RelSale;
        
    }
     public void SetTotalSales(double totSales){
        this.TotalSales=totSales;
            }
     
     public double GetReleaseSales(){
         System.out.println("The Release Sales Are "+ReleaseSales );
         return ReleaseSales;
     }
     public double GetTotalSales(){
         System.out.println("The Total Sales Are "+TotalSales);
         return TotalSales;

     }
}

Answered by Alex (thanks a lot dude) here's a temporary fix just for debugging so far by initializing the array dimensions outside the do-while loop

main

import java.util.Scanner;

public class GameProject {

  public static void main(String[] args) {

    Scanner input = new Scanner(System. in );
    int choice;
    GameSale[] games = new GameSale[1];

    do {
      System.out.println("Hello! welcome to the menu!");
      System.out.println("Please Choose your option!");
      System.out.println(" 1-Enter a new game's Info 
 2-Check a game's Information 
 3- Check a game's sales 
 0-Exit");
      choice = input.nextInt();
      switch (choice) {
      case 0:
        break;
      case 1:

        for (int i = 0; i < games.length; i++) {
          System.out.print("Enter Game " + (i + 1) + "'s Name!: 
");
          String tempname = input.next();
          System.out.print("Enter the ID of the Game : 
");
          int tempid = input.nextInt();
          System.out.print("Enter the Game Type : 
");
          String tempgametype = input.next();
          System.out.print("Enter the development Company's Name : 
");
          String tempgamecomp = input.next();
          System.out.print("Enter the Release Sale : 
");
          double temprelsale = input.nextDouble();
          System.out.print("Enter the total Sales : 
");
          double temptotsale = input.nextDouble();
          games[i] = new GameSale(temprelsale, temptotsale, tempid, tempname, tempgamecomp, tempgametype);
        }
        break;
      case 2:
        for (int i = 0; i < games.length; i++) {
          System.out.println(games[i].toString());
        }

      }
    } while ( choice != 0 );
  }
}

game info

public class GameInfo {

    protected int ID;
    protected String GameName;
    protected String DevelopmentCompany;
    protected String GameType;
    
    public GameInfo(int ID, String GameName, String DevelopmentCompany, String GameType) {
        
        this.ID = ID;
        this.GameName = GameName;
        this.DevelopmentCompany = DevelopmentCompany;
        this.GameType = GameType;
    }
        
    public void SetID(int id) {
        this.ID = id;
    }

    public int GetID() {
        System.out.println("The ID is " + ID);
        return ID;
    }

    public void SetName(String Name) {
        this.GameName = Name;
        
    }

    public String GetName() {
        System.out.println("The Name of the Game Is " + GameName);
        return GameName;

    }

    public void SetCompanyName(String CompanyName) {
        this.DevelopmentCompany = CompanyName;
        
    }

    public String GetCompanyName() {
        System.out.println("The Name Of the Development company Is " + DevelopmentCompany);
        return DevelopmentCompany;
    }
    public void SetGameType(String GameType){
        this.GameType=GameType;
        
    }
    public String GetGameType(){
        System.out.println("The Game Type is : "+GameType);
        return GameType;
    }
}

game sale

public class GameSale extends GameInfo {
    protected double ReleaseSales;
    protected double TotalSales;

    public GameSale(double ReleaseSales, double TotalSales, int ID, String GameName, String DevelopmentCompany, String GameType) {
        super(ID, GameName, DevelopmentCompany, GameType);
        this.ReleaseSales = ReleaseSales;
        this.TotalSales = TotalSales;
    }

    public void SetReleaseSales(double RelSale){
        this.ReleaseSales=RelSale;
        
    }
     public void SetTotalSales(double totSales){
        this.TotalSales=totSales;
            }
     
     public double GetReleaseSales(){
         System.out.println("The Release Sales Are "+ReleaseSales );
         return ReleaseSales;
     }
     public double GetTotalSales(){
         System.out.println("The Total Sales Are "+TotalSales);
         return TotalSales;

     }

 
     public String toString(){
    return this.ID + " " + this.GameName + " " + this.DevelopmentCompany + " " + this.GameType;
}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you wanted to print a game's info at a given index in case 2:

if (game == null) { // Break if no game exists
    System.out.println("There is no games to show!");
    break;
}
System.out.print("Enter game index to show information: ");
int index = input.nextInt();
// Break if index is not in bounds
if (index < 0 || index > game.length) {
    System.out.println("Incorrect index");
    break;
}

System.out.println("Info of game: " + game[index].toString());

This code should ask for index as input and prints the info of the game and it will print 'There is no games to show!' if there was no any game added yet

Note you must add the toString() method in your GameSale class:

@Override
public String toString() {
    return "ID: " + this.ID + " Name: " + this.GameName + " Development Company: " + 
      this.DevelopmentCompany + " Game Type: " + this.GameType + " Release Sales: " + ReleaseSales + " Total Sales: " + TotalSales;
}

also move the decleration of the game variable out of the scope of the do-while loop (above it) it will look like

GameSale[] game = null; 
do { ...

Also if you want case 2 to show all games info you can replace the index part with a loop that iterates the whole 'game' array and prints every element's toString.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...