So I'm creating an employee payroll system and the id requirements are:
must always be 10 characters long. The first seven are First 3 letters of first name, the middle initial(default is zero if no middlename), and the first 3 letters of last name
the last 3 chars are an incrementing value which represents the number of occurrences of the first 7 characters (eg. AAABCCC001, AAABCCC002, XXXYZZZ001,XXX0ZZZ001 etc).
I'm not sure how to approach this. Help Please!
This is the code I have so far:
count=1;
fnameSubstr= fname.substring(0,3).toUpperCase();
mInitial= mnames.substring(0,0).toUpperCase();
lnameSubstr= lname.substring(0,3).toUpperCase();
nameStr=fnameSubstr + mInitial + lnameSubstr + String.valueOf(count).format("%03d", count);
for (Employee e: emp_list){
if nameStr.equals(id){
intStr=nameStr.substring(7); //string representing the first 7 chars
strInt=Integer.parseInt(intStr);//string of the last 3 chars
if count==strInt{ //compares the count to the int value of the last 3 chars
count++;
nameStr=fnameSub + mInitial + lnameSub+String.valueOf(count).format("%03d",count);
}
}
else{
count=1;
nameStr=fnameSub + mInitial + lnameSub + String.valueOf(count).format("%03d", count);
}
}
I'm not sure if I'm on the right track.
Please use the below code
`
static Integer count = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getEmployeeIdBy("DILIP","","DURAISWAMY"));
System.out.println(getEmployeeIdBy("KUTTY","","DILIP"));
System.out.println(getEmployeeIdBy("PANDA","R","SADASIBA"));
}
public static String getEmployeeIdBy(String firstName, String middleName, String lastName) {
String res1 = firstName.substring(0, 3);
String res2 = middleName.isEmpty() ? "0" : middleName.substring(0, 1);
String res3 = lastName.substring(0, 3);
String res4 = res1 + res2 + res3;
String res5 = count.toString().length() == 1 ? ("00" + count)
: count.toString().length() == 2 ? ("0" + count) : count.toString();
count = count + 1;
String finalResult = res4 + res5;
return finalResult;
}`
The final output would be
DIL0DUR000
KUT0DIL001
PANRSAD002
Get the letters of the from the name using substring method. set variables
String fName = //first three letters of the first name;
String mName = "0";
String lName = //first three letters of the last name;
if (/*mName is not null*/){
mName = //get the middle initial
}
create a counter = 1 to count how many ids you have made. Note that you can use String.format("%03d",counter) to format the counter in three digits; and lastly concatinate all your variables.
Related
I have a String that goes "Peyton Manning; 49". Is there a way to have the computer read the left side of the String and make it equal to a new String "Peyton Manning" and take the right side of the String and make it equal to an int with a value of 49?
A number of ways spring to mind: you can tokenize or you can use regexs. You didn't specify about white space padding. Also, you didn't specify if the string segments around the delimiter can be invalid integers, mixtures strings digits etc. If this helped you, please mark accordingly.
public class Test {
public static void main(String[] args) {
String s="Blah; 99";
StringTokenizer st = new StringTokenizer(s, ";");
String name = st.nextToken();
Integer number = Integer.parseInt(st.nextToken().trim());
System.out.println(name.trim() + "---" + number);
number = Integer.parseInt(s.replaceAll("[^0-9]", ""));
name = s.replaceAll("[^A-Za-z]", "");
System.out.println(name + "---" + number);
int split = s.indexOf(";");
name = s.substring(0, split);
number = Integer.parseInt(s.substring(split+2, s.length()));
System.out.println(name + "---" + number);
}
}
The simplest way is:
String text = input.replaceAll(" *\\d*", "");
int number = Integer.parseInt(input.replaceAll("\\D", ""));
See KISS principle.
public static String number(String words) {
int length = words.length;
int total = 0;
while(int index < length) {
total = total + 1;
index = index + 1;
}
}
String output;
output = total + " word";
return output;
}
Example output for this would be:
numberOfWords("Hello whats up?")
3 word
This would work for all proper sentences but I have to account for bad input for example:
"Hi my name is bob"
, this would be like thirty plus words. Also
" "
, should be 0 words. Is there any simple way to make the first example to "hi my name is bob" ?
you can do something like this :
String trimmed = text.trim();
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;
or (simplest way):
use str.replaceAll("\\s+"," ");
Simplest will work in every case
String word = "Hi my name is bob";
word = word.replaceAll("\\s+", " ");
String count [] = word.split(" ");
System.out.println(count.length);
I am making a math program that guesses the birthday of the user.
The user enters a number lets say : 75622 and i subtract a number from it to get the birthday, in this case 42682 -- 04/26/82
i want to be able to turn that integer into a string and then add the forward slash between the month, day and year.. and also add a 0 if it is only 5 digits and not 6 ( because of the month being 1-9).
I know how to use Integer.toString(int) to turn it into a string, but i do not know how to insert the forward slashes and the zero.
thank you kindly!
If you just want to convert the int to a String (with a leading 0) you can use String.format like
int num = 42682;
String s = String.format("%06d", num);
You might then use another String.format and String.substring to build your desired output, like
String output = String.format("%s/%s/%s", s.substring(0, 2),
s.substring(2, 4), s.substring(4));
System.out.println(output);
Which outputs (as requested)
04/26/82
Please find answer below:
public class CreateDateFromNumber {
public static void main(String[] args) {
int number = 42682;
System.out.println(getDate(number));
}
public static String getDate(int number) {
String numberStr = Integer.toString(number);
String outputStr = "";
if (numberStr.length() != 5 && numberStr.length() != 6) {
throw new IllegalArgumentException(
"Number should be length of 5 or 6");
}
if (numberStr.length() == 5) {
numberStr = "0" + numberStr;
}
int var0 = 0;
while (var0 < numberStr.length()) {
String var1 = numberStr.substring(var0, var0 + 2);
outputStr = outputStr + var1;
if (var0 + 2 < numberStr.length()) {
outputStr = outputStr + "/";
}
var0 = var0 + 2;
}
return outputStr;
}
}
I'm working on a recursive method that will return and print in my main method a String that will count down by 1 for N. Deleting one letter for each recursion for the string. Until N becomes 1 or until the string has 1 letter. (N being a int on the commandline)
For example if my command lines aruments are: 5 valentine
It should output too:
5 valentine, 4 alentine, 3 lentine, 2 entine, 1 ntine,
So far I managed to count down the number inputted on the commandline argument. I just don't know how to go about deleting one letter from the string? :o
My code so far:
public static void main(String[] args){
int number = Integer.parseInt(args[0]);
String word = new String("");
word = args[1];
String method = recursive.method1(number);
System.out.println(method);
}
public static String method1(int number){
if (number < 0){
return "";
}
else{
return number + ", " + method1(number - 1);
}
}
You can read through subString() documentation to understand how to go about taking the portions of a String.
Change your method definition to include the word
Add the word to your return statement: return number + " " + word +...
Call substring of the original word from the 1st index
Check for <=0 rather than <0
Code:
public static void main(String[] args){
int number = 5;
String word = new String("");
word = "Valentine";
String method = Recurcive.method1(number, word);
System.out.println(method);
}
public static String method1(int number, String word){
if (number <= 0){
return "";
}
else{
return number + " " + word + ", " + method1(number - 1, word.substring(1));
}
}
Gives,
5 Valentine, 4 alentine, 3 lentine, 2 entine, 1 ntine,
So I have this little program and all it needs to do is check if the last letter of the last name is an "s". And if it is an "s" itll change the last name to plural.
Ex.
Smith = Smith's
Smiths = Smiths'
Changes the last name to plural. Simple right? Seems so, but my if statement isnt detecting if the last letter is an "s"
Here's some code
import javax.swing.JOptionPane;
public class Lastname {
public static void main(String[] args) {
String messageText = null;
String title = null;
int messageType = 0;
String lastName = "";
String pluralLastName = "";
Input input;
input = new Input();
messageText = "Please enter a last name. I'll make it plural.";
title = "Plural Last Names";
messageType = 3;
lastName = input.getString(messageText,title,messageType);
int intLength = lastName.length();
String lastLetter = lastName.substring(intLength- 1);
System.out.println("The last letter is: " + lastLetter);
if (lastLetter.equals('s'))
JOptionPane.showMessageDialog(null, "The last name entered as plural is " + lastName + "'" );
else
JOptionPane.showMessageDialog(null, "The last name entered as plural is " + lastName + "'s" );
}}
The if statement always just adds an "'s" to everything.
You need to use double quotes to represent a String literal.
if (lastLetter.equals("s"))
Otherwise you are comparing a String with a Character which will always return false.
Instead of comparing Strings, you can compare chars:
char lastLetter = lastName.charAt(intLength- 1);
System.out.println("The last letter is: " + lastLetter);
if (lastLetter == 's')
Right now, you are comparing Strings to chars.