Member Avatar for sonicx2218

I can't seem to figure out how to separate the code I wrote into 3 separate methods.
1. The main method, 2. the method that creates the strings i need and sends em back to the main method, 3. and the method that displays the if statement.

import java.io.*;
import java.util.StringTokenizer;

public class Super2
{
	public static void main(String[] args) throws IOException //main method
	{
		String number;
		StringTokenizer st;
		StringBuffer first, middle, last; // 3 separate number groups
		StringBuffer phonenumber = new StringBuffer();

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter phone number using the (xxx) xxx-xxxx format: ");
		number = br.readLine();


		st = new StringTokenizer(number, "  (), -"); //takes out symbols & spaces
		first = new StringBuffer(st.nextToken());
		middle = new StringBuffer(st.nextToken());
		last = new StringBuffer(st.nextToken());
		//combine 3 string buffers
		phonenumber = phonenumber.append(first).append(middle).append(last);
		String str = phonenumber.toString();
		System.out.println(str);

		String reverse = phonenumber.reverse().toString();

		//if statement for comparing reverse
		if (str.equals(reverse))
				System.out.println("The number is a palindrome");
			else
				System.out.println(str + "is not equal to" + reverse);

	}
}

Could anyone help me create the 2 other classes? I'm not sure how to send the data down to the other two methods..I've tried a few different things, but failed miserably every time

Recommended Answers

All 5 Replies

what have you tried so far?

EDIT: having three methods does not mean you need three separate classes.

I can't seem to figure out how to separate the code I wrote into 3 separate methods.
1. The main method, 2. the method that creates the strings i need and sends em back to the main method, 3. and the method that displays the if statement.

import java.io.*;
import java.util.StringTokenizer;

public class Super2
{
	public static void main(String[] args) throws IOException //main method
	{
		String number;
		StringTokenizer st;
		StringBuffer first, middle, last; // 3 separate number groups
		StringBuffer phonenumber = new StringBuffer();

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter phone number using the (xxx) xxx-xxxx format: ");
		number = br.readLine();


		st = new StringTokenizer(number, "  (), -"); //takes out symbols & spaces
		first = new StringBuffer(st.nextToken());
		middle = new StringBuffer(st.nextToken());
		last = new StringBuffer(st.nextToken());
		//combine 3 string buffers
		phonenumber = phonenumber.append(first).append(middle).append(last);
		String str = phonenumber.toString();
		System.out.println(str);

		String reverse = phonenumber.reverse().toString();

		//if statement for comparing reverse
		if (str.equals(reverse))
				System.out.println("The number is a palindrome");
			else
				System.out.println(str + "is not equal to" + reverse);

	}
}

Could anyone help me create the 2 other classes? I'm not sure how to send the data down to the other two methods..I've tried a few different things, but failed miserably every time

before i go any further what do you want classes or methods? pointing to here:Could anyone help me create the 2 other classes?
[EDIT] but as stultuske said it doesnt mean you need more classes, especially in the case of the above code... it would just need 2 methods and main acting as the 3rd 'main' method

Member Avatar for sonicx2218

ooopsy, yea that was a typo, I didn't mean help with classes. Well I've tried making the 2nd method a string method, so I could return the strings to the main method, and then I made the 3rd basically another main method, since I didn't need to return the results, I just left it void. I got a ton of error codes though, way too many, and after like an hour of trying to figure it out, I lost heart and gave up for awhile. Haven't worked with multiple methods in a month or 2, and I've gotten really rusty over that short time.

ooopsy, yea that was a typo, I didn't mean help with classes. Well I've tried making the 2nd method a string method, so I could return the strings to the main method, and then I made the 3rd basically another main method, since I didn't need to return the results, I just left it void. I got a ton of error codes though, way too many, and after like an hour of trying to figure it out, I lost heart and gave up for awhile. Haven't worked with multiple methods in a month or 2, and I've gotten really rusty over that short time.

okay lets take a look... the first piece of your code used to get the actual phone number into a stringbuffer is this right:

String number;
        StringTokenizer st;
        StringBuffer first, middle, last; // 3 separate number groups
        StringBuffer phonenumber = new StringBuffer();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter phone number using the (xxx) xxx-xxxx format: ");
        number = br.readLine();


        st = new StringTokenizer(number, "  (), -"); //takes out symbols & spaces
        first = new StringBuffer(st.nextToken());
        middle = new StringBuffer(st.nextToken());
        last = new StringBuffer(st.nextToken());
        //combine 3 string buffers
        phonenumber = phonenumber.append(first).append(middle).append(last);

now lets think if we put it into a method main thing is we need to return the stringbuffer which holds the phonenumber?
so in java if we want to write a method that returns a certain object we do it like so:

private static StringBuffer getPhoneNum() {
         //other code here 
         return phonenumber;//strng buffer we want to return
         }

as i'm sure you can see the method will now return a stringbuffer 'phonenumber'

The next problem is how do we get the data returned from the method? well easy we declare an object that matches what
the method returns and then we initialize it with the given method like so:

StringBuffer pNum = getPhoneNum();

so the stringbuffer pNum will now hold the data returned from the method...

now for you other method which will encapsulate the code:

String reverse = phonenumber.reverse().toString();
 
		//if statement for comparing reverse
		if (str.equals(reverse))
				System.out.println("The number is a palindrome");
			else
				System.out.println(str + "is not equal to" + reverse);

could be done in many ways but the one thing is we'll always need to parse the stringbuffer which we got in the method getPhoneNum() and lies
in the var pNum, we would also have to pass the unreversed string to the method so it can reverse and check it... so how do we pass data
to methods?:

private static void reverseAndCheckNum(StringBuffer pNum, String numString) {
}

the method would have to be called like this:

reverseAndCheckNum(pNum, str);

where pNum is the stringbuffer we got from our previous method and str is the string representation of it..

well i'm sure thats more then enough to get you on track for now ...
NB you'd have to rename variables in your methods according to the ones passed to it or else ..error!

and then I made the 3rd basically another main method, since I didn't need to return the results

you should have only one main method. off course, you can overload the main method, but it's easier to read, especially if you're not too familiar with java if you don't.

it doesn't need to be a main method, if you don't want to return anything, you should simply declare the return type as 'void'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.