1

This is the code I have now and it is completely butchered. I am having issues try to allow user input of a String and two doubles to go into 3 parallel arrays and then save to a .txt file. I can not figure out what is wrong could someone please assist me?

public static void addGames(int i, String[] array1, double[] array2, 
        double[] array3, int arrayLength, Scanner keyboard) throws IOException
{
    String newName;
    double newPrice;
    double newRating;

    if(i < arrayLength)
    {
        System.out.println("Please enter another game name: ");
        newName = keyboard.next();
        array1[i] = newName;
        System.out.println("Please enter another game price: ");
        newPrice = keyboard.nextDouble();
        array2[i] = newPrice;
        System.out.println("Please enter another game rating: ");
        newRating = keyboard.nextDouble();
        array3[i] = newRating;
        i++;
    }
    else
    {
        System.out.println("There is no more room to store games: ");
    }

    PrintWriter gamerOut = new PrintWriter("Project1_VideoGames.txt");

    while(i < array1.length)
    {
        gamerOut.write(array1[i]);
        gamerOut.add(array2[i]);
        gamerOut.add(array3[i]);
        i++;
    }
    gamerOut.close();

}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
DarthKiro
  • 31
  • 3

2 Answers2

0
for (int j = 0; j < i; ++j) {
    gamerOut.println(array1[j] + "\t" + array2[j] + "\t" + array3[j]);

No need to say the names are too imaginative for me. Make a

public class Game {
    String name;
    double price;
    double rating;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

Check if this is what you want.

Instead of having 3 arrays, I encapsulated all in a Gameclass.

public class Game {
    private String name;
    private double price;
    private double rating;

    public Game(String name, double price, double rating){
        this.name = name;
        this.price = price;
        this.rating = rating;
    }

    @Override
    public String toString(){
        String ret = "";
        ret = ret + name + " / " + price + " / " + rating;

        return ret;
    }   
}

And this is what I came with for your addGames function. It only takes 1 parameter now: the number of games you want to write in the file.

public static void addGames(int gamesNumber) throws IOException
    {
        int i = 0;
        String newName;
        double newPrice, newRating;
        Scanner keyboard = new Scanner(System.in);
        ArrayList<Game> array = new ArrayList<Game>();

        while(i < gamesNumber)
        {

            System.out.println("Please enter another game name: ");
            newName = keyboard.next();
            System.out.println("Please enter another game price: ");
            newPrice = keyboard.nextDouble();
            System.out.println("Please enter another game rating: ");
            newRating = keyboard.nextDouble();
            System.out.println();

            Game game = new Game(newName, newPrice, newRating);
            array.add(game);

            i++;
        }

        System.out.println("There is no more room to store games. ");

        PrintWriter gamerOut = new PrintWriter("Project1_VideoGames.txt");
        i = 0;

        while(i < array.size())
        {
            gamerOut.println(array.get(i));
            i++;
        }

        gamerOut.close();

        System.out.println("The games have been written in the file");
    }

You probably want to handle some errors while reading the users input or handle exceptions from the FileWriter but I'll leave that to you.

Also, I've changed to PrintWriter#println method instead of PrintWriter#write and override the toString method in the Game class. You may want to change the implementation of that too.

Hope this helped.

Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28