Suppose the text file \"data.txt\" contains the following 5 lines of text (there
ID: 3770048 • Letter: S
Question
Suppose the text file "data.txt" contains the following 5 lines of text (there's a newline character immediately after the last character on each line): Assume that the text file "data.txt" is located in the same project directory as the following code. Trace the execution of the following code: Give the value to which each variable, (s theta 1, s theta 2, s theta 3, s theta 4, s05, s theta 6), refers after the code above has finished executing. Use double-quotes around your string values, or use null to indicate that a variable refers to no object.Explanation / Answer
Here is the code for you with comments.
Scanner inScan = IOHelper.createScanner("data.txt");
String s01 = inScan.nextLine(); //nextLine() will scan the next input line from the file into s01. So, s01 will hold the string "Alice was beginning to".
String s02 = inScan.nextLine(); //nextLine() will scan the next input line from the file into s02. So, s02 will hold the string "get very tired of sitting".
String s03 = inScan.next(); //next() will scan the next string from the file into s03. So, s03 will hold the string "by".
String s04 = inScan.next(); //next() will scan the next string from the file into s04. So, s04 will hold the string "her".
s04 = inScan.next(); //next() will scan the next string from the file into s04. So, s04 will hold the string "sister".
String s05 = null; //s05 will be assigned to null.
String s06 = null; //s06 will be assigned to null.
for(int i = 0; i < 3; i++) //This loop will run for 3 values, 0, 1, and 2.
{
if(inScan.hasNext()) //If there is one more line.
{
s05 = inScan.next(); //Read the next word from the file.
s06 = inScan.next(); //Read the next word from the file.
}
}
//When i = 0:
//There is one more line.
//next() will scan the next string from the file into s05. So, s05 will hold the string "on".
//next() will scan the next string from the file into s06. Wo, s06 will hold the string "the".
//When i = 1:
//There is one more line.
//next() will scan the next string from the file into s05. So, s05 will hold the string "bank,".
//next() will scan the next string from the file into s06. So, s06 will hold the string "and".
//When i = 2:
//There is one more line.
//next() will scan the next string from the file into s05. So, s05 will hold the string "of".
//next() will scan the next string from the file into s06. So, s06 will hold the string "having".
System.out.println("s01 = "+s01); //s01 will print the string "Alice was beginning to".
System.out.println("s02 = "+s02); //s02 will print the string "get very tired of sitting."
System.out.println("s03 = "+s03); //s03 will print the string "by".
System.out.println("s04 = "+s04); //s04 will print the string "sister".
System.out.println("s05 = "+s05); //s05 will print the string "of".
System.out.println("s06 = "+s06); //s06 will print the string "having".
inScan.close();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.