3 Replies - 176 Views - Last Post: 06 December 2012 - 05:56 PM Rate Topic: -----

#1 fhsdar40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 12

LOADIN ARRAY FROM DIFFRENT FILE

Posted 06 December 2012 - 05:18 PM

I have a phrase guessing game and my instructor wants me to Create a file that that contains different words. the program should then read those words from the file and load them into an array.

here is the phrase game. please keep in mind i am new at java. first language. thanks in advance

import java.util.*;

public class gussinggame { 
	
	// Variables
	public static String[] words = {"detroit tigers"};
	public static boolean gameOver = false;
	public static StringBuilder newString = new StringBuilder();
	public static int missedBy = 0;
	
	public static void main(String[] args)
	{
		Random rand = new Random();
		int randWord = rand.nextInt(words.length);
		String wordSelected = words[0]; 
		runGame(wordSelected);
		printAstr();
	}
	
	
	
	public static void runGame(String wordSelected)
	{
		int i = 0;
		char charInputed;
		
		for(i = 0; i < wordSelected.length(); i++)
		{
			newString.append("*");
		}
		
		while(gameOver == false)
		{
			if(! wordSelected.equals(newString.toString()))
			{
				Scanner input = new Scanner(System.in);
				System.out.println("(Guess) Enter a letter in word " + newString + " > ");
				charInputed = input.next().charAt(0);
				calculate(wordSelected, charInputed);
			}
			else
			{
				System.out.println("The word is " + newString + ".  You missed " + missedBy + " time.");
				gameOver = true;
			}
		}
	}
	
	
	
	public static void calculate(String wordSelected, char charInputed)
	{
		int i = 0;
		String temp;
		boolean isItAMiss = false;
		
		for(i = 0; i < wordSelected.length(); i++)
		{
			if(wordSelected.charAt(i) == charInputed)
			{
				temp = Character.toString(wordSelected.charAt(i));
				newString.replace(i, (i + 1), temp);
			}
			else
			{
				isItAMiss = true;
			}
		}
		if(isItAMiss = true)
		{
		        missedBy++;
			isItAMiss = false;
		}
	}
	
	
	
	public static void printAstr()
	{
		System.out.println(newString);
	}
	
	
	
}

Attached File(s)


This post has been edited by Atli: 06 December 2012 - 05:27 PM
Reason for edit:: Use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: LOADIN ARRAY FROM DIFFRENT FILE

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: LOADIN ARRAY FROM DIFFRENT FILE

Posted 06 December 2012 - 05:29 PM

I don't see any code in there that is attempting to load anything from a file. Have you not attempted to do this yet? If not, then why? What, exactly, are you having problems with?
Was This Post Helpful? 0
  • +
  • -

#3 fhsdar40  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-December 12

Re: LOADIN ARRAY FROM DIFFRENT FILE

Posted 06 December 2012 - 05:51 PM

I dont know how to do it. Unfortunately i missed the lecture. I need a file created with words that can be loaded.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: LOADIN ARRAY FROM DIFFRENT FILE

Posted 06 December 2012 - 05:56 PM

Well, the first thing you want to look into is how to actually read a file in Java. There are about a billion tutorials on that subject out in the wild, so getting info on that shouldn't be tricky. I would suggest starting with the one in the Java docs.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1