jeudi 7 janvier 2016

Using an if-else statement to at any point make the program end

I am creating a simple program that takes some input and turns it into an output to .txt file.

I have been trying to use if-else statements to make it so that after it has received a name ;

    //user enters name and then moves to next line
    System.out.println("Enter Your Name");
    gamerName = Scan.nextLine();

it will either move onto the next part (if a name is entered) or break. How and where will i properly add and format these if-else statements? thanks you

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) {

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

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

        //Main data storage arrays
        String[] gameNames = new String[100];
        int[] highScores = 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 name and then moves to next line
        System.out.println("Enter Your Name");
        gamerName = Scan.nextLine();

        //user is given an example of input format 
        System.out.println("FALSE DATA FORMAT WILL CAUSE ERROR - Input Gamer Information " + "Using Format --> Game : Achievement Score : Minutes Played");
        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; // an array of string
        splitUpReport = gamerReport.split(":"); // split the text up on the colon

        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();
            writer.close();

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

        }



    }

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

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

}

Aucun commentaire:

Enregistrer un commentaire