
-----------------------------------
supahsain08
Fri Feb 22, 2008 11:04 am

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
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

-----------------------------------
Tony
Fri Feb 22, 2008 11:47 am

RE:How to compare extension in java
-----------------------------------
mime type

-----------------------------------
wtd
Fri Feb 22, 2008 1:09 pm

RE:How to compare extension in java
-----------------------------------
s.findInLine(.doc);

Hmmm.... spot any problems?

-----------------------------------
supahsain08
Fri Feb 22, 2008 3:16 pm

RE:How to compare extension in java
-----------------------------------
nope, no clue
*first time using the scanner command

-----------------------------------
wtd
Fri Feb 22, 2008 3:27 pm

RE:How to compare extension in java
-----------------------------------
Has nothing to do with the Scanner class.

-----------------------------------
Tony
Fri Feb 22, 2008 3:37 pm

RE:How to compare extension in java
-----------------------------------
what file type is my.document.java ?

-----------------------------------
supahsain08
Fri Feb 22, 2008 3:57 pm

RE:How to compare extension in java
-----------------------------------
java file
thing is my teacher didn't teach us anything

-----------------------------------
OneOffDriveByPoster
Fri Feb 22, 2008 4:30 pm

Re: RE:How to compare extension in java
-----------------------------------
java file
thing is my teacher didn't teach us anythingWhat did your teacher teach you?  In any case, you might want to stick to java.lang.String for checking the extension.

-----------------------------------
Tony
Fri Feb 22, 2008 4:50 pm

Re: RE:How to compare extension in java
-----------------------------------
java file
That's not the answer I was looking for. Lets try again.

what file type is my.document.java ?
s.findInLine(.doc);
Hmmm.... spot any problems?

-----------------------------------
supahsain08
Fri Feb 22, 2008 6:07 pm

Re: RE:How to compare extension in java
-----------------------------------
java file
thing is my teacher didn't teach us anythingWhat 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
Fri Feb 22, 2008 8:52 pm

Re: RE:How to compare extension in java
-----------------------------------
he said learn other stuff using internethttp://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
Fri Feb 22, 2008 11:09 pm

Re: How to compare extension in java
-----------------------------------
k finished it i think

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!");

    }
}
