SRL Archive - Help and Support

Java


Le Jingle

2012-10-29 00:06:26

One of our assignments was to make a box in the debug..

Would this be effective? I'd think it's okay, given this is all we've covered in the class, but are there things that I've overlooked, things that I've not covered (ex would be things that help the code that wouldn't be at the level of code or below) that anyone suggests?

public class Boxes {

private final static int SIZE = 6; // Prints a 6x6 hollow box

public static void main(String[] args) {
boxProblem();
}

public static void boxProblem() {

for (int row = 1; row <= SIZE; row++) {

if (row == 1) {
for (int ii = 1; ii <= SIZE; ii++) {
System.out.print("*");
}
System.out.println();
} else if ((row > 1) && (row < SIZE)) {

for (int col = 1; col <= SIZE; col++) {
if (col == 1) {
System.out.print("*");
} else if ((col > 1) && (col < SIZE)) {
System.out.print(" ");
} else if (col == SIZE) {
System.out.println("*");
}
}
} else if (row == SIZE) {

for (int ii = 1; ii <= SIZE; ii++) {
System.out.print("*");
}
System.out.println();
}
}
}
}

Basically, anything that's more advanced, what is it (or at least what's it called)? Just lookin' to learn :cool:

Will also edit in another problem I'm working on, about building a debug rocket ship :cool: x2

J J

2012-10-29 01:12:36

Do you use the book Big Java Late Objects? I had to make a very similar question. I solved it the same way as you did.

Le Jingle

2012-10-29 01:34:46

From: J J
Do you use the book Big Java Late Objects? I had to make a very similar question. I solved it the same way as you did.


Ah that's good news, thanks for the reassurance :)
The book we are learning from is: Absolute Java (5th Edition by Walter Savitch)



I'll be off for now working on my second problem to draw a rocket ship (can't help but to chuckle as I sound like a toddler) and there's some merlot with my name on it ;)

Cheers,
Lj! :)

JPHamlett

2012-10-29 03:33:46


public void drawHollowSquare (int size, char ch)
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
if (x==0 || x==size-1 || y==0 || y==size-1)
System.out.print(ch + " ");
else
System.out.print(" ");
}
System.out.println();
}
}

Look at that, it does that same thing just in less lines :)

Le Jingle

2012-10-29 12:32:37

From: JPHamlett


Look at that, it does that same thing just in less lines :)


Nice! I like the simplicity. I suppose if we're allowed to construct our own squares this could be ideal code (: , however sadly we're told we could not have spaces other than what's inside the enclosed box :/

Ex

*****
* *
* *
* *
*****

Any who, appreciated. :)

I'll try to get my rocket drawing problem done and see how that fairs against ya'll java minds :)

Cheers,
Lj

JPHamlett

2012-10-29 13:36:34

From: Le Jingle
Nice! I like the simplicity. I suppose if we're allowed to construct our own squares this could be ideal code (: , however sadly we're told we could not have spaces other than what's inside the enclosed box :/

Ex

*****
* *
* *
* *
*****

Any who, appreciated. :)

I'll try to get my rocket drawing problem done and see how that fairs against ya'll java minds :)

Cheers,
Lj


You could just as easily remove the spaces, its not to difficult, and you seem to have some java understanding, you can figure it out :)

Le Jingle

2012-10-29 19:26:17

Bah! good point, that is indeed much simpler code.


