HELP WITH MY JAVA PROGRAM Write a program that consists of t

HELP WITH MY JAVA PROGRAM!

Write a program that consists of three classes. The first class will be the actual program.

The second class will simply convert a string to lower case.

The third class will have three methods:

public static String trimmed(String str)

public static String trimmed(String str, int len)

public static String squeeze(String str)

The 1st trimmed method will return str without leading or trailing whitespace and will return an empty string if str is null.

The 2nd trimmed method will return the first len characters after trimming the string. The 2nd method must make use of the 1st method.

The squeeze method will replace all sequences of 2 spaces with 1 space.

The program will read a file containing the data below (cut and paste it into notepad and save it), and will display each line:

1.   as entered but trimmed and squeezed.

2.   as entered but trimmed, squeezed, and shortened to 10 characters

3.   as entered but trimmed, squeezed, converted to lower case, and shortened to 20 characters.

Data (copy after this line down to (not including) the line that says end data, you will have 5 lines):

     This is a test, this is only a test!
This    test    is    a    test   of    your   Java   programming   skills!
xyz
ABCDEFGHIJKLMONPQRSTUVWXYZ1234567890-=_+                         !
end data

Here is my java code: I am not getting any errors but I dont believe it is functioning correctly because I am not getting a output file. Could someone please look it over and see where the mistakes might be.

public static void main (String[] args) {
//inside you can put some logic
}
BufferedReader in = getReader (\"input.txt\");   

private BufferedReader getReader(String usersDylanNetBeansProjectsA3EA3207930inpu) {
throw new UnsupportedOperationException(\"Not supported yet.\"); //To change body of generated methods, choose Tools | Templates.
}

private String toLower(String trimmed) {
throw new UnsupportedOperationException(\"Not supported yet.\"); //To change body of generated methods, choose Tools | Templates.
}

class StringConversion {
private String lowerCaseString = \"\";   
public String stringConversion(String somestring) {
lowerCaseString = somestring.toLowerCase();
return this.lowerCaseString;
}
}

public static class ThirdStringManip{
//This method will trim the white space from the
//beginning and end of the string
public static String trimmed(String s){
if (s == null){
return \"\";
}
return s.trim();
}
//This method will return a trimmed string of size len
public static String trimmed(String s, int len){
String retVal = ThirdStringManip.trimmed(s);
if (len > retVal.length()){
len = retVal.length();
}
return retVal.substring(0,len);
}
//This method will convert all double spaces to a single space
public static String squeeze(String s){
return s.replace(\" \", \" \");
}
}

//This method will read strings from the input file
//and perform manipulations on each string. The results of the
//manipulations are displayed in the text area
private void displayManipulatedStrings() throws Exception{


while (true) {
//Get the next line in the file
String curString = s.nextLine();
  
//Trim and Squeeze
System.out.print ( \"Trim & Squeeze: \" + ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)) + \"\ \");
  
//Trim, Squeeze, and Shorten to 10 characters
System.out.print ( \"Trim, Squeeze, Shorten to 10: \" + ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),10) + \"\ \");
  
//Trim, Squeeze, lower-case and shorten to 20 characters
System.out.print ( \"Trim, Squeeze, Shorten to 20, lower-case: \" + toLower(ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),20)) + \"\ \");
System.out.print ( \"\ \");
}
}
}

Solution

Here is the code for the question described above. Please dont forget to rate the answer if it helped. Thank you very much.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


//A class containing a method to convert a string to lower case
class StringConversion {
   //converts a given string to lower case
   public static String lowerCase(String str)
   {
       return str.toLowerCase();
   }
}

//class containing 3 string functions for trim ,trim and shorten and squeeze operations
class ThirdStringManip {
   // This method will trim the white space from the
   // beginning and end of the string
   public static String trimmed(String s) {
       if (s == null) {
           return \"\";
       }
       return s.trim();
   }

   // This method will return a trimmed string of size len
   public static String trimmed(String s, int len) {
       String retVal = trimmed(s);
       if (len > retVal.length()) {
           len = retVal.length();
       }
       return retVal.substring(0, len);
   }

   // This method will convert all double spaces to a single space
   public static String squeeze(String s) {
       return s.replace(\" \", \" \");
   }
}

public class StringTest {
   public static void main(String[] args) {
       //use a scanner object to read the input file
       Scanner scanner;
       String line;
       try {
           scanner = new Scanner(new File(\"input.txt\"));
       } catch (FileNotFoundException e) {
           System.out.println(\"Could not open input file input.txt\");
           return;
       }
      
      
       //read each line from file and pass it the displayManipulatedString() function to shows the manipulations
       while(scanner.hasNext())
       {
           line=scanner.nextLine();
           displayManipulatedStrings(line);
       }
      
       //close the scanner after finish processing
       scanner.close();
      
   }

     
     

  

  

   // This method will perform manipulations on parameter line. The results of the
   // manipulations are displayed on console
   private static void displayManipulatedStrings(String line) {

      
           // Trim and Squeeze
           System.out.println(\"Trim & Squeeze: \" + ThirdStringManip.squeeze(ThirdStringManip.trimmed(line)));

           // Trim, Squeeze, and Shorten to 10 characters
           System.out.println(\"Trim, Squeeze, Shorten to 10: \"
                   + ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(line)), 10));

           // Trim, Squeeze, lower-case and shorten to 20 characters
           System.out.println(\"Trim, Squeeze, lower-case, Shorten to 20: \"
                   + ThirdStringManip.trimmed(StringConversion.lowerCase(ThirdStringManip.squeeze(ThirdStringManip.trimmed(line))), 20));
           System.out.println(\"_________________________________________\");
      
   }
}

Output of program for input.txt

Trim & Squeeze: This is a test, this is only a test!
Trim, Squeeze, Shorten to 10: This is a
Trim, Squeeze, lower-case, Shorten to 20: this is a test, this
_________________________________________
Trim & Squeeze: This test is a test of your Java programming skills!
Trim, Squeeze, Shorten to 10: This test
Trim, Squeeze, lower-case, Shorten to 20: this test is a t
_________________________________________
Trim & Squeeze: xyz
Trim, Squeeze, Shorten to 10: xyz
Trim, Squeeze, lower-case, Shorten to 20: xyz
_________________________________________
Trim & Squeeze: ABCDEFGHIJKLMONPQRSTUVWXYZ1234567890-=_+ !
Trim, Squeeze, Shorten to 10: ABCDEFGHIJ
Trim, Squeeze, lower-case, Shorten to 20: abcdefghijklmonpqrst
_________________________________________
Trim & Squeeze: end data
Trim, Squeeze, Shorten to 10: end data
Trim, Squeeze, lower-case, Shorten to 20: end data
_________________________________________

HELP WITH MY JAVA PROGRAM! Write a program that consists of three classes. The first class will be the actual program. The second class will simply convert a st
HELP WITH MY JAVA PROGRAM! Write a program that consists of three classes. The first class will be the actual program. The second class will simply convert a st
HELP WITH MY JAVA PROGRAM! Write a program that consists of three classes. The first class will be the actual program. The second class will simply convert a st
HELP WITH MY JAVA PROGRAM! Write a program that consists of three classes. The first class will be the actual program. The second class will simply convert a st

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site