Computer Applications
Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown.
Input
C:\Users\admin\Pictures\flower.jpg
Output
Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
Answer
import java.util.Scanner;
public class KboatFilepathSplit
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter full path: ");
String filepath = in.next();
char pathSep = '\\';
char dotSep = '.';
int pathSepIdx = filepath.lastIndexOf(pathSep);
System.out.println("Path:\t\t" + filepath.substring(0, pathSepIdx));
int dotIdx = filepath.lastIndexOf(dotSep);
System.out.println("File Name:\t" + filepath.substring(pathSepIdx + 1, dotIdx));
System.out.println("Extension:\t" + filepath.substring(dotIdx + 1));
}
}
Output
Related Questions
Write the output of the following String methods:
String x= "Galaxy", y= "Games";
(a) System.out.println(x.charAt(0)==y.charAt(0));
(b) System.out.println(x.compareTo(y));
Consider the following program segment in which the statements are jumbled, choose the correct order of statements to check if a given word is Palindrome or not.
boolean palin(String w) { boolean isPalin; w = w.toUpperCase(); int l = w.length(); isPalin = false; // Stmt (1) for (int i = 0; i < l / 2; i++) { char c1 = w.charAt(i), c2 = w.charAt(l - 1 - i); // Stmt (2) if (c1 != c2) { break; // Stmt (3) isPalin = true; // Stmt (4) } } return isPalin; }
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13
The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }