Objective Use String class methods to identify parts of text Background reading
ID: 3939922 • Letter: O
Question
Objective Use String class methods to identify parts of text Background reading ZyBooks 3.5 - 3.8 Assignment You are to write a Java application that Creates an ID from two words. The ID has the form: LastHalfOfWord1 + LengthOfWord2 + UppercaseFirstLetterOfWord2 For example, if the two words are george and Washington, the ID will be rge10W Extracts a file extension from a file path. For example, if the file path is/some/path/to/file/myfile.java, the program would print: The file extension is ".java".Explanation / Answer
// JavaApplication.java
import java.util.Scanner;
public class JavaApplication
{
// Part2
public static String getExtension(String filePath, char extensionSeparator)
{
int sep = filePath.lastIndexOf(extensionSeparator);
return filePath.substring(sep + 1);
}
public static void main(String[] args)
{
// Part1
Scanner sc=new Scanner(System.in);
System.out.print("Enter word1: ");
String word1 = sc.next();
System.out.print("Enter word2: ");
String word2 = sc.next();
String ID = word1.substring(word1.length()/2);
int l = word2.length();
ID = ID.concat(Integer.toString(l));
word2 = word2.toUpperCase();
char c = word2.charAt(0);
ID = ID.concat(Character.toString(c));
System.out.println("ID: " + ID);
// Part2
String filePath = "/home/ubuntu/index.html";
System.out.println(" PATH: " + filePath);
System.out.println("Extension = " + getExtension(filePath, '.'));
}
}
/*
output:
Enter word1: george
Enter word2: washington
ID: rge10W
PATH: /home/ubuntu/index.html
Extension = html
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.