Scanner/Calling Method Program

Ok, this program compiles, but doesn't really work, I think it is because when the method "input" is called Scanner sn isn't passing the input to String str, any help would be great!

import java.util.Scanner;

import java.lang.*;

class pp

{

publicstaticvoid main(String[] args)

{

Scanner sn =new Scanner(System.in);

int test;

int length;

String str =new String();

instructions();

input(sn, str);

length = str.length();

System.out.println(str +"564" + length);

compute(length, str);

}

staticvoid instructions()

{

System.out.println("Welcome to Palindrome Prover!");

System.out.println("We will now determine if you have a palindrome.");

}

staticvoid input(Scanner sn, String str)

{

System.out.println("Please enter your input now...");

str = sn.next();

System.out.println("Thank You, please hold.");

}

staticvoid compute(int length, String str)

{

char[] tempCharArray =newchar[length];

char[] charArray =newchar[length];

// put original string in an array of chars

for (int y = 0; y < length; y++)

{

tempCharArray[y] = str.charAt(y);

}

// reverse array of chars

for (int x = 0; x < length; x++)

{

charArray[x] = tempCharArray[length - 1 - x];

}

String reversePalindrome =new String(charArray);

System.out.println(reversePalindrome);

}

}

[3154 byte] By [indigopimaca] at [2007-11-26 21:48:36]
# 1

public class CallByValueExample {

public static void main(String[] args) {

String a = "abc";

wrong(a);

System.out.println("after wrong, a = " + a);

a = right();

System.out.println("after right, a = " + a);

}

static void wrong(String s) {

s = "def";

}

static String right() {

return "ghi";

}

}

DrLaszloJamfa at 2007-7-10 3:39:26 > top of Java-index,Java Essentials,Java Programming...
# 2
Suggestion: your code would be more readable if you removed someof those extra blank lines. I shrank your file from 124 lines to 43 lines!If you want people to look at your post, it is worth putting effort into it.
DrLaszloJamfa at 2007-7-10 3:39:26 > top of Java-index,Java Essentials,Java Programming...
# 3

static String input(Scanner sn, String str)

{

System.out.println("Please enter your input now...");

str = sn.next();

System.out.println("Thank You, please hold.");

return str;

}

In the main method

str = input(sn, str);

qUesT_foR_knOwLeDgea at 2007-7-10 3:39:26 > top of Java-index,Java Essentials,Java Programming...
# 4
You need to return the reference for the new String instance from the input method back to the main method and assign it back to str.
qUesT_foR_knOwLeDgea at 2007-7-10 3:39:26 > top of Java-index,Java Essentials,Java Programming...
# 5

No need to pass a string, is there?

static String input(Scanner sn) {

System.out.println("Please enter your input now...");

String str = sn.next();

System.out.println("Thank You, please hold.");

return str;

}

...

str = input(sn);

DrLaszloJamfa at 2007-7-10 3:39:26 > top of Java-index,Java Essentials,Java Programming...
# 6
Hey thanks much!
indigopimaca at 2007-7-10 3:39:27 > top of Java-index,Java Essentials,Java Programming...