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");
}
}
Last edited by 2kaud; March 13th, 2018 at 09:46 AM.
Reason: Added code tags
[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!
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(" ", " ");
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.
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.
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");
}
}
}
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.
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!
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.
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);
}
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)
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.