How to compare File extension in java?



Google


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 theater

I 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?


FirstPrevious12NextEnd

More Related Questions and Answers ...
  • Extension cord?????
  • PHP GD2 & cURL extension?
  • "blanket extension"..?
  • Registry Extension Problems with Emails?
  • PHP Script question - extension_dir does not ..
  • Housing extension cost? UK?
  • 17 month OPT Extension and e-verify?
  • Is there an extension glue that you CAN glue ..
  • Harvard / Harvard Extension school?
  • Extension Cord for Pool Pump has black / burn..
  • Late taxes/extension vs. rebate?
  • Canada Revenue Agency Deadline Extension?
  • Extension cord help, what's an Amp a..
  • Can I change the rm extension into another au..
  • UK Telephone extension kit problem?
  • Remington 870 2 round extension?
  • What file extension can I use for streaming a..
  • Extension cord exploded, why?detailed?
  • Extension For HP?

  • The information post by website user , we not guarantee correctness.