1.11M Members

printing Names

 
0
 

I'm having trouble figuring out a few different methods that I'm putting into a constructor. Any chance for advice?
I'm having trouble with the last 3 near the bottom. I want to compare two name variable to each other to see if they are the same regardless of case, then I want to take just the first initial from each name that the user inputs, and last I want to count the amount of characters not counting spaces. The user inputs their name as First Middle Last. I am lost.
Thanks in advance.

/* import the Scanner class */  
import java.util.Scanner;  

class Name {  

    String first, middle, last;
    public Name(){

        Scanner scanner = new Scanner(System.in);  

        System.out.print("Enter your first name: ");  //gets first name
        first = scanner.next();  

        System.out.print("Enter your middle name: "); //gets middle name
        middle = scanner.next();

        System.out.print("Enter your last name: "); //gets last name
        last = scanner.next();

        System.out.println(first + " " + middle + " " + last); //prints First Middle Last names
    }
    public String getFirst(){
        return first;
    }
    public String getMiddle(){
        return middle;
    }
    public String getLast(){
        return last;
    }
    public String firstMiddleLast(){
        return (first + " " + middle + " " + last);
    }
    public String lastFirstMiddle(){
        return (last + "," + first + " " + middle);
    }
    public boolean equals(){
        return (name1.first.equals(name2.first) && (name1.middle.equals(name2.middle) && (name1.last.equals(name2.last))));
    }
    public String initials(){
        return (charAt(0) + charAt(" " + 1) + charAt(" " + 1);
    }
    public int length(){
        return ();
    }
}
 
0
 

Does the code you posted compile? If not, copy full text of error messages and paste here.

 
0
 

It doesn't compile I think because my last 3 commands aren't in the correct format. I'm using eclipse so it won't even let me compile it, but it says that name1 and name2 cannot be resolved, that name1 and name2 cannot be resolved to a variable (I'm not sure if this means the same thing), it asks if I want to create the method charAt(String), and last it says there is a syntax error on the int length question because it expected a "," after the "(".

If it helps, I have to use this code to create a different file that creates 2 Name objects using the methods in this. I want to get the methods right first before I create the objects in a different file.

 
0
 

it says that name1 and name2 cannot be resolved,

Where are those two variables defined? Are their definitions "in scope" where you are trying to use them. In scope roughly means is the definition and usage within the SAME enclosing pair of {}s.

Where is the method charAt() defined? The IDE can't find its definition.

there is a syntax error on the int length

No idea what your IDE means here. You need to post the errors with the lines of code that are causing them.

 
0
 

Here is where I define my objects:

public class TestNames {
    public static void main(String[] args) {  

        Name name = new Name();
        Name otherName = new Name();

        System.out.println(name.firstMiddleLast());
        System.out.println(name.lastFirstMiddle());
        System.out.println(name.initials());
        System.out.println(name.length());
        
        System.out.println(otherName.firstMiddleLast());
        System.out.println(otherName.lastFirstMiddle());
        System.out.println(otherName.initials());
        System.out.println(otherName.length());
    }
}
 
0
 

Here are the errors I am getting when I run the TestNames.java file:
Description Resource Path Location Type
Cannot make a static reference to the non-static method length() from the type Name Name.java /HW3/src line 44 Java Problem
I am stumped on how to fix the length.

 
0
 

I want the output to look like this
John F Kennedy
Kennedy, John F
JFK
Name Length: 12
Jackie O Onassis
Onassis, Jackie O
JOO
Name Length: 14
the middle names are: F O
the last names are: Kennedy Onassis
Are John F Kennedy and Jackie O Onassis equal? : False

I'm not expecting the world but I feel like I've hit a roadblock with the length thing
I was getting some of the above output for my solution, but obviously not the length. Also I'm having trouble figuring out how to get it to print both middles and both lasts, and also with the boolean to compare the two names. That's why the boolean statement in my code is commented out.
Thanks in advance.

 
0
 

And this is where I define the class

/* import the Scanner class */  
import java.util.Scanner;  

class Name {  
	
	String first, middle, last;
	public Name(){

		Scanner scanner = new Scanner(System.in);  

		System.out.print("Enter your first name: ");  //gets first name
		first = scanner.next();  

		System.out.print("Enter your middle name: "); //gets middle name
		middle = scanner.next();

		System.out.print("Enter your last name: "); //gets last name
		last = scanner.next();

		System.out.println(first + " " + middle + " " + last); //prints First Middle Last names
	}
	public String getFirst(){
		return first;
	}
	public String getMiddle(){
		return middle;
	}
	public String getLast(){
		return last;
	}
	public String firstMiddleLast(){
		return (first + " " + middle + " " + last);
	}
	public String lastFirstMiddle(){
		return (last + "," + first + " " + middle);
	}
	//public boolean equalsIgnoresCase(){
	//return (Name.first.equals(otherName.first) && (Name.middle.equals(otherName.middle) && (Name.last.equals(otherName.last))));
	//	}
	public String initials(){
		return (first.substring(0,1) + middle.substring(0,1) + last.substring(0,1));
	}
	public int length(){
		return Name.length();
	}
}
 
0
 

Cannot make a static reference to the non-static method length() from the type Name Name.java /HW3/src line 44

That refers to

return Name.length();

What value do you want to return in the length() method? You need to decide what value to return here.
The syntax of the statement says that length() is a static method in the Name class. If length() were static, that call would be recursive and would result in an infinite recursion (it would call itself until the JVM ran out of memory)

hy the boolean statement in my code is commented out.

Sorry I missed the reason or problem you are having here. Can you explain?

 
0
 

I just said that I am having trouble with the boolean call, so I commented out the boolean portion of my code so that I could run the program.

so for length should I do return length(Name)?

I'm sorry, I'm very new at Java and just having a hard time grasping this.

 
0
 

What value do you want to return from your length method? Without knowing that, I can't tell you what to code.

You need to read the API doc for the String method to see how to use the length() method.

 
0
 
public int length(){
        return name.length();
    }

I want to return how many characters are in the name that is entered as an int. I don't see what else is missing. there are two objects, name and otherName. I think the above code will work.

 
0
 

Does it return the desired value?
What does the variable: name contain? Is it the first+middle+last? with or without separating spaces?

 
0
 

no for some reason it does not return the desired value. Here is the error

Exception in thread "main" java.lang.NullPointerException
    at Name.length(Name.java:47)
    at TestNames.main(TestNames.java:11)

the variable name contains first+middle+last with separating spaces. I want the character count to not include separating spaces.

 
0
 

What variable is at line 47 in the program? It needs to have a value that is not null.
Your code should test if the variable is null and return a zero.

Question Answered as of 3 Years Ago by NormR1
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: