-
March 19th, 2018, 09:33 AM
#16
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
-
March 29th, 2018, 08:57 AM
#17
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");
}
}
}
-
March 29th, 2018, 09:01 AM
#18
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|