Posted: Fri Feb 22, 2008 11:04 am Post subject: How to compare extension in java
this is 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 so far
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 ("doc file.");
}
else
{
System.out.println ("none.");
}
}
}
I cant seem to figure out, any ideas
Sponsor Sponsor
Tony
Posted: Fri Feb 22, 2008 11:47 am Post subject: RE:How to compare extension in java
Posted: Fri Feb 22, 2008 6:07 pm Post subject: Re: RE:How to compare extension in java
OneOffDriveByPoster @ Fri Feb 22, 2008 4:30 pm wrote:
supahsain08 @ Fri Feb 22, 2008 3:57 pm wrote:
java file
thing is my teacher didn't teach us anything
What did your teacher teach you? In any case, you might want to stick to java.lang.String for checking the extension.
he only taught us about if else command, and input in java
he said learn other stuff using internet
OneOffDriveByPoster
Posted: Fri Feb 22, 2008 8:52 pm Post subject: Re: RE:How to compare extension in java
supahsain08 @ Fri Feb 22, 2008 6:07 pm wrote:
he said learn other stuff using internet
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html will help. Seriously, write some pseudo-code so we can tell what your logic is for your program. If people do not seem to be helpful, it is because your program makes people think you do not have the right idea at all.
supahsain08
Posted: Fri Feb 22, 2008 11:09 pm Post subject: Re: How to compare extension in java
k finished it i think
code:
import java.io.*;
public class string1
{
public static void main (String [] args) throws Exception
{
DataInput keyboard = new DataInputStream (System.in);
String extension;
System.out.print ("please enter a file name with extension : ");
extension = keyboard.readLine ();
if (extension.endsWith (".doc"))
System.out.println ("That was a Word document!");
if (extension.endsWith (".xls"))
System.out.println ("That was an Excel spreadsheet!");
if (extension.endsWith (".java"))
System.out.println ("That was a Java source file!");