I don\'t know how to delete my other questions, but here is my entire code. The
ID: 665907 • Letter: I
Question
I don't know how to delete my other questions, but here is my entire code.
The instructions for the assignment:
1. Set Input File (I have this working)
2. Set Start Address: use JOptionPane.showInputDialog. Seed the input with "0000000" so that it is the default.
3. Set End Address. Follow the instruction for number 2, but seed the input with "EOF" so that it is the default. If an input file has been chosen, seed the input with the size of the file (in hex).
4. Set Output File. ( I have this working too, I think)
5. Dump to File, and Dump to Window. Two separate Menu options.
6. Exit.
Overall, number 2 and number 3 is what I'm having trouble with... Please, please help!!!
public class FileDump {
public static final int READ_THE_FILE = 1;
private JFrame frmFileDump;
private JFileChooser jfc = new JFileChooser();
private File userSelected;
private JScrollPane scrollPane = new JScrollPane();
static JTextArea textArea = new JTextArea();
public static int digitsInAddr = 8;
String StartAdd;
long userStartAdd;
static int userSelectedStart;
String EndAdd;
static int userSelectedEnd;
File outPutFile;
static String line;
public File os;
public String startAdd;
public long startLoc;
public String endAddr;
public long endLoc;
private int length;
public String fName;
public File addr;
private long startAddr;
public String inputStr;
private int jumped;
public static JProgressBar progressBar;
private byte[] bytes;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileDump window = new FileDump();
window.frmFileDump.setVisible(true);
progressBar.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public FileDump() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmFileDump = new JFrame();
frmFileDump.getContentPane().setFont(
new Font("Courier New", Font.PLAIN, 13));
frmFileDump.setFont(new Font("Courier New", Font.PLAIN, 13));
frmFileDump.setName("frmFileDump");
frmFileDump.setTitle("File Dump: ");
frmFileDump.setBounds(100, 100, 578, 469);
frmFileDump.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFileDump.getContentPane().getLayout();
// JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, 562, 410);
frmFileDump.getContentPane().add(scrollPane);
textArea.setFont(new Font("Courier New", Font.PLAIN, 13));
// JTextArea textArea = new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
//Progress Bar
progressBar = new JProgressBar();
progressBar.addFocusListener(new ProgressBarFocusListener(null));
progressBar.setName("ProgressBar");
//when the task of (initially) unknown length begins:
progressBar.setIndeterminate(true);
//do some work; get length of task...
progressBar.setMaximum(length);
progressBar.setValue(0);
progressBar.setIndeterminate(false);
progressBar.setStringPainted(true);
JMenuBar menuBar = new JMenuBar();
frmFileDump.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("File");
menuBar.add(mnNewMenu);
JMenuItem mntmOp = new JMenuItem("Set Input File");
mntmOp.addActionListener(new SetInputActionListener());
mnNewMenu.add(mntmOp);
JMenuItem mntmNewMenuItem = new JMenuItem("Exit");
mntmNewMenuItem.addActionListener(new MntmNewMenuItemActionListener());
JMenuItem mntmNewMenuItem_2 = new JMenuItem("Set Start Address");
mntmNewMenuItem_2.addActionListener(new SetStartAddActionListener());
mnNewMenu.add(mntmNewMenuItem_2);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("Set End Address");
mntmNewMenuItem_3.addActionListener(new SetEndAddActionListener());
mnNewMenu.add(mntmNewMenuItem_3);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("Set Output File");
mntmNewMenuItem_1.addActionListener(new SetOutputActionListener());
mnNewMenu.add(mntmNewMenuItem_1);
JMenuItem mntmDumpToFile = new JMenuItem("Dump to File");
mntmDumpToFile.addActionListener(new DumpToFileActionListener());
mnNewMenu.add(mntmDumpToFile);
JMenuItem mntmDumpToWindow = new JMenuItem("Dump to window");
mntmDumpToWindow.addActionListener(new DumpToWindowActionListener());
mnNewMenu.add(mntmDumpToWindow);
mnNewMenu.add(mntmNewMenuItem);
}
private class MntmNewMenuItemActionListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
}
// set input file
private class SetInputActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int whatHappened = jfc.showOpenDialog(null);
if (whatHappened != JFileChooser.APPROVE_OPTION)
return;
userSelected = jfc.getSelectedFile();
fName = userSelected.getAbsolutePath();
// dealWithFile(userSelected, READ_THE_FILE);
inputStr = fName.toString();
}
}
// set start address
private class SetStartAddActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String start = JOptionPane.showInputDialog("START ADDRESS: ");
try {
startLoc = Long.parseLong(start, 16);
startAddr = Long.parseLong(start);
if (startLoc < addr.length())
JOptionPane.showInputDialog("Too Small");
return;
} catch (Exception err) {
return;
}
}
}
// set end address
private class SetEndAddActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String end = JOptionPane.showInputDialog("END ADDRESS: ");
try {
endLoc = Long.parseLong(end, 16);
} catch (Exception err) {
return;
}
}
}
// dump to window
private class DumpToWindowActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
frmFileDump.setResizable(true); // make GUI resizable
PrintStream console = new PrintStream(new myOutputStream(textArea));
System.setOut(console);
console.println(dealWithFile(jfc.getSelectedFile(), READ_THE_FILE));
}
}
// set output file
private class SetOutputActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser out = new JFileChooser("Output");
int whatHappened = out.showOpenDialog(null);
if (whatHappened != JFileChooser.APPROVE_OPTION)
return;
os = out.getSelectedFile();
}
}
// dump to file
private class DumpToFileActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String path = os.getAbsolutePath() + "dmp"; // get input
try {
outPutFile = new File(path);
if (!outPutFile.exists()) {
outPutFile.createNewFile();
}
PrintStream console = new PrintStream(new File(path));
System.setOut(console);
console.println(dealWithFile(jfc.getSelectedFile(),
READ_THE_FILE));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public PrintStream dealWithFile(File f, int whatToDo) {
if (whatToDo != READ_THE_FILE)
return null;
try {
f = new File(inputStr);
InputStream is = new FileInputStream(f);
DataInputStream dis = new DataInputStream(is);
int aByte = 0;
String addr, hex, ascii;
int digitsInAddr = 8;
boolean atEOF = false;
while (true) {
addr = Long.toString(startAddr, 16);
while (addr.length() < digitsInAddr)
addr = "0" + addr;
startAddr += 16;
startLoc += 0;
endLoc += 0;
hex = " ";
ascii = " ";
jumped = 0;
while (jumped < startLoc) {
jumped += dis.skip(startLoc - jumped);
}
int length = (int) (Math.min(endLoc, 16) - startLoc);
bytes = new byte[length];
int bytesRead = 0;
while (bytesRead < bytes.length) {
bytesRead = dis.read(bytes, bytesRead, bytes.length
- bytesRead);
if (bytesRead == -1) {
break;
}
}
for (int i = 0; i < 16; i++) {
try {
aByte = dis.readByte();
} catch (Exception e) {
atEOF = true;
break;
}
if (aByte < 0)
aByte += 256;
String bs = Integer.toString(aByte, 16);
if (bs.length() == 1)
bs = "0" + bs;
hex += bs + " ";
// if (jumped > 0) {?
// strJumped = " ";
// } else {
// strJumped = "";
// }
if (i == 7)
hex += " ";
char aChar = '.';
if (aByte >= 32 && aByte <= 126)
aChar = (char) aByte;
ascii += aChar;
}
while (hex.length() < 50)
hex += " ";
String line = addr + hex + ascii;
System.out.println(line);
if (atEOF)
break;
}
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
OUTPUT STREAM CLASS:
package fileDump;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
public class myOutputStream extends OutputStream {
private JTextArea textArea;
public myOutputStream(JTextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
textArea.append(String.valueOf((char)b));
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
Explanation / Answer
I have carefully read your code specially number 2
class SetStartAddActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String start = JOptionPane.showInputDialog("START ADDRESS: ");
try {
startLoc = Long.parseLong(start, 16);
startAddr = Long.parseLong(start);
if (startLoc < addr.length())
JOptionPane.showInputDialog("Too Small");
return;
} catch (Exception err) {
return;
}
}
}
and number 3,
class SetEndAddActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String end = JOptionPane.showInputDialog("END ADDRESS: ");
try {
endLoc = Long.parseLong(end, 16);
} catch (Exception err) {
return;
}
}
}
they seem me perfect. you might get NegativeArrayException due to the code is incorrectly de-marshalling the class object due to which the array size may become negative, the compiler at the run time check if the size given for an array length is integer or not and not checking whether this value is negative or positive, so whenever the array size got negative, NegativeArrayException is raised.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.