public class temp
{


public static void drawHollowSquare (int size)
{
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
if (x==0 || x==size-1 || y==0 || y==size-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}

}

Gave the same (I'm pretty sure) as the original code I posted. Indeed nice, also, we've yet to learn headers that deal with only public void methodName(), so I changed it to a public static void header :)

Thanks for lesson!

I'm still working on my rocketShip drawing, it's due in 2 days, so I'll probably wait till the last minute and then get around to posting it the day after or so.

Thanks all for the Java help!

Cheers,
Lj

Le Jingle

2012-10-30 15:03:23


public class pointUp
private final static int HEIGHT = 5;

public static void drawPointFacingUp() {
for (int i = 0; i < HEIGHT; i++) {

System.out.print("|");

for (int j = 0; j < HEIGHT - i - 1; j++) {
System.out.print(".");
}

System.out.print("/\\");

if (i > 0) {
for (int j = 0; j < 2 * i - 2; j++) {
System.out.print("/\\");
}
System.out.print("/\\");
}


for (int j = HEIGHT - i - 1; j > 0; j--) {
System.out.print(".");
}

// Repetitive but we need it all in the same line on the same loop?

for (int j = 0; j < HEIGHT - i - 1; j++) {
System.out.print(".");
}
System.out.print("/\\");
if (i > 0) {
for (int j = 0; j < 2 * i - 2; j++) {
System.out.print("/\\");
}
System.out.print("/\\");
}

for (int j = HEIGHT - i - 1; j > 0; j--) {
System.out.print(".");
}

System.out.println("|");
}
}
}

I can't get even spaced diamond shaped point tips for a part of my rocket :/

Any help/advice would be helpful/appreciated :)

(with a final int HEIGHT = 5, I get)

|..../\......../\....|
|.../\/\....../\/\...|
|../\/\/\/\..../\/\/\/\..|
|./\/\/\/\/\/\../\/\/\/\/\/\.|
|/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\|
or if I remove the for loops within the if statements:

|..../\......../\....|
|.../\/\....../\/\...|
|../\/\..../\/\..|
|./\/\../\/\.|
|/\/\/\/\|

I see that each pattern is 2 more or 2 less each following row, but I don't know how to get the middle ground :/

I should end up with 2 diamond top halfs or tips or arrows pointing up.

Le Jingle

2012-10-31 00:09:12

