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

I am currently working on a project and almost done with it but have run into an

ID: 3601141 • Letter: I

Question

I am currently working on a project and almost done with it but have run into an error i can't fix.

I have to run ScramTest

ScramTest:

import java.io.*;
import java.util.*;
public class ScramTest
{
public static void main(String[] args) throws IOException
{
System.out.println("Testing normal use... ");
Scramble theScramble = new Scramble("words.txt");
String word = theScramble.getRealWord();
System.out.print(word);
while (word != null)
{
System.out.println("Real word is : " + word);
System.out.println("Scrambled word is: " + theScramble.getScrambledWord());

word = theScramble.getRealWord();
}

System.out.println(" Testing special cases... ");
theScramble = new Scramble("words.txt");
String word1 = theScramble.getRealWord();
String word2 = theScramble.getRealWord();
System.out.println("Two calls to getRealWord(): ");
System.out.println("Word 1: " + word1);
System.out.println("Word 2: " + word2);
System.out.println(" Two calls to getScrambledWord(): ");
word1 = theScramble.getScrambledWord();
word2 = theScramble.getScrambledWord();
System.out.println("Scram word 1: " + word1);
System.out.println("Scram word 2: " + word2);
System.out.println(" Now a call to each: " );
word1 = theScramble.getRealWord();
word2 = theScramble.getScrambledWord();
System.out.println("Word: " + word1);
System.out.println("Scram word: " + word2);
}
}

Scramble:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Random;

import java.util.Scanner;

public class Scramble {

boolean wordsleft = true;

private String word = "";

private Scanner getword;

private File words;

private boolean called = true;

private StringBuilder holder= new StringBuilder("");

public Scramble(String s) {

try {

words = new File(s);

getword = new Scanner(words);

} catch (FileNotFoundException e) {

System.out.println("File not found");

}

}

public String getRealWord() {

if (getword.hasNextLine() && called==true) {

word = getword.next();

getword.nextLine();

}else if(called==false){

word=word;

}else

word = null;

called = false;

return word;

}

public String getScrambledWord() {

if(called)

return holder.toString();

String scrambler ="";

if(word==null)

{

return("No Scrambled Word Yet");

}

StringBuilder reg = new StringBuilder(word);

StringBuilder scram = new StringBuilder();

while (reg.length() != 0) {

int randPicker = (int) (Math.random() * reg.length());

scram.append(reg.charAt(randPicker));

reg.deleteCharAt(randPicker);

}

scrambler += scram;

holder.replace(0,holder.length(),scrambler);

called = true;

return scrambler;

}

}

I am not allowed to edit ScramTest so my error must be somewhere within Scramble.

The change when made must not interfere with the running of Assig2

Assig2:

import java.util.Scanner;

public class Assig2 {

public static void main(String[] args) {
char compscram, compguess;
Scanner input = new Scanner(System.in);
System.out.println("Welcome new player, please enter your name:");
String player = input.nextLine();
boolean play = true;
Scramble s = new Scramble("words.txt");
Results r = new Results("results.txt");
// Play Loop//
while (play == true) {
boolean win = false;
String word = s.getRealWord();
if (word == null)
break;
String scramble = s.getScrambledWord();
  
System.out.println(player + ", you have 3 guesses, good luck!");
for (int i = 3; i > 0; i--) {
System.out.println(" You have " + i + " guesses remaining.");
System.out.println("Scramble:" + scramble);
System.out.println("Enter a Guess:");
String guess = input.next();
if(!(guess.equalsIgnoreCase(word)))
{
System.out.print("Sorry, " + player + " that is not correct. ");
System.out.print("Here are the letters you got correct! ");
int len = word.length();
if (len > guess.length())
len = guess.length();
for(int counter = 0; counter < len; counter++ )
{
compscram = word.charAt(counter);
compguess = guess.charAt(counter);

if(compscram != compguess)
{
System.out.print("_");
}
else
{
System.out.print(compscram);
System.out.print("");
}
}
}
if (guess.equalsIgnoreCase(word)) {
win = true;
break;
}
  

}

if (win) {
System.out.println(" Yay! You won this round!");
r.won();
} else {
System.out.println("Round over. The correct word was " + word
+ ".");
r.lost();
}

System.out.println("Play again?(y/n)");
String y = input.next();
if (y.equalsIgnoreCase("n"))
play = false;

}
r.saved();
System.out.println("Game over. Here are the results:");
System.out.println(r.toString());

}
}

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Results {

private int rounds, wins, losses = 0;
private PrintWriter pw;
private File file_r;
private Scanner result_reader;

public Results(String s) {
try {
file_r = new File(s);
pw = new PrintWriter(file_r);
result_reader = new Scanner(file_r);
} catch (FileNotFoundException e) {
System.out.println("Can't find file");
}

}

public void won() {
wins++;
rounds++;

}

public void lost() {
losses++;
rounds++;

}

public void saved() {
pw.println("Rounds:" + rounds);
pw.println("Wins:" + wins);
pw.println("Losses:" + losses);
pw.close();

}

public String toString() {

while (result_reader.hasNextLine()) {
System.out.println(result_reader.next());
result_reader.nextLine();
}
return "";

}
}

The output of ScramTest is supposed to look like:

It works fine during normal use but breaks during the special cases.

The exact error is:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Scramble.getRealWord(Scramble.java:28)
at ScramTest.main(ScramTest.java:16)

Explanation / Answer


Answer:
This issue happening because of nextLine() method at line number 16 in Scramble.
Could you please replace the code below
The existing method in Scramble.java:
public String getRealWord() {
if (getword.hasNextLine() && called == true) {
word = getword.next();
getword.nextLine();
} else if (called == false) {
word = word;
} else
word = null;
called = false;
return word;
}
Changed method in Scramble.java
public String getRealWord() {
if (getword.hasNextLine() && called == true) {
word = getword.next();
getword.next();
} else if (called == false) {
word = word;
} else
word = null;
called = false;
return word;
}