Welcome to Dream.In.Code
Become a Java Expert!

Join 149,574 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,686 people online right now. Registration is fast and FREE... Join Now!




String Methods

 
Reply to this topicStart new topic

String Methods, charAt() Methods

nikkirose7
16 Oct, 2007 - 01:42 PM
Post #1

New D.I.C Head
*

Joined: 16 Oct, 2007
Posts: 2


My Contributions
Hi I am a beginner and I am trying to write an application that asks the user for their full name and street address and then creates a user ID from the user's initals and numberic part of the address. Example Nicole Fink W7935 Haberkorn Drive = NF7935 for an ID. I have looked everywhere for how to get it to pull the initials and the numberic part of the address and cannot find anything. So far I have. . .

CODE

import javax.swing.*;
public class ConstructID
{
    public static void main(String[] args)
    {
    String nameString;
    String addressString;
    char name;
    char address;
    
    nameString = JOptionPane.showInputDialog (null, "Enter your full name");
    name = Double.parseDouble(nameString);
    
    addressString = JOptionPane.showInputDialog (null, "Enter your full name");
    address = Double.parseDouble(addressString);
    }
}



I think the Double is wrong because it's a character not a double?
Any help would be awesome!!!!!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: String Methods
16 Oct, 2007 - 02:42 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi nikkirose7,

Below is a proposed solution for you. I took out the showdialog calls since I am not using a GUI frame in the example and figured a scanner might be easier to understand at this point. As you can see I simply collect the name and address, split the name and pull off the first character on each part of the name, then loop through the address adding only the digits. The result is your ID.

CODE

import javax.swing.*;
import java.util.Scanner;

public class constructid
{
    public static void main(String[] args)
    {
        String nameString;
        String addressString;
        String id = "";

        // Use the scanner to setup reading input from user.
        Scanner s = new Scanner(System.in);
    
        // Prompt for the user's name and their address
        System.out.print("Enter your full name: ");    
        nameString = s.nextLine();

        System.out.print("Enter your full address: ");
        addressString = s.nextLine();

        // Split the name on the spaces and take the first character of each piece
        String[] pieces = nameString.split(" ");

        for (String piece : pieces) {
                id += Character.toString(piece.charAt(0));
        }


        // Loop through the address looking for digits (using isDigit of the Character class)
        for (int i = 0; i < addressString.length(); i++) {
                if (Character.isDigit(addressString.charAt(i))) {
                        id += Character.toString(addressString.charAt(i));
                }
        }

        System.out.println(id);
    }
}


Read through the in code comments to see what I am doing where. Hopefully it is pretty straight forward for you and that it is what you were asking for. Of course if you are building a GUI and everything you can go about collecting the information anyway you like.

Enjoy!

"At DIC we be coding ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month