I am trying to print the hexadecimal and then the ASC11, but it is not displaying correct. Can you please review my code nad provided feedback.    Current format :  File contents: C:\Users\shari\OneDrive\Documents\testfile.dat   Hexadecimal:0000000001 ASC11:2 ASC11:3 ASC11:4 ASC11:5 ASC11:6 ASC11:7 ASC11:8 ASC11:9 ASC11:10 Requirment dispaly example : You should display the 10 bytes in hex first, then the ASCII charatter.  For readability, put spacing between the hex and the ASCII.  A example of printing the first 4 lines of a file is shown below: 7c 31 7c 0d 0a 44 75 6d 62 6c     .1...Dumbl 65 64 6f 72 65 7c 41 6c 62 75     edore.Albu 73 7c 48 6f 67 77 61 72 74 73     s.Hogwarts 7c 45 4e 47 7c 77 61 6e 64 7c     .ENG.wand.   Write a program that will use a FileInputStream to read bytes from a file, and display both a hex version of the byte, and it's corresponding ASCII code   package cop2251.fall23.week3.sullivan; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.swing.JFileChooser; import java.io.*;   //a FileInputStream to read bytes from a file, and display both a hex version of the byte, //and it's corresponding ASCII code (if it is a printable character). public class Files{   public static void main(String arpgs[]) {         JFileChooser fc = new JFileChooser();     int returnVal = fc.showOpenDialog(null);       if (returnVal == JFileChooser.APPROVE_OPTION) {         File file = fc.getSelectedFile();         System.out.println("File contents: " + file.getAbsolutePath());         System.out.println();         showContents(file);     } }//Input File is read in binary and output files print in Hex and ASC11 private static void showContents(File file){          try (  //Create an input file stream using try/catch block. This will automate the closing of the file.  //FileInputStream Reads files  FileInputStream incomingFile = new FileInputStream (file); ) {//create a variable to read the file          int valueH; // values from the file to Hex         int valueA;//values to hold ASC11         while ((valueH = incomingFile.read()) != -1) //read the file as long as it is not the end of the file.          {//Print Hex         // /You should display the 10 bytes in hex first, then the ASCII charatter.          System.out.println(String.format("Hexadecimal:" +  "%10s", Integer.toHexString(valueH)).replace(' ', '0'));          //Print Hex to ASC11          while ((valueA = incomingFile.read()) != -1) //read the file as long as it is not the end of the file.          {//Print Hex           System.out.println(String.format("ASC11:" + Integer.toString(valueA)));         }            //Print ASC11         // System.out.println((char)value);         }         //temp varaible outside prinnt stagtement > covert to bi > use print statement > outside print statement Convert tp ASC11> Print statement  check to   //Try/Catch Block closes the file.  } catch (IOException e) {   e.getMessage(); }              } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%
I am trying to print the hexadecimal and then the ASC11, but it is not displaying correct. Can you please review my code nad provided feedback. 
 
Current format : 

File contents: C:\Users\shari\OneDrive\Documents\testfile.dat

 

Hexadecimal:0000000001

ASC11:2

ASC11:3

ASC11:4

ASC11:5

ASC11:6

ASC11:7

ASC11:8

ASC11:9

ASC11:10

Requirment dispaly example :

You should display the 10 bytes in hex first, then the ASCII charatter.  For readability, put spacing between the hex and the ASCII.  A example of printing the first 4 lines of a file is shown below:

7c 31 7c 0d 0a 44 75 6d 62 6c     .1...Dumbl
65 64 6f 72 65 7c 41 6c 62 75     edore.Albu
73 7c 48 6f 67 77 61 72 74 73     s.Hogwarts
7c 45 4e 47 7c 77 61 6e 64 7c     .ENG.wand.

 
Write a program that will use a FileInputStream to read bytes from a file, and display both a hex version of the byte, and it's corresponding ASCII code
 
package cop2251.fall23.week3.sullivan;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import java.io.*;
 
//a FileInputStream to read bytes from a file, and display both a hex version of the byte,
//and it's corresponding ASCII code (if it is a printable character).
public class Files{
 
public static void main(String arpgs[]) {    
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(null);
 
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        System.out.println("File contents: " + file.getAbsolutePath());
        System.out.println();
        showContents(file);
    }
}//Input File is read in binary and output files print in Hex and ASC11
private static void showContents(File file){
        
try (  //Create an input file stream using try/catch block. This will automate the closing of the file. 
//FileInputStream Reads files 
FileInputStream incomingFile = new FileInputStream (file); )
{//create a variable to read the file 
        int valueH; // values from the file to Hex
        int valueA;//values to hold ASC11
        while ((valueH = incomingFile.read()) != -1) //read the file as long as it is not the end of the file. 
        {//Print Hex 
       // /You should display the 10 bytes in hex first, then the ASCII charatter.
         System.out.println(String.format("Hexadecimal:" +  "%10s", Integer.toHexString(valueH)).replace(' ', '0'));
         //Print Hex to ASC11
         while ((valueA = incomingFile.read()) != -1) //read the file as long as it is not the end of the file. 
        {//Print Hex 
         System.out.println(String.format("ASC11:" + Integer.toString(valueA)));
        }
 
         //Print ASC11
        // System.out.println((char)value);
        }
        //temp varaible outside prinnt stagtement > covert to bi > use print statement > outside print statement Convert tp ASC11> Print statement  check to
  //Try/Catch Block closes the file. 
} catch (IOException e) {
 
e.getMessage();
}
  
 
        }
}
 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I am getting an error at my while statement. It says Type mismatch and wants me to change the vaule type to bollean vs int. But if I do that the greater than function does't work. Do you see any errors in my code ?

 

