How to compare File extension in java?
That's what i have to do
Write a program that prompts the user for a file name (including the extension), checks the extension and responds with a comment about the type of file, for example ?
.doc That was a Word document!
.xls That was an Excel spreadsheet!
.java That was a Java source file!
my code
import java.util.Scanner;
import java.io.*;
class string
{
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
String extension;
System.out.println ("Please enter a file name with extension.");
extension = in.nextLine();
Scanner s = new Scanner(extension);
s.findInLine("doc");
if (s.equals("doc"))
{
System.out.println ("Please enter a file name with extension.");
}
else
{
System.out.println ("ji.");
}
}
}
I can't figure out
How to compare File extension in java?home theaterI don't know why you're using the "Scanner". You don't need to.
If you're required to because of some class.. then you'll need to modify what i have done below. The entire if (s.equals("doc")) block is not needed. If you have to use scanner.. then atleast grab what it returns from s.findInLine("doc"); You should not check if it is "in" the string. It has to be at the end of the filename. what if a user inputs document.doc.zip ?? how will your program work then?
More Related Questions and Answers ...
The information post by website user , we not guarantee correctness.