BEHOLD! :) The awesome rocket ship drawer! (took me two days off and on but I finally made some (over used / longer) code to do what I want!


public class Rocket {
private final static int HEIGHT = 5; // height of rocket internal subfigures

public static void main(String[] args) {

displayCone(); // display rocket nose
lineTheRocket(); // place a line inbetween rocket Body Parts/Cone/Thruster

drawTopDiamond();
drawLowerDiamond();

lineTheRocket();

drawLowerDiamond();
drawTopDiamond();

lineTheRocket();
displayCone(); // display rocket thrust

}

public static void displayCone() {
for (int i = 0; i < HEIGHT * 2 - 1; i++) {

//1st nest: Going from left to right, we'll say hey, we need ii # of spaces
for (int ii = HEIGHT * 2 - 1; ii-i > 0; ii--) {
System.out.print(" ");
}

//2nd nest: Will print out a "/" while ii is less than or equal to outer loop i
for (int ii = 0; ii <= i ; ii++) {
System.out.print("/");
}

System.out.print("**");

//3rd nest: Same loop as the first "/" side, excepting face opposite
for (int ii = 0; ii <= i ; ii++) {
System.out.print("\\");
}

System.out.println();
}
}

public static void drawTopDiamond() {
for (int i = 0; i < HEIGHT; i++) {

System.out.print("|");

for (int j = 0; j < HEIGHT - i - 1; j++) {
System.out.print(".");
}

System.out.print("/\\");

if (i > 0) {
for (int j = 0; j <= i - 2; j++) {
System.out.print("/\\");
}
System.out.print("/\\");
}

for (int j = HEIGHT - i - 1; j > 0; j--) {
System.out.print(".");
}

// Repetitive but I think we need it all in the same line on the same loop

for (int j = 0; j < HEIGHT - i - 1; j++) {
System.out.print(".");
}

System.out.print("/\\");

if (i > 0) {
for (int j = 0; j <= i - 2; j++) {
System.out.print("/\\");
}
System.out.print("/\\");
}

for (int j = HEIGHT - i - 1; j > 0; j--) {
System.out.print(".");
}

System.out.println("|");
}
}

// Opposite of drawTopDiamond(), different code as I had trouble trying to reverse the loops in drawTopDiamond().
// General idea here is that we...:
// 1- start with drawing the side wall and then pad with dots,
// 2- do our first diamond,
// 3- pad with dots x2
// 4- draw our second diamond,
// 5- pad with dots and close by drawing the side wall.
public static void drawLowerDiamond() {
for (int i = 0; i < HEIGHT; i++) {

// 1
System.out.print("|");
for (int ii = 0; ii < i ; ii++) {
System.out.print(".");
}

// 2
for (int ii = 0; ii < HEIGHT - i; ii++) {
System.out.print("\\/");
}

// 3
for (int ii = 0; ii < i; ii++) {
System.out.print(".");
}
for (int ii = 0; ii < i ; ii++) {
System.out.print(".");
}

//4
for (int ii = 0; ii < HEIGHT - i; ii++) {
System.out.print("\\/");
}

//5
for (int ii = 0; ii < i; ii++) {
System.out.print(".");
}
System.out.println("|");
} // end of outer loop
} // end of method

public static void lineTheRocket() {
System.out.print("+");
for (int i = 0; i < HEIGHT*2; i++) {
System.out.print("=*");
}
System.out.println("+");
}

}

^ This will draw :


/**\
//**\\
///**\\\
////**\\\\
/////**\\\\\
//////**\\\\\\
///////**\\\\\\\
////////**\\\\\\\\
/////////**\\\\\\\\\
+=*=*=*=*=*=*=*=*=*=*+
|..../\......../\....|
|.../\/\....../\/\...|
|../\/\/\..../\/\/\..|
|./\/\/\/\../\/\/\/\.|
|/\/\/\/\/\/\/\/\/\/\|
|\/\/\/\/\/\/\/\/\/\/|
|.\/\/\/\/..\/\/\/\/.|
|..\/\/\/....\/\/\/..|
|...\/\/......\/\/...|
|....\/........\/....|
+=*=*=*=*=*=*=*=*=*=*+
|\/\/\/\/\/\/\/\/\/\/|
|.\/\/\/\/..\/\/\/\/.|
|..\/\/\/....\/\/\/..|
|...\/\/......\/\/...|
|....\/........\/....|
|..../\......../\....|
|.../\/\....../\/\...|
|../\/\/\..../\/\/\..|
|./\/\/\/\../\/\/\/\.|
|/\/\/\/\/\/\/\/\/\/\|
+=*=*=*=*=*=*=*=*=*=*+
/**\
//**\\
///**\\\
////**\\\\
/////**\\\\\
//////**\\\\\\
///////**\\\\\\\
////////**\\\\\\\\
/////////**\\\\\\\\\

Feel free to post shorter code for me to learn from :p

Le Jingle

2012-11-19 00:28:28

Newest homework I've dev'd…

Count char characters in a text file!!! (ground breaking, right?!) :p

code - feel free to criticize improve too :)

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

public class LetterInventory {

public final static String filename = "testFile.txt";

// private globals to work with:
private static int[] letters;

// Driver to test LetterInventory class
public static void main(String[] args) {
Scanner inputFile = null;
try {
inputFile = new Scanner(new File(filename));
} catch (Exception e) {
System.out.println("File could not be opened: " + filename);
System.exit(0);
}

LetterInventory inventory = new LetterInventory();
inventory.countOccurrences(inputFile);
inventory.displayTable();
inventory.resetInventory();
}

// Method to create our inventory,
// an array to count char occurences:
// max of 26 indices - for letters array to hold.
public LetterInventory() {
letters = new int[26];
}

// Counts frequency of letters by;
// 1. making sure there's a next token to consume,
// 2. store the token as string,
// 3. loop through the string token and mis-match char values to String temp index,
// 4. increase private letters int[] counter if appropiate value found-
// (after meeting the if statement requirements).
public static void countOccurrences(Scanner file) {

// While theres tokens left to consume...
while (file.hasNext()) {

// Store the next token as a String;
String temp = file.next();

// loop through the temp String and count occurence of char values (if any);
for (int i = 0; i < temp.length(); i++) {

// compare values of char in the temp String
// to values in alphabet.
if ((temp.charAt(i)-'a' < 26) && (temp.charAt(i)-'a' >= 0)) {
letters[(int)(temp.charAt(i)-'a')]++;
} else if ((temp.charAt(i)-'A' < 26) && (temp.charAt(i)-'A' >= 0)) {
letters[(int)(temp.charAt(i)-'A')]++;
}
}
}
}

// Displays the global private letters array:
// loop starts at 97 since output is desired to be lowercase,
// casts the loop value to char, and prints the index-97 value of int counter.
public static void displayTable() {
for (int i = 97; i < 123; i++) {
System.out.print((char)(i) + ": ");
System.out.println(letters[i-97]);
}
}

// Resets all the array to null, or all are equal to zero counts.
public static void resetInventory() {
letters = null;
}
}

Le Jingle

2012-12-04 05:20:08

Here's some stuffs the professor went over rather quickly:
(Was fun to experiment with negatives, blurring, sharpening, resizing smaller, and very loose edge detection.)

Note: I didn't author this, sole credit to my professor.


import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class ImageStuff {

public static int getRed(int pixel) {
return (pixel >> 16) & 255;
}

public static int getGreen(int pixel) {
return (pixel >> 8) & 255;
}

public static int getBlue(int pixel) {
return pixel & 255;
}

public static int getPixel(int red, int green, int blue) {
return (255 << 24) + (red << 16) + (green << 8) + blue;
}

public static BufferedImage blur(BufferedImage input) {
int width = 5;

BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);

for (int r = 0; r < input.getHeight(); r++) {
for (int c = 0; c < input.getWidth(); c++) {
int red = 0, green = 0, blue = 0;
int area = 0;
for (int i = -width/2; i <= width/2; i++) {
for (int j = -width/2; j <= width/2; j++) {
if (r+i >= 0 && c+j >=0 && r+i < input.getHeight() && c+j < input.getWidth()) {
int pixel = input.getRGB(c+j, r+i);
red += getRed(pixel);
green += getGreen(pixel);
blue += getBlue(pixel);
area++;
}
}
}
output.setRGB(c, r, getPixel(red / area, green / area, blue / area));
}
}

return output;
}

public static void edges(BufferedImage input) {
int width = 5;

BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);

for (int r = 0; r < input.getHeight(); r++) {
for (int c = 0; c < input.getWidth(); c++) {
int red = 0, green = 0, blue = 0;
int area = 0;
for (int i = -width/2; i <= width/2; i++) {
for (int j = -width/2; j <= width/2; j++) {
if (r+i >= 0 && c+j >=0 && r+i < input.getHeight() && c+j < input.getWidth()) {
int pixel = input.getRGB(c+j, r+i);
red += getRed(pixel);
green += getGreen(pixel);
blue += getBlue(pixel);
area++;
}
}
}
output.setRGB(c, r, getPixel(red / area, green / area, blue / area));
}
}

for (int r = 0; r < input.getHeight() - 1; r++) {
for (int c = 0; c < input.getWidth() - 1; c++) {
int pixel1 = output.getRGB(c, r);
int pixel2 = output.getRGB(c+1, r);
int pixel3 = output.getRGB(c, r+1);

double redDiff = Math.pow(getRed(pixel1) - getRed(pixel2), 2.0) +
Math.pow(getRed(pixel1) - getRed(pixel3), 2.0);
double greenDiff = Math.pow(getGreen(pixel1) - getGreen(pixel2), 2.0) +
Math.pow(getGreen(pixel1) - getGreen(pixel3), 2.0);
double blueDiff = Math.pow(getBlue(pixel1) - getBlue(pixel2), 2.0) +
Math.pow(getBlue(pixel1) - getBlue(pixel3), 2.0);

int gradient = (int) (2 * Math.sqrt(redDiff + greenDiff + blueDiff));
input.setRGB(c, r, getPixel(gradient, gradient, gradient));
}
}
}

public static void main(String[] args) {
BufferedImage input = null;

try {
input = ImageIO.read(new File("asd.gif"));
} catch (Exception e) {
System.out.println("Couldn't read image");
System.exit(0);
}

BufferedImage negative = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);

for (int r = 0; r < input.getHeight(); r++) {
for (int c = 0; c < input.getWidth(); c++) {
int pixel = input.getRGB(c, r);
int red = getRed(pixel);
int green = getGreen(pixel);
int blue = getBlue(pixel);
int alpha = pixel >> 24;

red = 256 - red;
green = 256 - green;
blue = 256 - blue;

negative.setRGB(c, r, getPixel(red, green, blue));
}
}

BufferedImage smaller = new BufferedImage(input.getWidth()/2, input.getHeight()/2, BufferedImage.TYPE_INT_ARGB);

for (int r = 0; r < smaller.getHeight(); r++) {
for (int c = 0; c < smaller.getWidth(); c++) {
int pixel1 = input.getRGB(2*c, 2*r);
int pixel2 = input.getRGB(2*c+1, 2*r);
int pixel3 = input.getRGB(2*c, 2*r+1);
int pixel4 = input.getRGB(2*c+1, 2*r+1);
int red = (getRed(pixel1) + getRed(pixel2) + getRed(pixel3) + getRed(pixel4)) / 4;
int green = (getGreen(pixel1) + getGreen(pixel2) + getGreen(pixel3) + getGreen(pixel4)) / 4;
int blue = (getBlue(pixel1) + getBlue(pixel2) + getBlue(pixel3) + getBlue(pixel4)) / 4;

smaller.setRGB(c, r, getPixel(red, green, blue));
}
}

BufferedImage blurred = blur(input);

BufferedImage sharpened = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int r = 0; r < input.getHeight(); r++) {
for (int c = 0; c < input.getWidth(); c++) {
double percent = 1.0;
int pixel1 = input.getRGB(c, r);
int pixel2 = blurred.getRGB(c, r);
int red = getRed(pixel1) + (int) (percent * (getRed(pixel1) - getRed(pixel2)));
red = Math.min(Math.max(red, 0), 255);

int green = getGreen(pixel1) + (int) (percent * (getGreen(pixel1) - getGreen(pixel2)));
green = Math.min(Math.max(green, 0), 255);

int blue = getBlue(pixel1)+ (int) (percent * (getBlue(pixel1) - getBlue(pixel2)));
blue = Math.min(Math.max(blue, 0), 255);

sharpened.setRGB(c, r, getPixel(red, green, blue));
}
}

edges(input);

try {
ImageIO.write(negative, "gif", new File("negative.gif"));
ImageIO.write(blurred, "gif", new File("blurred.gif"));
ImageIO.write(sharpened, "gif", new File("sharpened.gif"));
ImageIO.write(smaller, "gif", new File("smaller.gif"));
ImageIO.write(input, "gif", new File("edges.gif"));
} catch (Exception e) {
System.out.println("Couldn't write image");
System.exit(0);
}

}
}

Le Jingle

2012-12-05 19:01:17

Fun game to play, only took an evening for my novice skills to develop (note: ignore the horrid code mess :p )

Hangman

import java.util.Scanner;
public class Hangman {

// used for storing file in constructor
private Scanner wordList;

public Hangman(Scanner file) {
// Store the file object in an instance variable for later use
wordList = file;
}

public void displayGameIntro() {
// Output the introduction
System.out.println("Welcome to the hangperson game ...");
System.out.println("To play, guess a letter to try to guess the word.");
System.out.println("Every time you choose an incorrect letter another");
System.out.println("body part appears on the gallows. If you guess the");
System.out.println("word before you're hung, you win :-)");
System.out.println("If you get hung you lose :-(");
System.out.println("");
System.out.println("Time to guess ...");
System.out.println("");
}

/*
* switch statement, returns the image/state of man on gallows based
* on how many failCount is passed as parameter.
*/
public void hangmanState(int failCount) {

switch (failCount) {
case 0:
System.out.println("|-----|-");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|__________");
System.out.println("");
break;

case 1:
System.out.println("|-----|-");
System.out.println("| O");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|__________");
System.out.println("");
break;

case 2:
System.out.println("|-----|-");
System.out.println("| O");
System.out.println("| |");
System.out.println("|");
System.out.println("|");
System.out.println("|__________");
System.out.println("");
break;

case 3:
System.out.println("|-----|-");
System.out.println("| O");
System.out.println("| |");
System.out.println("| |");
System.out.println("|");
System.out.println("|__________");
System.out.println("");
break;

case 4:
System.out.println("|-----|-");
System.out.println("| O");
System.out.println("| \\|");
System.out.println("| |");
System.out.println("|");
System.out.println("|__________");
System.out.println("");
break;

case 5:
System.out.println("|-----|-");
System.out.println("| O");
System.out.println("| \\|/");
System.out.println("| |");
System.out.println("|");
System.out.println("|__________");
System.out.println("");
break;

case 6:
System.out.println("|-----|-");
System.out.println("| O");
System.out.println("| \\|/");
System.out.println("| |");
System.out.println("| / ");
System.out.println("|__________");
System.out.println("");
break;

case 7:
System.out.println("|-----|-");
System.out.println("| O");
System.out.println("| \\|/");
System.out.println("| |");
System.out.println("| / \\");
System.out.println("|__________");
System.out.println("");
break;

}
}

/*
* This method has parameters to accept a String (ideally in this case
* to be the word from the text file being read) and an array
* of char (ideally being guesses from user input).
*
* If char's from the char array are in the String word,
* then those that match (see interal method description) are saved
* and the remaining spaces are padded/hidden with '-' char.
*
*/
public char[] getWordState(String word, char[] testChars) {
// result is going to be a string with the same length and the input String word.
char[] result = new char[word.length()];
char[] temp;
temp = word.toCharArray();

for (int i = 0; i < word.length(); i++) {
for (int j = 0; j < testChars.length; j++) {
// if the i-index of temp equals the j-index of testChars,
// then that same char is placed in the char result i-th index.
if (temp[i] == testChars[j]) {
result[i] = temp[i];
}
}
}

// here is a loop to pad in - to hide non guessed char's / letters of the word.
for (int k = 0; k < word.length(); k++) {
if (!(word.charAt(k) == result[k])) {
result[k] = '-';
}
}
return result;
}

/*
* Converts a char array parameter to a String, as well as formatting the string
*/
public String charArrToString(char[] characters) {
String result = "";
for (int i = 0; i < characters.length; i++) {
result += result.valueOf(characters[i]) + " ";
}
return result;
}

/*
* method to compare a char against a char array.
* returns true only if the char c, is found within the char[] cArr.
*/
public boolean charInCharArray(char c, char[] cArr) {
for (int i = 0; i < cArr.length; i++) {
if (c == cArr[i]) {
return true;
}
}
return false;
}

/*
* Plays hangman game.
* * Has an outer while loop -> grabs next word when present from constructor Scanner file.
* - declares inner variables that reset upon a new word.
* * has an inner while loop -> continues to play the game as long as the fail count doesnt equal 7
* and the index of different letters doesn't exceed 26
* - reads the input character from console
* - tests to filter our non alphabet characters
* - marks the guessed character in a boolean array
* - checks given tests above before proceeding to intake next token/character/letter
* - increases fails if wrong guess (letter isn't in hidden/final word)
* - breaks from inner while loop if the word is guessed.
* - outputs results from game (W or L messages)
* - breaks from outer while loop -> to check if user wants to play more/stop playing.
*/

public void play () {
String inputLetter;
char[] letters;
char nextLetter;

while (wordList.hasNext()) {
// variable values to be reset upon each new word/string token read from file
String currentWord = wordList.next();
letters = new char[currentWord.length()];
letters = currentWord.toCharArray();
char[] guesses = new char[26];
boolean[] alreadyPicked = new boolean[26];
int fails = 0;
int guessIndex = 0;

// we won't continue if fails == 7, and will only continue if our guessIndex is less than 26 (meaning we can't guess more letters than in the alphabet.
while ((!(fails == 7)) || (guessIndex < 26)) {
hangmanState(fails);
//System.out.println("Letters guessed already => " + new String(guesses));
System.out.println("Letters guessed already => " + charArrToString(guesses));
System.out.println("Number of wrong guesses => " + fails);
System.out.println("The word so far => " + new String(getWordState(currentWord, guesses)));
System.out.println("");
System.out.print("Choose a letter => ");

boolean appropiateGuessBool = false;
boolean alreadyUsed = false;

// reads the input character from console
Scanner keyboard = new Scanner(System.in);
inputLetter = keyboard.next().toLowerCase();
nextLetter = inputLetter.charAt(0);

// tests to filter our non alphabet characters
for (int i = 'a'; i <= 'z'; i++) {
if (nextLetter == i) {
// marks if the guess was within range of an appropiate letter.
appropiateGuessBool = true;
}
}

// marks the guessed character in a boolean array
if (appropiateGuessBool == true) {
if (!(alreadyPicked[(int)('z') - (int)(nextLetter)] == false)) {
alreadyUsed = true;
}

// marks which index in bool arr that corresponds to a guessed letter
alreadyPicked[(int)('z') - (int)(nextLetter)] = true;
}

System.out.println("");

// checks given tests above before proceeding to intake next token/character/letter
if (alreadyUsed == true) {
System.out.println("You already tried this letter");
System.out.println("");
continue;
}

if (appropiateGuessBool == false) {
System.out.println("Invalid, don't you know your ABCs?");
System.out.println("");
} else {

guesses[guessIndex] = (char)(nextLetter);
guessIndex++;

if (!(charInCharArray(nextLetter, letters))) {
fails++;
}

if (new String(getWordState(currentWord, guesses)).equals(currentWord)) {
break;
}

}
}

// Handle result (w vs. l)
if (fails == 7) {
System.out.println("Too bad, you lose!");
System.out.println("The word was ==> " + currentWord);
} else {
hangmanState(fails);
System.out.println("Letters guessed already => " + new String(guesses));
System.out.println("Number of wrong guesses => " + fails);
System.out.println("The word so far => " + new String(getWordState(currentWord, guesses)));
System.out.println("");
System.out.println("Congratulations, you win!!!");
System.out.println("");
}

break;
}
}
}



Hangman Driver

/* This program is a word guessing game called hangman.
* A person will try and guess a word before the max
* number of guesses are used. Every time the user chooses an
* incorrect letter another body part is displayed in the gallows.
*/

import java.util.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class HangmanDriver {

public static final String filename = "words.txt";

// Driver to run the game using the student Hangman class
public static void main(String[] args) {
Scanner wordsFile = null; // words data file

// open the file containing the words
try {
wordsFile = new Scanner(new FileInputStream(filename));
}
catch (FileNotFoundException e) {
System.out.println("File not found or not opened.");
System.exit(0);
}

// create object and prepare to play game
Hangman game = new Hangman(wordsFile);
Scanner keyboard = new Scanner(System.in);

// display an introduction on the game to the player
game.displayGameIntro();

// continually play new games if the user desires
String playAgain;
do {
game.play();
System.out.print("Do you want to play again? ");
playAgain = keyboard.next();
System.out.println();
} while (playAgain.toUpperCase().startsWith("Y"));

System.out.println("Thanks for playing!");
}
}


* note to save a text file in same directory with tokens in upper/lowercase
ex.

hello
world
program
yayFunStuff
HAHA
* copy and paste into text file, save as/name file as,