Java Assignment questions on Eclops (Need screenshots of output)

User Generated

xngvr88

Computer Science

Description

Q1 Modify the solution below such that it catches and handles the exception if it is thrown. Handle the exception by printing an appropriate message, and then continue processing more strings.


package exception;

public class StringTooLongException extends Exception{

public StringTooLongException(){

super("String too long");

}

}

-----------------------------------------

 Driver class for StringTooLongException

-----------------------------------------

package exception;

import java.util.Scanner;

public class Driver{

public static void main (String[] args)

throws StringTooLongException{

Scanner scan = new Scanner(System.in);

String enterstr;                            //String to be entered

System.out.print("Enter a string (DONE to exit): ");

enterstr = scan.nextLine();

while (!enterstr.equals("DONE")){

if (enterstr.length()>20)

throw new StringTooLongException();

System.out.print("Enter a string (DONE to exit): ");

enterstr = scan.nextLine();

}

}

}


Q2. Modify the DisplayFile program below to add a button labeled Save above the text area. When the button is pushed, write the contents back out to the file. 


import java.util.Scanner; 

import java.io.*; 

import javax.swing.*;

public class DisplayFile {

   //----------------------------------------------------------------   // 

 Opens a file chooser dialog, reads the selected file and   //  loads it into a text area.  

 //----------------------------------------------------------------   

public static void main (String[] args) throws IOException   {   

   JFrame frame = new JFrame ("Display File");    

  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      JTextArea ta = new JTextArea (20, 30);     

 JFileChooser chooser = new JFileChooser();

      int status = chooser.showOpenDialog (null);

      if (status != JFileChooser.APPROVE_OPTION)         

ta.setText ("No File Chosen");     

 else      {         File file = chooser.getSelectedFile();     

           Scanner scan = new Scanner(file);

         String info = "";       

         while (scan.hasNext())           

          info += scan.nextLine() + "\n";

         ta.setText (info);      }

      frame.getContentPane().add (ta);      

frame.pack();      

frame.setVisible(true);

   } 

}


I need the answers to both in 24 hours...Can you get it done..Message me if you have any questions//


Thanks

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer


Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags