-1

hi i would like to create a string method,i have to create a readpassword method which returns

the password which is a string so to create stated method is it as such

public void readpassword(String inputpassword) //inputpassword is declared in the main method
{
     (whatever command)
     return inputpassword;
}

is my code right?

Maroun
  • 94,125
  • 30
  • 188
  • 241
sherrez
  • 45
  • 2
  • 8
  • 3
    If you want to return something, then in the method signature you have to put a return type. Your method has `void`, it probably should have `String`. – Eel Lee Oct 22 '13 at 10:59

6 Answers6

3
public String readpassword(String inputpassword)

Use String instead of void

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Hariharan
  • 3,191
  • 4
  • 19
  • 31
2

Use String instead of void in method signature.

public String readpassword(String inputpassword){
  return inputpassword;
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
1

You are misinterpreting the scope of the variable inputpassword and the way you are able to write back values to it. Declared in main, there's no reason to pass inputpassword (wich is empty, I assume) to your readpassword() method. Instead you should read the password from inside the method and return the result.

you'd better do something like this:

 public static String readPassword()
 {
      String passwordReadResult;

      // read the password from wherever it comes, e.g. console
      // ...

      return passwordReadResult;
 }

Update:

I think in your case you don't want do pass inputpassword to your readPassword method (there are cases where this makes sense, but i don't want to add confusion here). You pass something to a method if it needs this data to operate on it. If you have inputpassword declared outside, then it doesn't yet contain a password. So passing it to readpassword makes no sense because readpassword doesn't need an empty password variable! It can read the password let's say from the console but it doesn't need inputpassword for this. If the method is done reading the password, the it return it's value via the return statement. To get your password to be stored into inputpassword in main you do s.t. like this:

String inputpassword = readPassword();
xmoex
  • 2,602
  • 22
  • 36
  • i declared inputpassword as a string in the main method so shouldnt i add a that variable within the curly brackets? when i want to create a readpassword method – sherrez Oct 22 '13 at 11:08
0
public String readpassword(String inputpassword){
    return inputpassword;
}
Lukas Warsitz
  • 1,231
  • 1
  • 11
  • 20
0

update the return type and parameters

The method signature should be

   public String readpassword() //inputpassword is declared in the main method
    {
    String inputpassword;
  // logic
     return inputpassword;
    }
upog
  • 4,965
  • 8
  • 42
  • 81
0

Your function needs to return a string:

public String readpassword(...){

But, it's important not to handle passwords like this. One reason is that strings can find their way into a global string pool that's surprisingly easy to read and, as such, is insecure.

One way round that is to use a character array instead:

public char[] readpassword(...){

See Why is char[] preferred over String for passwords?

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483