10 Replies - 940 Views - Last Post: 12 October 2012 - 10:40 AM Rate Topic: -----

#1 smattison1980  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-September 12

need help with classes and methods

Posted 12 October 2012 - 08:57 AM

Ok, so I am really struggling with this whole thing. I understand the concept of methods but I just can't get this right when i try to write my program. I am also having a hard time figuring out the reading/manipulating the strings. Here is what i am trying to do and then I will post what code i have.

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)

and

public static String trimmed(String str, int len)

and

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:

as entered but trimmed and squeezed.
as entered but trimmed, squeezed, and shortened to 10 characters
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

Grading Notes:

You can do this program however you like (console, GUI, AWT, Swing, Applet, etc.).
If you use an inner classes for the second and third classes, you will receive up to 5 extra points, and if the third class is anonymous, you will receive up to five additional points.
If you make the program graphical, you will receive up to 10 extra points (total won't exceed 100).
You will lose points for poor formatting, poor commenting, not following instructions, program that don't work, etc.
In any case, your total cannot exceed 100 points.
Since this is the 3rd assignment, you are expected to get this right before you turn it in. It will be graded much more rigorously than the earlier assignments and in most cases, you won't get a second chance! If something is confusing in the instructions, ask before submitting!

import java.io.*;
import java.util.*;
public class console
{

//anonymous class to return lower case
public String toLower(final String str) {
return new object(){
public String toString(){
return str.toLowerCase;
}
}
.toString();
}

public static class innerStringManip
{

//trims the white space
public static String trimmed(String str) {
	if (str == null) {
	return "";
}
return str.trim();
}

//trims string to size of len
public static String trimmed(String str, int len);
String retVal = innerStringManip.trimmed(str);
	if (len > retVal.length()) {
	len = retVal.length();
}
return retVal.substring(0, len);
}
//converts double space to single space
public static String squeeze(String str) {
return str.replace("  ", " ");
}
}
//reads string from file and performs required manipulations

public static void main(String[] args)
								throws  FileNotFoundException
Scanner inFile = 
	new scanner(new FileReader("C:\\input.txt"));
	
System.out.print("Trim and squeeze: " + innerStringManip.trimmed(innerStringManip.squeeze(curString));
Scanner.nextLine();

System.out.print("Trim, squeeze, and shortened to 10: " + innerStringManip.trimmed(innerStringManip.squeeze(innerStringManip.trimmed(curString)), 10));
Scanner.nextLine();

System.out.print("Trim, squeeze, shortened to 20, and converted to lower case:" + toLower(innerStringManip.trimmed(innerStringManip.squeeze(innerStringManip.trimmed(cuarString)), 20));
Scanner.nextLine();

inFile.close();

}
}



Is This A Good Question/Topic? 0
  • +

Replies To: need help with classes and methods

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: need help with classes and methods

Posted 12 October 2012 - 09:02 AM

Okay, I've read your assignment. What is your question? What, specifically are you having trouble with?
Was This Post Helpful? 0
  • +
  • -

#3 smattison1980  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-September 12

Re: need help with classes and methods

Posted 12 October 2012 - 09:13 AM

View Postjon.kiparsky, on 12 October 2012 - 09:02 AM, said:

Okay, I've read your assignment. What is your question? What, specifically are you having trouble with?


Sorry, that was indeed pretty vague. When i compile, this is the part the comes out not right.

27	//trims string to size of len
28	public static String trimmed(String str, int len);
29	String retVal = innerStringManip.trimmed(str);
30	    if (len > retVal.length()) {
31	    len = retVal.length();
32	}
33	return retVal.substring(0, len);
34	}



and here are the errors i get. I have stared at this piece of code and can't figure out what i'm doing wrong.

