Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMMING Correct the spacing in the code below to get an output for the

ID: 3696057 • Letter: J

Question

JAVA PROGRAMMING

Correct the spacing in the code below to get an output for the program.

_____________________________________________________________

import java.util.Scanner;

import java.io.*;

public class lhangman

{

public static void main(string[] args) throws IOException

{

Scanner lscan = new Scanner (System.in);

char lAgain = 'n';

String lsecret;

StringBuffer ldashes;

final int lMAXPARTS =6;

int lbodyparts;

boolean done;

String lguess;

String guesses;

char lLetter;

Scanner lInfile = new Scanner (new FileReader("hangWords.txt"));

do

{

lsecret = lInfile.next();

guesses = " ";

done = false;

lbodyparts = lMAXPARTS;

ldashes = lmakeDashes(lsecret);

While (! done)

{

System.out.println("Your word is here: " + ldashes);

System.out.println("Guesses : " + guesses);

System.out.print("Enter a guess : ");

lguess=lscan.next();

if (lguess.length () > 1)

{

if(lguess.equals(lsecret))

System.out.println("You win!");

else

System.out.println("You lose");

done=true;

}

else

{

lLetter = lguess.charAt(0);

guesses += lLetter;

if (lsecret.indexOf (lLetter) < 0)

{

- -lbodyparts;

System.out.print("bad guess - ");

}

else

{

lmatchLetter(lsecret,ldashes,lLetter);

}

System.out.println(lbodyparts + "bodyparts left");

if(lbodyparts == 0)

{

System.out.println("You lose");

done= true;

}

if (lsecret.equals(ldashes.toString() ) )

{

System.out.println("You win! ");

done= true;

}

}

}

if (lInfile.hasNext() )

{

System.out.print("play again ?: ");

lAgain = lscan.next().charAt(0);

}

else

System.out.println("Thanks ");

}

while (lInfile.hasNext() && (lAgain == 'Y' // lAgain == 'Y') );

}

public static void lmatchLetter(String lsecret,StringBuffer ldashes,char lLetter)

{

for (int lIndex = 0; lIndex < lsecret.length(); lIndex++)

if (lsecret.charAt(lIndex) == lLetter)

ldashes.setCharAt(lIndex,lLetter);

System.out.print("good guess - ");

}

public static StringBuffer lmakeDashes(String s)

{

StringBuffer ldashes = new StringBuffer (s.length());

for (int lcount = 0; lcount < s.length(); lcount++)

ldashes.append ( ' -');

return ldashes;

}

}

Explanation / Answer


package ihangman;

import java.util.Scanner;
import java.io.*;
public class lhangman
{
public static void main(String[] args) throws IOException
{
Scanner lscan = new Scanner (System.in);
char lAgain = 'n';
String lsecret;
StringBuffer ldashes;
final int lMAXPARTS =6;
int lbodyparts;
boolean done;
String lguess;
String guesses;
char lLetter;
Scanner lInfile = new Scanner (new FileReader("hangWords.txt"));
do
{
lsecret = lInfile.next();
guesses = " ";
done = false;
lbodyparts = lMAXPARTS;
ldashes = lmakeDashes(lsecret);
while(!done)
{
System.out.println("Your word is here: " + ldashes);
System.out.println("Guesses : " + guesses);
System.out.print("Enter a guess : ");
lguess=lscan.next();
if (lguess.length () > 1)
{
if(lguess.equals(lsecret))
System.out.println("You win!");
else
System.out.println("You lose");
done=true;
}
else
{
lLetter = lguess.charAt(0);
guesses += lLetter;
if (lsecret.indexOf (lLetter) < 0)
{
--lbodyparts;
System.out.print("bad guess - ");
}
else
{
lmatchLetter(lsecret,ldashes,lLetter);
}
System.out.println(lbodyparts + "bodyparts left");
if(lbodyparts == 0)
{
System.out.println("You lose");
done= true;
}
if (lsecret.equals(ldashes.toString() ) )
{
System.out.println("You win! ");
done= true;
}
}
}
if (lInfile.hasNext() )
{
System.out.print("play again ?: ");
lAgain = lscan.next().charAt(0);
}
else
System.out.println("Thanks ");
}while (lInfile.hasNext() && (lAgain == 'Y' || lAgain == 'y')); // lAgain == 'Y') );
}

public static void lmatchLetter(String lsecret,StringBuffer ldashes,char lLetter)
{
for (int lIndex = 0; lIndex < lsecret.length(); lIndex++)
if (lsecret.charAt(lIndex) == lLetter)
ldashes.setCharAt(lIndex,lLetter);
System.out.print("good guess - ");
}

public static StringBuffer lmakeDashes(String s)
{
StringBuffer ldashes = new StringBuffer (s.length());
for (int lcount = 0; lcount < s.length(); lcount++)
ldashes.append ('-');
return ldashes;
}
}