Evening Chegg. I have a list of Java questions I need answers to. Thank you for
ID: 3568169 • Letter: E
Question
Evening Chegg. I have a list of Java questions I need answers to. Thank you for your time, skill and perseverance.
There are 6 questions in all, and while I know I will only get better at java by practicing java, I have two other things I need to be doing. If you can, would you kindly leave comment on your code, just to help the learning process. Thank you in advance.
11.10.3 Fill in Code:
24. This code segment reads a file named datap.txt that contains one data item per line, and outputs only the data items that are integers. Hint: You may need to use nested try and catch blocks.
Given Code:
[
String s = "" ;
int n = 0;
try
{
Scanner file = new Scanner( new File( "data.txt" ) );
while ( file.hasNext ( ) )
{
s = file.nextLine ( );
// your code goes here
}
}
]
My Code:
[
]
For Questions 26-29, you should assume that the file dataq.txt contains the
following:
Java
Illuminate:
Programning
Is Not A
Spectator
Sport
28. This code sequence reads the file dataq.txt, concatenates all the lines
with a space between them, and out puts them as:
Java Illuminated: Programming Is Not A Spectator Sport
Given Code:
[
try
{
Scanner file = new Scanner( new File( "dataq.txt" ) );
String result = "";
// your code goes here
}
catch ( IOException ioe )
{
ioe.printStackTrace ( );
}
]
My Code:
[
]
30. This code sequence loops through the array grades and writes all its
elements into the file datag.txt, one per line:
Given Code:
[
int [ ] grades = { 98, 76, 82 , 90, 1OO, 75 };
try
{
FileOutputStream fos = new FileOutputStream( "datag.txt", false);
// your code goes here
}
// and your code contines here
]
My Code:
[
]
32. This code sequence writes the values of the variables i and d to the file
dataf.txt, one line at a time.
Given Code:
[
int i = 45;
double d = 6.7;
try
{
fileOutputStream fos = new FileOutputStream( "dataf.txt", false );
PrintWriter pw = new PrintWriter ( fos );
// your code goes here
catch ( IOExceptlon ioe )
{
ioe.printStackTrace( );
}
]
My Code:
[
]
11.10.5 Debugging Area:
39.
Given Code:
[
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Test
{
public static void main (String [ ] args )
{
String stringRead;
try
{
Scanner file = new Scanner ( new File ( "data.txt" ) );
stringRead = file.nextline ( );
}
catch ( IOException ioe )
{
ioe.printStackTrace ( );
}
System.out.println( "string read: " + stringRead );
// line above is line 21
}
}
Compile Error:
Test.java:21: variable stringRead might not have been initialized
error at line 21
]
Debugging:
40.
Given Code:
[
Scanner parse = new Scanner ( "1 3.5 6" );
try
{
while ( parse.hasNext ( ))
{
int number = parse.nextlnt ( );
System.out.println( number );
}
}
catch( InputMismatchException e )
{
System.out.println( "In catch block" );
}
Compile Error:
The code compiles and runs, but the result is:
1
In catch block
Explain why and fix
]
Debugging:
Explanation / Answer
11.10.3 Fill in Code:
24. This code segment reads a file named datap.txt that contains one data item per line, and outputs only the data items that are integers. Hint: You may need to use nested try and catch blocks.
Given Code:
[
String s = "" ;
int n = 0;
try
{
Scanner file = new Scanner( new File( "data.txt" ) );
while ( file.hasNext ( ) )
{
s = file.nextLine ( );
try{
int value = Integer.parseInt(s);
System.out.println(value);
}
catch(NumberFormatException e){
}
}
}
]
For Questions 26-29, you should assume that the file dataq.txt contains the
following:
Java
Illuminate:
Programning
Is Not A
Spectator
Sport
28. This code sequence reads the file dataq.txt, concatenates all the lines
with a space between them, and out puts them as:
Java Illuminated: Programming Is Not A Spectator Sport
Given Code:
[
try
{
Scanner file = new Scanner( new File( "dataq.txt" ) );
String result = "";
while(file.hasNext())
result = result + file.nextLint() + " ";
}
catch ( IOException ioe )
{
ioe.printStackTrace ( );
}
System.out.println(result);
]
30. This code sequence loops through the array grades and writes all its
elements into the file datag.txt, one per line:
Given Code:
[
int [ ] grades = { 98, 76, 82 , 90, 1OO, 75 };
try
{
FileOutputStream fos = new FileOutputStream( "datag.txt", false);
PrintWriter pw = new PrintWriter ( fos );
for(int i=0; i<grades.length; i++)
pw.println(grades[i]);
}
catch(IOExceptlon ioe )
{
ioe.printStackTrace( );
}
]
32. This code sequence writes the values of the variables i and d to the file
dataf.txt, one line at a time.
Given Code:
[
int i = 45;
double d = 6.7;
try
{
fileOutputStream fos = new FileOutputStream( "dataf.txt", false );
PrintWriter pw = new PrintWriter ( fos );
pw.println(i);
pw.println(d);
}
catch ( IOExceptlon ioe )
{
ioe.printStackTrace( );
}
]
11.10.5 Debugging Area:
39.
Given Code:
[
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Test
{
public static void main (String [ ] args )
{
String stringRead = "";
try
{
Scanner file = new Scanner ( new File ( "data.txt" ) );
stringRead = file.nextline ( );
}
catch ( IOException ioe )
{
ioe.printStackTrace ( );
}
System.out.println( "string read: " + stringRead );
// line above is line 21
}
}
Debugging:
40.
Given Code:
[
Scanner parse = new Scanner ( "1 3.5 6" );
try
{
while ( parse.hasNext ( ))
{
int number = parse.nextlnt ( );
System.out.println( number );
}
}
catch( InputMismatchException e )
{
System.out.println( "In catch block" );
}
this is because second one is 3.5 which not a integer. it throws InputMismatchException
results in "In catch block"
fix is replace line Scanner parse = new Scanner ( "1 3.5 6" ); with
Scanner parse = new Scanner ( "1 3 5 6" );
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.