C:\Users\Shawn\Desktop\cst145\console.java:30: error: illegal start of type
if (len > retVal.length()) {
^
C:\Users\Shawn\Desktop\cst145\console.java:30: error: <identifier> expected
if (len > retVal.length()) {
^
C:\Users\Shawn\Desktop\cst145\console.java:30: error: ';' expected
if (len > retVal.length()) {
^
C:\Users\Shawn\Desktop\cst145\console.java:30: error: illegal start of type
if (len > retVal.length()) {
^
C:\Users\Shawn\Desktop\cst145\console.java:30: error: ';' expected
if (len > retVal.length()) {
^
C:\Users\Shawn\Desktop\cst145\console.java:33: error: illegal start of type
return retVal.substring(0, len);
^
C:\Users\Shawn\Desktop\cst145\console.java:33: error: ';' expected
return retVal.substring(0, len);
^
C:\Users\Shawn\Desktop\cst145\console.java:33: error: illegal start of type
return retVal.substring(0, len);
^
C:\Users\Shawn\Desktop\cst145\console.java:33: error: illegal start of type
return retVal.substring(0, len);
^
C:\Users\Shawn\Desktop\cst145\console.java:33: error: <identifier> expected
return retVal.substring(0, len);
^
I'm not asking for someone to do my assignment just point me in the right direction.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: need help with classes and methods

Posted 12 October 2012 - 09:15 AM

public static String trimmed(String str, int len);



The right direction, huh? Okay: look at this line, and sweep your eyes to the right. When you come to the end of the line, you're looking in the right direction. :)
Was This Post Helpful? 0
  • +
  • -

#5 smattison1980  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-September 12

Re: need help with classes and methods

Posted 12 October 2012 - 09:21 AM

View Postjon.kiparsky, on 12 October 2012 - 09:15 AM, said:

public static String trimmed(String str, int len);



The right direction, huh? Okay: look at this line, and sweep your eyes to the right. When you come to the end of the line, you're looking in the right direction. :)



Ahh...the ; needed to be a {.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: need help with classes and methods

Posted 12 October 2012 - 09:27 AM

Bingo. Easy mistake to make.
Was This Post Helpful? 0
  • +
  • -

#7 smattison1980  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-September 12

Re: need help with classes and methods

Posted 12 October 2012 - 09:37 AM

View Postjon.kiparsky, on 12 October 2012 - 09:27 AM, said:

Bingo. Easy mistake to make.


Thank you. I have been looking at this forever and didn't catch that. Any advice on why it won't read my file in?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: need help with classes and methods

Posted 12 October 2012 - 09:45 AM

new scanner(new FileReader("C:\\input.txt"));



First of all, I assume that this file is located here. Second, due to backslash-escaping, "\\" renders as one backslash, so if you want two backslashes you need "\\\\". (if you want the literal string "\n" on your screen, you need to enter it in the code as "\\n" - otherwise you get a newline character)

Finally, here

Scanner.nextLine();



you're calling nextLine as if it were a static method, when you want to call it on the particular Scanner that you set up, which I think you called inFile but I'm too lazy to look.
Was This Post Helpful? 0
  • +
  • -

#9 smattison1980  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-September 12

Re: need help with classes and methods

Posted 12 October 2012 - 09:47 AM

public static void main(String[] args)
								throws  FileNotFoundException
Scanner inFile = 
	new scanner(new FileReader("C:\\input.txt"));
	
System.out.print("Trim and squeeze: " + innerStringManip.trimmed(innerStringManip.squeeze(curString));
Scanner.nextLine();

System.out.print("Trim, squeeze, and shortened to 10: " + innerStringManip.trimmed(innerStringManip.squeeze(innerStringManip.trimmed(curString)), 10));
Scanner.nextLine();

System.out.print("Trim, squeeze, shortened to 20, and converted to lower case:" + toLower(innerStringManip.trimmed(innerStringManip.squeeze(innerStringManip.trimmed(cuarString)), 20));
Scanner.nextLine();

inFile.close();



I apologize for all the questions, I'm pretty new to Java and am having a hard time figuring this out sometimes.
Was This Post Helpful? 0
  • +
  • -

#10 smattison1980  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-September 12

Re: need help with classes and methods

Posted 12 October 2012 - 09:58 AM

View Postjon.kiparsky, on 12 October 2012 - 09:45 AM, said:

new scanner(new FileReader("C:\\input.txt"));



First of all, I assume that this file is located here. Second, due to backslash-escaping, "\\" renders as one backslash, so if you want two backslashes you need "\\\\". (if you want the literal string "\n" on your screen, you need to enter it in the code as "\\n" - otherwise you get a newline character)

Finally, here

Scanner.nextLine();



you're calling nextLine as if it were a static method, when you want to call it on the particular Scanner that you set up, which I think you called inFile but I'm too lazy to look.


if the file I'm calling in is at "C:\input.txt", wouldn't i use \\?
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: need help with classes and methods

Posted 12 October 2012 - 10:40 AM

View Postsmattison1980, on 12 October 2012 - 11:58 AM, said:

if the file I'm calling in is at "C:\input.txt", wouldn't i use \\?


Yes, that would be correct. Sorry, I'm not very familiar with Windows file path conventions. I expected a double backslash at the start of the pathname.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1