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(); }              } }

Np Ms Office 365/Excel 2016 I Ntermed
1st Edition
ISBN:9781337508841
Author:Carey
Publisher:Carey
Chapter8: Working With Advanced Functions
Section: Chapter Questions
Problem 2.4CP
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();

}

 

 

}

}

 

 

H
27
28
29
30
X31
32
OWNED 6W w w w w w w
33
4567
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
849
CreditCard.java
*Files.java X
try (Fileinputstream
incomingrlle = new flleinputstr
{//create a variable to read the file )
int value = 0;
int count = 0;
while (value= incomingFile.read() ) != -) {
//Print as Hex and ASC11
System.out.printf("%02X ", value); //
System.out.print((char)
count++;
if (count 100)
{
}
(value >= 32 && val
System.out.println ();
}
// Print ASCII character if printable, else '.'
//Print Hex and ASC11 using printf 02X = HEX wit
// increase the count
Transcribed Image Text:H 27 28 29 30 X31 32 OWNED 6W w w w w w w 33 4567 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 849 CreditCard.java *Files.java X try (Fileinputstream incomingrlle = new flleinputstr {//create a variable to read the file ) int value = 0; int count = 0; while (value= incomingFile.read() ) != -) { //Print as Hex and ASC11 System.out.printf("%02X ", value); // System.out.print((char) count++; if (count 100) { } (value >= 32 && val System.out.println (); } // Print ASCII character if printable, else '.' //Print Hex and ASC11 using printf 02X = HEX wit // increase the count
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
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Oracle 12c: SQL
Oracle 12c: SQL
Computer Science
ISBN:
9781305251038
Author:
Joan Casteel
Publisher:
Cengage Learning