Can someone help me figure out what I am doing wrong here. I think I may be running my head into this way to many times to see what is wrong with it. The error code is posted below.

import java.util.Scanner;
public class userName {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        getNames(firstName, lastName);
		userPassword(firstName, lastName);//function call
	
	}
    public static void getNames(String firstName, String lastName){
		Scanner kbd = new Scanner(System.in);//Creates new scanner
        System.out.println("Please enter your first and middle names.");//prompts user for input
		firstName = kbd.nextLine();//first and middle names are entered here
		System.out.println("Please enter your last name.");//prompts user for input
		lastName = kbd.nextLine();//last name is entered here
    }
	
    public static void userPassword(String firstName, String lastName){
    	int x = 1;//used to print out the first letter of the first name
    	int y = 4;//used to print out the first four letters of the last name 
    	//System.out.println("Your first and middle names are " + firstName);//test to see if firstName was passed correctly
    	//System.out.println(firstName.substring(0,x));//test to see if the print out is abbreviated
    	//System.out.println("Your last name is " + lastName);//test to see if lastName was passed correctly
    	//System.out.println(lastName.substring(0,y));//test to see if the print out is abbreviated
    	System.out.println("Your user password is " + firstName.substring(0,x) + lastName.substring(0,y));
    	
    }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
firstName cannot be resolved to a variable
lastName cannot be resolved to a variable
firstName cannot be resolved to a variable
lastName cannot be resolved to a variable

Edited 5 Years Ago by jmcorpse: n/a

The errors are pretty clear. You can't use the variables firstName and lastName in main() if you haven't declared them.

You are taking inputs from the user in getNames()..
so you should not pass parameters to that method.

you can check this code,

import java.util.Scanner;

public class GetUserPasswd {

public static String firstName, lastName; 	// Declare your firstName & lastName

/**
#
* @param args
#
*/

public static void main(String[] args) {

// TODO Auto-generated method stub

setNames();	// to set firstName & lastName --- Dont pass parameters here

setuserPassword(firstName, lastName);//function call

 

}

public static void setNames(){ 		// Sets firstName & lastName

Scanner kbd = new Scanner(System.in);//Creates new scanner

System.out.println("Please enter your first and middle names.");//prompts user for input

firstName = kbd.nextLine();//first and middle names are entered here

System.out.println("Please enter your last name.");//prompts user for input

lastName = kbd.nextLine();//last name is entered here
}

public static void setuserPassword(String firstName, String lastName){
int x = 1;//used to print out the first letter of the first name
int y = 4;//used to print out the first four letters of the last name
//System.out.println("Your first and middle names are " + firstName);//test to see if firstName was passed correctly
//System.out.println(firstName.substring(0,x));//test to see if the print out is abbreviated
//System.out.println("Your last name is " + lastName);//test to see if lastName was passed correctly
//System.out.println(lastName.substring(0,y));//test to see if the print out is abbreviated
System.out.println("Your user password is " + firstName.substring(0,x) + lastName.substring(0,y));
}

}

It outputs --

Please enter your first and middle names.
Sourabh
Please enter your last name.
Chakraborty
Your user password is SChak

import java.util.Scanner;
   
      public class userName {
  static Scanner kbd = new Scanner(System.in);//Creates new scanner
  
     static  String firstName,lastName;  
   
      /**
   
      * @param args
   
      */
   
      public static void main(String[] args) {
   
      // TODO Auto-generated method stub
  
      getNames(firstName, lastName);
  
  
  
       
  
      }
  
      public static void getNames(String firstName, String lastName){
  
      
      System.out.println("Please enter your first and middle names.");//prompts user for input
  
      firstName = kbd.nextLine();//first and middle names are entered here
  
      System.out.println("Please enter your last name.");//prompts user for input
  
      lastName = kbd.nextLine();//last name is entered here
  
     
  
      int x = 1;//used to print out the first letter of the first name
  
      int y = 4;//used to print out the first four letters of the last name
  
      //System.out.println("Your first and middle names are " + firstName);//test to see if firstName was passed correctly
  
      //System.out.println(firstName.substring(0,x));//test to see if the print out is abbreviated
  
      //System.out.println("Your last name is " + lastName);//test to see if lastName was passed correctly
  
      //System.out.println(lastName.substring(0,y));//test to see if the print out is abbreviated
  
      System.out.println("Your user password is " + firstName.substring(0,x) + lastName.substring(0,y));
  
       
  
      }
  
      }

use the code above it works perfectly well and will suit your demand and at the same tym learn from ur little mistake...Thanks

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Actually both of the code "solutions" above are bad and I wouldn't recommend either one of them.

Also please note that we do not encourage just completing someone's homework for them here. Offer advice, examples, and code fragments rather than simply fixing and re-posting their code with no explanation.

Edited 5 Years Ago by Ezzaral: n/a

Thanks for your help. But I figured it out right after you posted Ezzaral. Thanks for the help.

This question has already been answered. Start a new discussion instead.