dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Problem with Code

  1. #1
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Problem with Code

    Good morning, I am working on an assignment for class. I am fighting with it and cant figure out what i am missing? Any assistance on understanding this would be great. I have attached the assignment and my code is below what i have so far. Thank you so much

    Code:
    import java.io.*;
    
    public class a3mo6252017 {
        BufferedReader in = getReader ("input.txt");   
    
        public static void main(String[] args) {
            StringConversion lowerCaseString = new StringConversion();
            String lowerCase = lowerCaseString.stringConversion("SOME STRING");
        }
    
        private BufferedReader getReader(String inputtxt) {
            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 class Thirdmet {
     //This method will trim the white space from the beginning and end 
        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 = Thirdmet.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, perform manipulations on each string. The results are displayed in the text area
    private void displayManipulatedStrings() throws Exception {
          while(loop)
          {
             //Get the next line in the file
             String curString = s.nextLine();
             //Trim and Squeeze
             System.out.print ("Trim & Squeeze: " + ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)) + "\n");
             //Trim, Squeeze, and Shorten to 10 characters
             System.out.print ("Trim, Squeeze, Shorten to 10: " + ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),10) + "\n");
             //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)) + "\n");
             System.out.print ("\n");
          }
    }
    Attached Files Attached Files
    Last edited by 2kaud; March 13th, 2018 at 09:46 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,508

    Re: Problem with Code

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'. I've amended post #1.]

    Cheers!

    You don't actually say what issues you are experiencing?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.3)

  3. #3
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Problem with Code

    Sorry about that! I am new at using the forum. I have edited my code and i am getting "Build Successful" but not seeing any output? Am i missing it?
    Code:
    import java.io.*;
    
    public class a3mo6252017 {
    
     public static void main (String[] args) {
    //inside you can put some logic
    }
    BufferedReader in = getReader ("input.txt");   
    
       private BufferedReader getReader(String inputtxt) {
            throw new UnsupportedOperationException("Not supported yet."); 
    }
    
    private String toLower(String trimmed) {
    throw new UnsupportedOperationException("Not supported yet."); 
    }
    
        private static class s {
    
            private static String nextLine() {
                throw new UnsupportedOperationException("Not supported yet."); 
            }
    
            public s() {
            }
        }
    
    
        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(" ", " ");

  4. #4
    Join Date
    Feb 2017
    Posts
    368

    Re: Problem with Code

    The execution of a Java program always starts in the main method, but you're not doing anything there.

    Try this to get started,
    Code:
    public static void main (String[] args) {
          //inside you can put some logic
        System.out.println("Hello World");
    }
    Your program should now print Hello World to the console.

    Once you have this up and running use the so called "stepwise refinement" method. Add functionality little by little in small incremental steps, and most importantly, after each change you make sure everything works as intended. Eventually you'll end up with a complete working program because it's been working all the time.
    Last edited by wolle; March 14th, 2018 at 12:00 AM.

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,792

    Re: Problem with Code

    i am getting "Build Successful"
    I don't understand what that means.
    Looking at the code I see several errors, one being the missing }s at the end of the class. You should get errors from the compiler.

    Please post the current version of the code you are working on.

    Note: The code at the end has lost all its indentations making it harder to read and understand. See the first part of the code which does have indentations.
    Norm

  6. #6
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Problem with Code

    I am using Netbeans and when i run the program it says that. I am trying to rework it and see what i can fix

  7. #7
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Problem with Code

    Ugh I think i messed it up even more than it was before.


    Code:
    import java.util.*;
    import java.io.*;
    public class A3MO6252017 {
    
     public static void main(String[] args) throws IOException {
    //inside you can put some logic
    }
    BufferedReader in = getReader ("input.txt")static ; 
        private BufferedReader getReader(String input.txt) ;
        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 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)) + "\n");
          //Trim, Squeeze, and Shorten to 10 characters
        System.out.print ( "Trim, Squeeze, Shorten to 10: " + ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),10) + "\n");
          //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)) + "\n");
        System.out.print ( "\n");
        }
    }
    }

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,792

    Re: Problem with Code

    I think i messed it up even more than it was before
    What happens when you try to compile it?
    If there are error messages, copy the full text and paste it here.
    Norm

  9. #9
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Problem with Code

    These are the errors

    Compiling 1 source file to C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\build\classes
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:15: error: ';' expected
    BufferedReader in = getReader ("input.txt")static ;
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:15: error: illegal start of type
    BufferedReader in = getReader ("input.txt")static ;
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:15: error: ';' expected
    BufferedReader in = getReader ("input.txt")static ;
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:17: error: illegal start of type
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:17: error: ';' expected
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:17: error: invalid method declaration; return type required
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:17: error: illegal start of type
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:21: error: class, interface, or enum expected
    private String toLower(String trimmed) {
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:23: error: class, interface, or enum expected
    }
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:58: error: class, interface, or enum expected
    private void displayManipulatedStrings() throws Exception{
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:66: error: class, interface, or enum expected
    System.out.print ( "Trim, Squeeze, Shorten to 10: " + ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),10) + "\n");
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:68: error: class, interface, or enum expected
    System.out.print ( "Trim, Squeeze, Shorten to 20, lower-case: " + toLower(ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),20)) + "\n");
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:69: error: class, interface, or enum expected
    System.out.print ( "\n");
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:70: error: class, interface, or enum expected
    }
    14 errors
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\nbproject\build-impl.xml:953: The following error occurred while executing this line:
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 0 seconds)

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,792

    Re: Problem with Code

    There are too many errors to work with here.

    I suggest that you start from the beginning.
    Did you get a starting version of the program from your instructor?
    Start with that code without any of your code.
    Compile it to make sure there are no errors.

    When there are no more compiler errors:
    Add a few lines, compile it, fix the errors, compile it again and fix. Continue until no errors.

    Then do it again:
    Add a few lines, compile it, fix the errors, compile it again and fix. Continue until no errors.

    The do it again a small step at a time: add code, compile and fix.

    Try to keep the code properly formatted.
    Last edited by Norm; March 14th, 2018 at 01:06 PM.
    Norm

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,508

    Re: Problem with Code

    I'm not a java programmer, but for the first reported error in the code in post #1 you have

    Code:
    BufferedReader in = getReader ("input.txt");
    which compiled, and in the code in post #6 you have

    Code:
    BufferedReader in = getReader ("input.txt")static ;
    which doesn't.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.3)

  12. #12
    Join Date
    Feb 2017
    Posts
    368

    Re: Problem with Code

    Quote Originally Posted by sunshinehm18 View Post
    Ugh I think i messed it up even more than it was before.
    Why not consider my advice in post #4? You have fallen into the common newbie trap of entering a bigger chunk of code than you can swallow.

    Start a new program with just the main method (that is the Java entry point: public static void main(String[] args)). Make sure it prints Hello World. Then add on code little by little making sure everything works properly after each small change (by putting in print massages to make sure the program flows as intended and that variables hold the expected values). This is not all there is to the Stepwise Refinement program development method but it's the gist of it and it will take you a long way.
    Last edited by wolle; March 15th, 2018 at 03:40 AM.

  13. #13
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Problem with Code

    Thank you both! I put each of my parts in separately and am finding where the problems are locating. One part is here and i cant figure what is missing or if i have it written right. Any suggestions?
    Code:
     public String trimmed(String str, int len){
        String retVal = StrinMan.trimmed(str);
         if (len > retVal.length()){
        len = retVal.length();
           }
           return retVal.substring(0,len);
     }
    Thank you

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,792

    Re: Problem with Code

    i cant figure what is missing or if i have it written right.
    Why do you say that?
    Norm

  15. #15
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Problem with Code

    When i compile i get this error
    Compiling 1 source file to C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\build\classes


    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\src\a3mo6252017\A3MO6252017.java:53: error: non-static method trimmed(String) cannot be referenced from a static context
    String retVal = StrinMan.trimmed(str);


    1 error
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\nbproject\build-impl.xml:953: The following error occurred while executing this line:
    C:\Users\morganh\Documents\NetBeansProjects\A3MO6252017\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 0 seconds)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)