6 Replies - 1220 Views - Last Post: 14 September 2009 - 03:06 PM Rate Topic: -----

#1 babylon.pride  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-May 09

Returning Part of a String

Posted 13 September 2009 - 11:22 AM

Meh. I feel stupid asking a few times on a few different codes but whatever. I haven't done anything even close to coding in four months and now have forgotten it all.

My assignment is to take input from a name (first, middle and last) and a social security number to output it as a username. Example, for Alicia Betty Cook, with the social 123-45-6789, it'd be returned as, "abc6789"

Thing is... Well, a few things. One, I really do forget everything and it's annoying me because I knew it last year. But I can't seem to remember for the life of me how to output just a part of a string (double, whatever needs to be used). Secondly, I know the code is crappy. I need to go through and change a lot of stuff so don't mind that. Three... is there a better way to check for the social being correct? I can't think of one. And I'm also not sure if the four ors will work together.

/*
 * UnixAccount.java
 *
 * Version:
 *	 $Id$
 *
 * Revisions: 
 *	 $Log$
 */

import java.util.Scanner;

/**
 * This program computes a Unix user name for a user by taking
 * the first letter of their first name, their middle name and 
 * their last name, along with the last four of their social security
 * number and puts them together. 
 * 
 * @author	
 *
 */

class UnixAccount {

	/**
	 * Main method for program.
	 *
	 * @param  args command line arguments (ignored)
	 */

	public static void main( String args[] ) {
	String fname;					  // First name
	String mname; 					// Middle name
	String lname;					  // Last name
	double ss; 						// Social Security
	Scanner scan = new Scanner (System.in); // input scanner 

	// Get names, and social

	System.out.print ( "Enter First Name, Middle Name, Last Name, and SSN: " );
	fname = scan.nextLine();
	mname = scan.nextLine();
	lname = scan.nextLine();
	ss = scan.nextDouble();
	System.out.println();
	
	// Check to see if social security number is valid
	
	if (ss <= 000000000 || ss >= 999999999 || ss <= 000-00-000 
			|| ss >= 999-99-9999){
		System.out.print("Social security number invalid. Please try again:");
		ss = scan.nextDouble();
	}
	

	
		
	
	System.out.print ("Unix Account Name: " + );

} 



Thanks a bunch.

Is This A Good Question/Topic? 0
  • +

Replies To: Returning Part of a String

#2 aks29921  Icon User is offline

  • D.I.C Regular

Reputation: 116
  • View blog
  • Posts: 345
  • Joined: 24-August 09

Re: Returning Part of a String

Posted 13 September 2009 - 11:31 AM

you have to concatenate the first letter of each word of your username, isn't it?
do this:
start a loop from the beginning of the username until its length,
concatenate in a string (which can be set to initially contain the first letter of the username) each character of the username which occurs after a space...
u can use the substring function or any other way you want to implement this.
Was This Post Helpful? 1
  • +
  • -

#3 Timeaisis  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 08-September 09

Re: Returning Part of a String

Posted 13 September 2009 - 11:36 AM

Alright, so the easiest way to do this is String.charAt(0);

I'd do it like this:
String ssn = "";
String unixAccountName = "";
unixAccountName = scan.nextLine().charAt(0);
unixAccountName = unixAccountName + scanNextLine().chartAt(0) + scanNextLine().charAt(0);
ssn = scan.nextLine();



And then to get the substring of ssn, you just use ssn.substring(start, end)

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12134
  • View blog
  • Posts: 45,114
  • Joined: 27-December 08

Re: Returning Part of a String

Posted 13 September 2009 - 12:56 PM

View PostTimeaisis, on 13 Sep, 2009 - 12:36 PM, said:

Alright, so the easiest way to do this is String.charAt(0);

I'd do it like this:
String ssn = "";
String unixAccountName = "";
unixAccountName = scan.nextLine().charAt(0);
unixAccountName = unixAccountName + scanNextLine().chartAt(0) + scanNextLine().charAt(0);
ssn = scan.nextLine();



And then to get the substring of ssn, you just use ssn.substring(start, end)

Hope this helps.


If you need to store these values in variables, you can; however, Timeaisis' solution is more efficient. For the social security number, you should use the substring method with start being the length()-5 and end being the length()-1. Hope this helps some. Good luck!
Was This Post Helpful? 0
  • +
  • -

#5 babylon.pride  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-May 09

Re: Returning Part of a String

Post icon  Posted 14 September 2009 - 02:17 PM

Oh, so happy. I almost got this to be perfect.

So the assignment is to ask for the first, middle and last name of someone, and their social, and make a username from their initials and their last four. (Example: Alicia Barbara Cook, Social 567-89-1234, would be abc1234).

I'm just not sure on two things now.

One is I need to make it so that the social can be inputted with either dashes or without dashes. I know how to do it without but I'm not sure how to do it with.

EDIT: I figured out how to change the letters to lowercase. Woo!

/*
 * UnixAccount.java
 *
 * Version:
 *	 $Id$
 *
 * Revisions: 
 *	 $Log$
 */


import java.util.Scanner;

/**
 * This program computes a Unix user name for a user by taking
 * the first letter of their first name, their middle name and 
 * their last name, along with the last four of their social security
 * number and puts them together. 
 * 
 * @author	
 *
 */

class UnixAccount
{
	

	/**
	 * Main method for program.
	 *
	 * @param  args command line arguments (ignored)
	 */
	
	public static void main(String[] args)
	{
		String firstName;				// First name
		String middleName;				// Middle name
		String lastName;				// Last name
		String socialSecurity;			// Social security number
		char firstInitial;				// First name initial
		char middleInitial;				// Middle name initial
		char lastInitial;				// Last name initial
		char socialOne;					// Fourth to last social number
		char socialTwo;					// Third to last social number
		char socialThree;				// Second to last social number
		char socialFour;				// Last social number
		Scanner myScanner = new Scanner(System.in);
		
		// Get information
		
		System.out.print( "Enter First Name, Middle Name, Last Name, and SSN: " );
		firstName = myScanner.next();
		firstInitial = firstName.charAt(0);
		middleName = myScanner.next();
		middleInitial= middleName.charAt(0);
		lastName = myScanner.next();
		lastInitial = lastName.charAt(0);
		socialSecurity = myScanner.next();
		socialOne = socialSecurity.charAt(5);
		socialTwo = socialSecurity.charAt(6);
		socialThree = socialSecurity.charAt(7);
		socialFour = socialSecurity.charAt(8);
		
		if(Character.isUpperCase(firstInitial))
			firstInitial = Character.toLowerCase(firstInitial);
		
		if(Character.isUpperCase(middleInitial))
			middleInitial = Character.toLowerCase(middleInitial);
		
		if(Character.isUpperCase(lastInitial))
			lastInitial = Character.toLowerCase(lastInitial);
		
		
		
		// Show information
		
		System.out.println("\nUnix Account Name: " + firstInitial + middleInitial + 
		lastInitial + socialOne + socialTwo + socialThree + socialFour);
	}

}
	




Thanks. =D

This post has been edited by babylon.pride: 14 September 2009 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Returning Part of a String

Posted 14 September 2009 - 02:59 PM

Topics merged :)
Was This Post Helpful? 0
  • +
  • -

#7 babylon.pride  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-May 09

Re: Returning Part of a String

Posted 14 September 2009 - 03:06 PM

=] Okay. I also did a second topic for my die problem, just to let you know. I wasn't sure of how to repost or just to update or what.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1