CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Problem with Code

    non-static method trimmed(String) cannot be referenced from a static context
    Change the definition of trimmed() to be static so that it can be called from a static method.
    Norm

  2. #17
    Join Date
    Mar 2018
    Location
    Delaware
    Posts
    10

    Re: Problem with Code

    Woohoo almost figured this out but stuck on the bottom of code.

    Code:
     while(loop)
                {
                 //Get the next line in the file
                    String curString = s.nextLine();
                    //Trim and Squeeze
                    System.out.print ( "Trim & Squeeze: " + ThreeMethods.squeeze(ThreeMethods.trimmed(curString)) + "\n");
                    //Trim, Squeeze, and Shorten to 10 characters
                    System.out.print ( "Trim, Squeeze, Shorten to 10: " + ThreeMethods.trimmed(ThreeMethods.squeeze(ThreeMethods.trimmed(curString)),10) + "\n");
                    //Trim, Squeeze, lower-case and shorten to 20 characters
                    System.out.print ( "Trim, Squeeze, Shorten to 20, lower-case: " + ThreeMethods.trimmed(ThreeMethods.trimmed(ThreeMethods.squeeze(ThreeMethods.trimmed(curString)),20)) + "\n");
                    System.out.print ( "\n");
                }
    What am i missing??
    Error I am getting is this - error: cannot find symbol
    String curString = s.nextLine();
    symbol: variable s
    location: class A3MO6252017

    My brain is in a fog right now from looking at it so much. Any help would be great.
    Thank you!

    Full code below
    Code:
    mport java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    /**
     *
     * @author morganh
     */
    public class A3MO6252017 
    {
        /**
         * @param args the command line arguments
         * @throws java.io.FileNotFoundException
         */
        public static void main(String[] args) 
                                throws FileNotFoundException, IOException
        {   
            FileReader inputStream = new FileReader("input.txt");
    
            class StringConversion
            {
                private String lowerCase = "";
                
                public String stringConversion(String doc)
                    {
                    lowerCase = doc.toLowerCase();
                    return this.lowerCase;
                    }
            }
        }
           
           public static class ThreeMethods
        {
         //white space trimmed
         
                public static String trimmed(String s)
                {
                    if (s == null)
                    {
                        return "";
                    }
                    return s.trim();
                }
            //will return a trimmed string of size len
            public static String trimmed(String s, int len)
            {
                String retVal = ThreeMethods.trimmed(s);
                if (len > retVal.length())
                {
                    len = retVal.length();
                }
                return retVal.substring(0,len);
            }
            //convert  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(boolean loop) throws Exception{
    //
                while(loop)
                {
                 //Get the next line in the file
                    String curString = s.nextLine();
                    //Trim and Squeeze
                    System.out.print ( "Trim & Squeeze: " + ThreeMethods.squeeze(ThreeMethods.trimmed(curString)) + "\n");
                    //Trim, Squeeze, and Shorten to 10 characters
                    System.out.print ( "Trim, Squeeze, Shorten to 10: " + ThreeMethods.trimmed(ThreeMethods.squeeze(ThreeMethods.trimmed(curString)),10) + "\n");
                    //Trim, Squeeze, lower-case and shorten to 20 characters
                    System.out.print ( "Trim, Squeeze, Shorten to 20, lower-case: " + ThreeMethods.trimmed(ThreeMethods.trimmed(ThreeMethods.squeeze(ThreeMethods.trimmed(curString)),20)) + "\n");
                    System.out.print ( "\n");
                }
            }
     }

  3. #18
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Problem with Code

    error: cannot find symbol
    String curString = s.nextLine();
    symbol: variable s
    The compiler can not find a definition for the variable: s that is in scope where it is used.

    Where is s defined?
    Norm

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured