dimanche 10 janvier 2016

An array of string causing my program not to continue to run after an if-else statement - java

I am creating a simple program that gets an input from a user and outputs to .txt file. I have made some changes to my code so that whenever a user enters a variation of 'quit', 'Quit', or 'QUIT' it will break, else it will continue as normal. However when I run and enter data as normal it is not continuing as normal and stopping. Also, how would I display a printline so that when a user does type 'Quit' it would say for example

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

My Code

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.nio.file.*;



public class JavaProject {

    private static char[] input;

    public static void main(String[] args) {


        //variables 
        int hrs, mins;  
        String gamerName, gamerReport;
        int gameCount;
        int errorCount;

        //Main data storage arrays
        String[] gameNames = new String[100];
        int[] highScores = new int[100];
        int[] minutesPlayed = new int [100];

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

        //formatting for output and input
        System.out.println("////// Game Score Report Generator \\\\\\\\\\\\");
        System.out.println("     ");

        //User enters either their name or quit. Entering a name will move on to next part
        for ( ; ; )
        {
            System.out.print("Enter your Name. If you do not wish to proceed, enter 'Quit' to quit.");
            System.out.println("   ");
            gamerName = Scan.nextLine();
                if ("Quit".equals(gamerName))
                {
                    break;
                }

            for(int b = 1; b < 100; b++ ) { //this is making the code loop 100 times




        //user is given an example of input format 
        System.out.println("Input Gamer Information " + "Using Format --> Game : Achievement Score : Minutes Played");
        System.out.println("FALSE DATA FORMAT WILL CAUSE ERROR");
        System.out.println("    ");

        //another data input guide which is just above where data input is in console
        System.out.println("Game : Achievement Score : Minutes Played");
        gamerReport = Scan.nextLine();

        String[] splitUpReport = null; // an array of string
        //splitUpReport = gamerReport.split(":"); // split the text up on the colon

        //gets input, if "quit" then break
        for (int count = 0; count <= 100; count++){
            gamerReport = Scan.nextLine();

            if (gamerReport.equals("quit"))
            {
                break;
            }
            if (gamerReport.equals("Quit"))
            {
                break;
            }
            if (gamerReport.equals("QUIT"))
            {
                break;
            } 
            else  
            {
                splitUpReport = gamerReport.split(":");
            }
            System.out.println("You have quit!");
        }

        int i = 0;

        //copy data from split text into main data storage arrays 
        gameNames[i] = splitUpReport[0];
        highScores[i] = Integer.parseInt(splitUpReport[1].trim() );
        minutesPlayed[i] = Integer.parseInt(splitUpReport[2].trim());




        //output to file using a PrintWriter using a FileOutPutStream with append set to true within the printwriter constructor
        //

        try
        {

            PrintWriter writer = new PrintWriter(new FileOutputStream("Gaming Report Data", 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: " + splitUpReport);
            //writer.println("Total Achievement: " + highScores);
            //writer.println("Total Time: " + minutesPlayedS%60);
            writer.println();
            writer.close();

        } catch (IOException e)
        {
            System.err.println("File does not exist!");
        }

        }

        }
        System.out.println("Goodbye!");

    }

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

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

}

my code issue

//gets input, if "quit" then break
            for (int count = 0; count <= 100; count++){
                gamerReport = Scan.nextLine();

                if (gamerReport.equals("quit"))
                {
                    break;
                }
                if (gamerReport.equals("Quit"))
                {
                    break;
                }
                if (gamerReport.equals("QUIT"))
                {
                    break;
                } 
                else  
                {
                    splitUpReport = gamerReport.split(":");
                }
                System.out.println("You have quit!");
            }

Aucun commentaire:

Enregistrer un commentaire