简体   繁体   中英

Need guidance with array lists

How would i go around getting my txt to output the following format ! It only outputs the first line i input but would like it look like the following>>

This is an example of how I would like an output to look - http://i.stack.imgur.com/7Lfr0.png

import java.io.FileOutputStream;
import java.io.IOException;
import java.io[enter image description here][1].PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;



public class GAMESCORE {

private static char[] input;

public static void main(String[] args) {



        int[] minutesPlayed = new int [100];
        String gamerName, gamerReport;


        String[] gameNames = new String[100];
        int[] highScores = new int[100];

        @SuppressWarnings("resource")
        Scanner Scan = new Scanner(System.in);


        System.out.println("-------------- Game Score Report Generator --------------");
        System.out.println("     ");


        System.out.println("Enter Your Name");
        gamerName = Scan.nextLine();
        boolean isEmpty = gamerName == null || gamerName.trim().length() == 0;

        if (isEmpty) {

            System.out.print("Enter your Name.");

            gamerName = Scan.nextLine();
        }



        System.out.println("Enter details in this format - " + " -->");
        System.out.println("    ");



        System.out.println("Game : Achievement Score : Minutes Played");
        gamerReport = Scan.nextLine();
        Scanner scanner = new Scanner(System.in);     

        List<String> al = new ArrayList<String>();    
        String word;                                  
        while (scanner.hasNextLine()) {               
          word = scanner.nextLine();                  
          if (word != null) {                        
            word = word.trim();                      
            if (word.equalsIgnoreCase("quit")) {      
              break;                                  
            }
            al.add(word);                             
          } else {
            break;                                    
}
        }


        String[] splitUpReport; 
        splitUpReport = gamerReport.split(":"); 

        int i = 0;


        gameNames[i] = splitUpReport[0];
        highScores[i] = Integer.parseInt(splitUpReport[1].trim() );
        minutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());




        try
       {

           PrintWriter writer = new PrintWriter(new FileOutputStream("Gaming Report Data.txt", true));
           writer.println("Player : " + gamerName);
           writer.println();
           writer.println("--------------------------------");
           writer.println();
           String[] report = gamerReport.split(":");
           writer.println("Game: " + report[0] + ", score= " +report[1] + ", minutes played= " +report[2]);
           //writer.println("Games Played : " + minutesPlayed);
           writer.close();





       } catch (IOException e)
       {
           System.err.println("You have made an error with data input");
       }



System.out.println("You have quit!");
           }





public static char[] getInput() {
    return input;
}

public static void setInput(char[] input) {
    GAMESCORE.input = input;
}

}

Well the variable "gamerReport" is set for the first line, and is never touched again other than to print it, and the list "al" has stuff added to it, but is never used. Maybe try something like this?

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;



public class GAMESCORE {

private static char[] input;

public static void main(String[] args) {

    int totalGames, totalAchievements, totalTime;

    totalGames = 0;
    totalAchievements = 0;
    totalTime = 0;

    String gamerName, gamerReport;

    @SuppressWarnings("resource")
    Scanner Scan = new Scanner(System.in);


    System.out.println("-------------- Game Score Report Generator --------------");
    System.out.println("     ");


    System.out.println("Enter Your Name");
    gamerName = Scan.nextLine();
    boolean isEmpty = gamerName == null || gamerName.trim().length() == 0;

    if (isEmpty) {

        System.out.print("Enter your Name.");

        gamerName = Scan.nextLine();
    }



    System.out.println("Enter details in this format - " + " -->");
    System.out.println("    ");



    System.out.println("Game : Achievement Score : Minutes Played");
    gamerReport = Scan.nextLine();
    Scanner scanner = new Scanner(System.in);

    List<String> al = new ArrayList<String>();
    String word;
    while (scanner.hasNextLine()) {
        word = scanner.nextLine();
        if (word != null) {
            word = word.trim();
            if (word.equalsIgnoreCase("quit")) {
                break;
            }
            al.add(word);
        } else {
            break;
        }
    }




    try
    {

        PrintWriter writer = new PrintWriter(new FileOutputStream("Gaming Report Data.txt", true));

        writer.println("Player : " + gamerName);
        writer.println();
        writer.println("--------------------------------");
        writer.println();
        for(String listString : al){
            String[] splitUpReport;
            splitUpReport = listString.split(":");

            writer.println("Game: " + splitUpReport[0].trim() + ", score= " + splitUpReport[1].trim() + ", minutes played= " +splitUpReport[2].trim());

            totalGames++;
            totalTime += Integer.parseInt(splitUpReport[2].trim());
            totalAchievements += Integer.parseInt(splitUpReport[1].trim());
        }

        writer.println();
        writer.println("--------------------------------");
        writer.println();
        writer.println("Games Played: " + String.valueOf(totalGames));
        writer.println("Total Achievement: " + String.valueOf(totalAchievements));
        writer.println("Total Time: " + String.valueOf(totalTime) + " (" + String.valueOf(totalTime/60) + " hours and " + String.valueOf(totalTime%60) + " minutes)");
        //writer.println("Games Played : " + minutesPlayed);
        writer.close();





    } catch (IOException e)
    {
        System.err.println("You have made an error with data input");
    }



    System.out.println("You have quit!");
}





public static char[] getInput() {
    return input;
}

public static void setInput(char[] input) {
    GAMESCORE.input = input;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM