• Post Reply Bookmark Topic Watch Topic
  • New Topic

putting a name in an array  RSS feed

 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello;

I am trying to receive a name and place it in an array. From there I want to select a letter from the array and display it. I am new to Java but really enjoying it.

Can someone help me get started in the right direction ? I do not want anyone to write my code.

This is a great forum. Thanks to the members and moderator.

Ken
 
Suhrid Karthik
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String class has a toCharArray() method which will be of use to you.
 
K Abhijit
Ranch Hand
Posts: 88
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For sake of displaying individual char of 'name' you need not have array... String class does has extensive API to cater that, just have deeper look @ String class
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My goal here is to have the user input a first, midle, and last name. Then I want to grab the first initial and display it as a monogram
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Mullins wrote:My goal here is to have the user input a first, midle, and last name. Then I want to grab the first initial and display it as a monogram


Go with k Abhijit's recommendation: check out the String API as it has methods you can use including split(...) to separate out the separate name Strings as well as others that allow you to get the first letter of a String.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I understand what you are saying. I have three strings entered by the user. They are firstName, middleName, and lastName. These are input using JOptionPane.showDialogInputDialog.

I am able to easily display these names and the complete name. I want to be able to grab the first initial and display them as a monogram.

Can I just grab this first letter from each name out of the string ?

I appreciate your help.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I have the monogram but it is in ASCII. Is there a simple way to convert this to letters ?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Mullins wrote:Ok, I have the monogram but it is in ASCII. Is there a simple way to convert this to letters ?


What is ASCII? Do you mean you have a char(s) and want to convert it/them to a String? If so, then String has a valueOf(...) method that can help you here.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the program and choose to display the char1, which is the first letter of the first name it displays fine. When I try to display the others, char2 and char3 I get an integer vlaue. I am trying to display the char1, char2, and char3 and a monogram. Do I need to convert these or is there a way to display them as they are ?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may wish to show us the important portions of your code.

Best of luck!
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String name;
int monogram;
name = firstName + middleName + lastName;
char char1 = firstName.charAt(0); //gets first letter of this name
char char2 = middleName.charAt(0); //gets first letter of this name
char char3 = lastName.charAt(0); //gets first letter of this name

JOptionPane.showMessageDialog(null, firstName + " " + middleName + " " + lastName);//displays full name
char firstInitial;
firstInitial = char1;
char secondInitial;
secondInitial = char2;
char thirdInitial;
thirdInitial = char3;
JOptionPane.showMessageDialog(null, firstInitial); //will display the first initial of the first name. If I add the others I get a number
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the resulting part of my code. How can I get all three of the first initials to print out in the JOption.Pane together ? I have tried everything I can find.
If I only put one initial values in there it prints fine. Is there a way to put all 3 in and use a special separator that will not try to add them arithmetically ? I think they need converted but I am not sure how.

String name;
int monogram;
name = firstName + middleName + lastName;
char char1 = firstName.charAt(0); //gets first letter of this name
char char2 = middleName.charAt(0); //gets first letter of this name
char char3 = lastName.charAt(0); //gets first letter of this name

JOptionPane.showMessageDialog(null, firstName + " " + middleName + " " + lastName);//displays full name
char firstInitial;
firstInitial = char1;
char secondInitial;
secondInitial = char2;
char thirdInitial;
thirdInitial = char3;
JOptionPane.showMessageDialog(null, firstInitial); //this will display the result of "firstInitial"
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution has already been given to you: String.valueOf method which can be used to convert a char into a String. Please look this up in the API and give it a try.

Best of luck!
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

I will look closer at it.

I appreciate your help.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have thried this suggestion annd I still cannot get this to display correctly.

Is there a conversion I need to do ?

Any help would be very much appreciated
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Mullins wrote:I have thried this suggestion annd I still cannot get this to display correctly.

Please show us what you've tried, else we'll never know what you're doing wrong.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate your help
This code displays the letter of the firstInitial in the message box. As long as it is by itself it will display. If I try to add second and or thirdInitial I get a number
I need all 3 firstInitial, secondInitial, thirdInitial to display in the messge box



import javax.swing.JOptionPane;

public class EasyMonogram
{
public static void main(String[] args)
{
String firstName = JOptionPane.showInputDialog("Enter First name: ");
String middleName = JOptionPane.showInputDialog("Enter Middle Name: ");
String lastName = JOptionPane.showInputDialog("Enter Last name: ");
String name;
String monogram;
name = firstName + middleName + lastName;
char char1 = firstName.charAt(0);
char char2 = middleName.charAt(0);
char char3 = lastName.charAt(0);

JOptionPane.showMessageDialog(null, firstName + " " + middleName + " " + lastName);
char firstInitial;
firstInitial = char1;
char secondInitial;
secondInitial = char2;
char thirdInitial;
thirdInitial = char3;
int output = firstInitial + secondInitial + thirdInitial;


JOptionPane.showMessageDialog(null, firstInitial);

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken Mullins wrote:I really appreciate your help
This code displays the letter of the firstInitial in the message box. As long as it is by itself it will display. If I try to add second and or thirdInitial I get a number
I need all 3 firstInitial, secondInitial, thirdInitial to display in the messge box


When you add chars, Java returns an int which is why you're seeing the number. Again, use String.valueOf(char) to translate each char into a String. I've recommended this a few times above but have yet to see you try this method. If you've tried it and it doesn't work, again, please show us how you're trying it. It will work, trust me.
 
Ken Mullins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally got it to work using String.substring(

Thanks for your help and patience.

I aam just beginning my Java journey. Its really cool !
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget to use code tags; your posts are difficult to read without them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!