Need help with game input

Need help with game input

Page 1 of 1

4 Replies - 201 Views - Last Post: 25 September 2018 - 04:36 AM Rate Topic: -----

#1 fearless_swami   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-September 18

Need help with game input

Posted 24 September 2018 - 07:48 PM

The objective of this project is to take in a user input for a GoT character and have an output of all the battles the character appears in. The data is stored in a csv file. The main trouble I am having is finding a way to manage my user input with my Character object. For some reason, when the user types in a name that is in the file information is not printed and the user is prompted again. I will post my code below. The main error is occurring in the GameOfThrones class with the getUserInput() and readCharacter() functions. Thank You.
package project1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class GameOfThrones {
	//create a main method
	/*
	 * main method should include the user input with the flag
	 * create instances of Battles and Character objects
	 * pass in data values into objects (battle and character)
	 *  Implement a way to find the character that the user is searching for --> split(",")[0]
	 *  Remember to ignore the case of userInput
	 * use e.equalsIgnoreCase(input) for checking
	 */
	public static void main(String args[]) {
		readBattleFile();
	}

	
		
	public static String getUserInput() {
		Scanner in = new Scanner(System.in);
		
		String input;
		
		
		while(true) {
			System.out.println("Enter a character name (or type \"all\" for all characters of \"exit\" to exit): ");
			input = in.next();
			if(input.equals("exit")) {
				break;
			} else {
				readCharactersFile();
				
			}
		}
		in.close();
		return input;
	}
	
	
	
	//create a method to parse through the characters file
	public static void readCharactersFile(){
		
		String input = getUserInput();
		
		String fileName = "data/characters.csv";
		File f = new File(fileName);
		
		if (!f.canRead()) {
			System.err.printf("Error: cannot read "
		 + "data from file %s" ,fileName);
		 System.exit(1);
	 }
	
	Scanner inputFile = null;
		 try {
		 inputFile = new Scanner(f);
		 inputFile.nextLine();
		 while(inputFile.hasNextLine()) {
			 String data = inputFile.nextLine();
			 String names = data.split(",")[0];
			 String allegiances = data.split(",")[1];
			 Character chrUno = new Character(names, allegiances, null);
			 String charUnoStr = chrUno.toString();			 
			 if(names.equalsIgnoreCase(input)) {
				 System.out.println(charUnoStr);			 
			 }
			 
		 }
		 inputFile.close();
		 } catch (FileNotFoundException e) {
		
			 System.err.printf("Error: cannot read "
			 + "data from file %s" ,fileName);
			 System.exit(1);
		 }
	}

		
	public static void readBattleFile(){
		String fileName = "data/battles.csv";
		File f = new File(fileName);
		
		if (!f.canRead()) {
			System.err.printf("Error: cannot read "
		 + "data from file %s" ,fileName);
		 System.exit(1);
	 }
	
	Scanner inputFile = null;
		 try {
		 inputFile = new Scanner(f);
		 inputFile.nextLine();
		 while(inputFile.hasNextLine()) {
			 String data = inputFile.nextLine();
			 String[] values = data.split(",");
			 String battleName = values[0];
			 String attackerKing = values[1];
			 String defenderKing = values[2];
			 String attackerOutcome = values[3];
			 String battleType = values[4];
			 String location = values[5];
			 String region = values[6];
			 Battle battlesUno = new Battle(battleName, attackerKing, defenderKing, attackerOutcome, battleType, location, region);
			 String bStr = battlesUno.toString();
			 System.out.println(bStr);
		 }
		 inputFile.close();
		 } catch (FileNotFoundException e) {
		
			 System.err.printf("Error: cannot read "
			 + "data from file %s" ,fileName);
			 System.exit(1);
		 }
	}
}

This post has been edited by macosxnerd101: 24 September 2018 - 07:55 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with game input

#2 Programmer2004   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 96
  • Joined: 25-October 17

Re: Need help with game input

Posted 25 September 2018 - 01:37 AM

The first problem is in the while loop in getUserInput function. The user is prompted again and again because when something other than "exit" is typed in, there's no break instruction.

If talking about the second problem, I'm not really sure what may be the actual cause. Maybe that the first line in CSV file is not an empty line, but contains some data, so you should remove that input.nextFile(); in line 62. Try doing something like this: inside the loop in readCharactersFile(), test if "data" variable equals null or "", if yes, then start the loop again.

It looks like this:

inputFile = new Scanner(f);
while (inputFile.hasNextLine()) {
    String data = inputFile.nextLine();
    //If data is null or empty then start the loop again
    if (data == null || data.equals("")) {
        continue;
    }


Was This Post Helpful? 1
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • Posts: 16,459
  • Joined: 20-September 08

Re: Need help with game input

Posted 25 September 2018 - 02:40 AM

Quote

if (data == null || data.equals("")) {

That might still fail if there's any space characters in the line so

if (data == null || (data = data.trim()).equals("")) {

might be better.

Please name your questions something more descriptive in future.
Was This Post Helpful? 0
  • +
  • -

#4 Programmer2004   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 96
  • Joined: 25-October 17

Re: Need help with game input

Posted 25 September 2018 - 03:51 AM

Quote

That might still fail if there's any space characters in the line so

if (data == null || (data = data.trim()).equals("")) {


might be better.


Oh yeah, I didn't think about it, thank you for correcting me.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7288
  • View blog
  • Posts: 15,172
  • Joined: 16-October 07

Re: Need help with game input

Posted 25 September 2018 - 04:36 AM

Currently, we have an interesting infinite loop:
public static String getUserInput() {
    // don't do this here, should only be done once
    // Scanner in = new Scanner(System.in);

    String input;
    while (true) {
        System.out.println("Enter a character name (or type \"all\" for all characters of \"exit\" to exit): ");
        input = in.next();
        if (input.equals("exit")) {
            break;
        } else {
            // so, you've accepted a character name from the user
            // note, you never tested for "all"
            // you call your method, but ignore the character name
            readCharactersFile();

        }
    }
    // doin't do this, you don't get to close System.in
    // in.close();
    return input;
}

//create a method to parse through the characters file
public static void readCharactersFile() {
    // whoa, wait a minute
    // didn't you get here by calling getUserInput already?!?
    // what will this do, then
    String input = getUserInput();



Think about it:
call readCharactersFile()
    call getUserInput()
        call readCharactersFile()
            call getUserInput()
            ...



Instead, think about passing values. Also think about passing back values.

So:
private static void userMenu() {
    // if you are using a single point of user interaction
    // and I recommend you do
    // then this is where you'd want your scanner
    // indeed, this might be your main
    final Scanner in = new Scanner(System.in);

    // String input;
    boolean done = false;
    //while (true) {
    while (!done) {
        System.out.println("Enter a character name (or type \"all\" for all characters of \"exit\" to exit): ");
        String input = in.next();
        // weren't you told something about "Remember to ignore the case of userInput"
        // perhaps you could do that before we start looking at input?
        if (input.equals("exit")) {
            done = true;
            // break;
        } else {
            readCharactersFile(input);
        }
    }
}

private static void readCharactersFile(String characterName) {



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1