Question - String Reversal

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 418 Views - Last Post: 05 April 2011 - 04:06 AM Rate Topic: -----

#1 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Question - String Reversal

Posted 04 April 2011 - 08:58 PM

I know there are a million topics on string reversal and I read through all of them, however, I'm still having an issue with my code.
I'm attempting to call "s" in my main.

Main:
else if (op.equals("rev")) {
                s = rev(scan.nextLine());
                System.out.println(s);


public static String rev(String nextLine) {
	String s = scan.nextLine;
	return (new StringBuffer(s)).reverse().toString();
} 


It works fine if I declare s as a string like
 String s = "hello";
but not if I try to use scanner.

I'm getting an error for "scan" in my method though.

Thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: Question - String Reversal

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Question - String Reversal

Posted 04 April 2011 - 09:05 PM

Post the error message(s) as they appear on your compiler.
Was This Post Helpful? 0
  • +
  • -

#3 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Re: Question - String Reversal

Posted 04 April 2011 - 09:11 PM

Eclipse says "scan cannot be resolved"
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Question - String Reversal

Posted 04 April 2011 - 09:12 PM

Did you import java.util.Scanner, as well as declare a Scanner variable named scan? If not, do such. If you did, then post your entire code, including the line numbers the error is occuring at. Otherwise, we are the blind leading the blind.
Was This Post Helpful? 0
  • +
  • -

#5 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Re: Question - String Reversal

Posted 04 April 2011 - 09:14 PM

Yes, scanner is imported.

Here is my code:

import java.util.Scanner;

public class Methods 
{

public static void main(String[] args) 
	{
        Scanner scan = new Scanner(System.in);
        int [] a;
        boolean b;
        int n;
        double d;
        String s;

        for (;;)/> {
            System.out.println("Enter command and arguments");
            String op = scan.next();
            if (op.equals("quit"))
                break;
            else if (op.equals("add")) {
                d = add(scan.nextDouble(), scan.nextDouble());
                System.out.println(d);
            }
            else if (op.equals("max")) {
                d = max(scan.nextDouble(), scan.nextDouble());
                System.out.println(d);
            }
            else if (op.equals("pi")) {
                d = pi();
                System.out.println(d);
            }
            else if (op.equals("even")) {
                b = even(scan.nextInt());
                System.out.println(B)/>;
            }
            else if (op.equals("rev")) {
                s = rev(scan.nextLine());
                System.out.println(s);
            }
            else {
                System.out.println("Invalid command: " + op);
            }
            
        }
    }

public static String rev(String nextLine) {
	String s = scan.nextLine;
	return (new StringBuffer()).reverse().toString();
}

private static boolean even(int nextInt) {
	if (nextInt % 2 != 0)
        return false;
    else 
	return true;
}

private static double pi() {
	double pi = 3.14159;
	return pi;
}

public static double max(double nextDouble, double nextDouble2) {
	return Math.max(nextDouble, nextDouble2);

}

public static double add(double nextDouble, double nextDouble2) {
	double d = nextDouble + nextDouble2;
	return d;
}


}



The error is occurring at line 48.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Question - String Reversal

Posted 04 April 2011 - 09:16 PM

In your reverse method, the error is at this line String s = scan.nextLine;. A few things here. First, the method is nextLine()- it needs to have the indicative parentheses for a method. Second, the method already accepts a param for the String to reverse. The rev() method should not handle user input. Lastly, The scan variable was declared in the main() method. As such, it is local to the main() method and cannot be accessed outside of it.
Was This Post Helpful? 1
  • +
  • -

#7 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Re: Question - String Reversal

Posted 04 April 2011 - 09:20 PM

View Postmacosxnerd101, on 04 April 2011 - 09:16 PM, said:

In your reverse method, the error is at this line String s = scan.nextLine;. A few things here. First, the method is nextLine()- it needs to have the indicative parentheses for a method. Second, the method already accepts a param for the String to reverse. The rev() method should not handle user input. Lastly, The scan variable was declared in the main() method. As such, it is local to the main() method and cannot be accessed outside of it.


So are you saying that I do not need
 String s = scan.nextLine(); 


And should just use
 return (new StringBuffer()).reverse().toString(); 


If I do that, nothing is returned.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Question - String Reversal

Posted 04 April 2011 - 09:20 PM

The rev() method accepts a String parameter. Pass that to the StringBuffer.
Was This Post Helpful? 0
  • +
  • -

#9 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Re: Question - String Reversal

Posted 04 April 2011 - 09:26 PM

Time for me to be a PITA again.

So where do I put rev()?
Anywhere I tried to place it in my code causes an error.

All of these get some sort of an error:

 return (new StringBuffer(rev())).reverse().toString(); 

 return (new StringBuffer()).reverse(rev()).toString(); 

 return (new StringBuffer()).reverse().toString(rev()); 

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Question - String Reversal

Posted 04 April 2011 - 09:30 PM

Look at your rev() method. See the param in the method, the String named nextLine. Pass that to the StringBuffer's constructor.
public static String rev(String nextLine) {  
    return (new StringBuffer()).reverse().toString();  
}  




The reason you are getting errors (which it would be nice if you told us what errors you encounter in your posts) is b/c you are trying to invoke a method named rev() which does not accept any params, and that method does not exist.
Was This Post Helpful? 0
  • +
  • -

#11 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Re: Question - String Reversal

Posted 04 April 2011 - 09:39 PM

If I change it to
 return (new StringBuffer(nextLine)).reverse().toString(); 

then it doesn't return anything. I can't figure out how to assign it to s so that it returns properly.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Question - String Reversal

Posted 04 April 2011 - 09:41 PM

Post all your code. And explain how you know it isn't returning anything. You HAVE to be more specific in your description of your problems and errors.
Was This Post Helpful? 0
  • +
  • -

#13 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Re: Question - String Reversal

Posted 04 April 2011 - 09:43 PM

Sorry, thought I had posted all of my code earlier.

import java.util.Scanner;

public class Methods 
{

public static void main(String[] args) 
	{
        Scanner scan = new Scanner(System.in);
        int [] a;
        boolean b;
        int n;
        double d;
        String s;

        for (;;)/> {
            System.out.println("Enter command and arguments");
            String op = scan.next();
            if (op.equals("quit"))
                break;
            else if (op.equals("add")) {
                d = add(scan.nextDouble(), scan.nextDouble());
                System.out.println(d);
            }
            else if (op.equals("max")) {
                d = max(scan.nextDouble(), scan.nextDouble());
                System.out.println(d);
            }
            else if (op.equals("pi")) {
                d = pi();
                System.out.println(d);
            }
            else if (op.equals("even")) {
                b = even(scan.nextInt());
                System.out.println(B)/>;
            }
            else if (op.equals("rev")) {
                s = rev(scan.nextLine());
                System.out.println(s);
            }
            else {
                System.out.println("Invalid command: " + op);
            }
            
        }
    }

public static String rev(String nextLine) {

	return (new StringBuffer(nextLine)).reverse().toString();
}

private static boolean even(int nextInt) {
	if (nextInt % 2 != 0)
        return false;
    else 
	return true;
}

private static double pi() {
	double pi = 3.14159;
	return pi;
}

public static double max(double nextDouble, double nextDouble2) {
	return Math.max(nextDouble, nextDouble2);

}

public static double add(double nextDouble, double nextDouble2) {
	double d = nextDouble + nextDouble2;
	return d;
}


}



Basically, I say that it isn't returning anything because it isn't printing anything from
else if (op.equals("rev")) {
                s = rev(scan.nextLine());
                System.out.println(s);


Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: Question - String Reversal

Posted 04 April 2011 - 09:45 PM

I ran your code for the rev() method, and this is what I got:

Quote

Enter command and arguments
rev Test
tseT

Was This Post Helpful? 1
  • +
  • -

#15 KeithStone  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 50
  • Joined: 29-March 11

Re: Question - String Reversal

Posted 04 April 2011 - 09:47 PM

Wow, I'm an idiot. I was hitting enter before entering the string. Thank you very much
:stupid:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2