public class Files{

 

public static void main(String arpgs[]) {

JFileChooser fc = new JFileChooser();

intreturnVal = fc.showOpenDialog(null);

 

if (returnVal == JFileChooser.APPROVE_OPTION) {

File file = fc.getSelectedFile();

System.out.println("File contents: " + file.getAbsolutePath());

System.out.println();

showContents(file);

}

}//Input File is read in binary and output files print in Hex and ASC11

private static void showContents(File file){

//Create an input file stream using try/catch block. This will automate the closing of the file.

//FileInputStream Reads files

try ( FileInputStream incomingFile = new FileInputStream (file))

{//create a variable to read the file )

intvalue =0;

intcount = 0;

 

while (value= incomingFile.read())!= -1){

 

//Print as Hex and ASC11

System.out.printf("%02X ", value); //

 

System.out.print((char) (value >= 32 && value <= 126 ? value : '.'));

count++;

 

if(count % 10 ==0)

{

System.out.println();

}

}

// Print ASCII character if printable, else '.'

 

 

//Print Hex and ASC11 using printf %02X = HEX with leading zeros

 

// increase the count

 

//Try/Catch Block closes the file.

} catch (IOException e) {

//print error message

e.getMessage();

}

 

 

}

}

 

 

```java
try ( FileInputStream incomingFile = new FileInputStream(args[0])) {
    // (create a variable to read the file )
    int value = 0;
    int count = 0;

    while ((value = incomingFile.read()) != -1) {

        // Print as Hex and ASCII
        System.out.printf("%02X ", value); // Print Hex value

        System.out.print((char) (value >= 32 && value <= 126 ? value : '.')); // Print ASCII character if printable, else '.'

        count++;

        if (count % 10 == 0) {
            System.out.println();
        }
    }
}
```

### Explanation:

- **Lines 27-28**: A `FileInputStream` is created to read a file specified by the first command-line argument. Two variables, `value` and `count`, are initialized to zero.

- **Line 30**: A `while` loop is used to read bytes from the file until the end is reached (`read()` returns -1).

- **Line 34**: For each byte value read, it is printed in hexadecimal format using `System.out.printf("%02X ", value);`.

- **Line 36**: The program checks if the byte value corresponds to a printable ASCII character (between 32 and 126). If it is printable, it prints the character; otherwise, it prints a period (`'.'`).

- **Line 38**: The `count` variable is incremented with each iteration.

- **Lines 40-42**: Every ten characters, a newline is printed to format the output neatly.

This program provides a simple hex and ASCII dump of the content of a file, formatting the output in columns for easier reading.
Transcribed Image Text:```java try ( FileInputStream incomingFile = new FileInputStream(args[0])) { // (create a variable to read the file ) int value = 0; int count = 0; while ((value = incomingFile.read()) != -1) { // Print as Hex and ASCII System.out.printf("%02X ", value); // Print Hex value System.out.print((char) (value >= 32 && value <= 126 ? value : '.')); // Print ASCII character if printable, else '.' count++; if (count % 10 == 0) { System.out.println(); } } } ``` ### Explanation: - **Lines 27-28**: A `FileInputStream` is created to read a file specified by the first command-line argument. Two variables, `value` and `count`, are initialized to zero. - **Line 30**: A `while` loop is used to read bytes from the file until the end is reached (`read()` returns -1). - **Line 34**: For each byte value read, it is printed in hexadecimal format using `System.out.printf("%02X ", value);`. - **Line 36**: The program checks if the byte value corresponds to a printable ASCII character (between 32 and 126). If it is printable, it prints the character; otherwise, it prints a period (`'.'`). - **Line 38**: The `count` variable is incremented with each iteration. - **Lines 40-42**: Every ten characters, a newline is printed to format the output neatly. This program provides a simple hex and ASCII dump of the content of a file, formatting the output in columns for easier reading.
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education