Results 1 to 4 of 4
  1. #1
    Deepwoods is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default Simple File extension program

    I'm having trouble getting this program to display output. Please tell me what is wrong with the code provided.

    N is the number of media type associations and Q is the number of filenames and they will be given as numbers on first line of input.

    INPUT:
    Following this are N lines, each containing a file extension and a media type, separated by a space. Finally, Q lines, each containing the name of a file.

    OUTPUT:
    For each of the Q file names, print on a line the media type of the file. If there is no matching entry, print "unknown" (quotes for clarity).

    Sample Input

    5 6
    html text/html
    htm text/html
    png image/png
    svg image/svg+xml
    txt text/plain
    index.html
    this.file.has.lots.of.dots.txt
    nodotsatall
    virus.exe
    dont.let.the.png.fool.you
    case.matters.TXT

    Sample Output

    text/html
    text/plain
    unknown
    unknown
    unknown
    unknown

    Java Code:
    import java.util.Scanner;
    import java.lang.String;
    class sumNQ
    {
    public static void main(String args[])
    {
    Scanner s=new Scanner(System.in);
    System.out.println("enter N and Q :");
    String NQ=s.nextLine();
    Scanner snum = new Scanner(NQ).useDelimiter(" ");//for getting the number of file types n and number of filenames q
    int n=snum.nextInt();
    int q=snum.nextInt();
    int sumnq=n+q; // sum of total number of lines in input
    
    String[] strarr = new String[sumnq]; // array of strings with each element containing a line of input
    int i=0;
    while(i<sumnq)
    {
    strarr[i]=s.nextLine();
    i++;
    }
    
    String[][] strN = new String[n][2]; // 2d array of strings containing filetype in first element and mediatype in second element of first dimension
    for(int j=0;j<n;j++)
    {
    Scanner strarrstr=new Scanner(strarr[j]).useDelimiter(" ");
    strN[j][0]=strarrstr.next();
    strN[j][1]=strarrstr.next();
    }
    
    String res="";
    for(int m=n;m<sumnq;m++) // for iterating filenames starting at index n of strarr
    {
    String stest=strarrAfterPeriod(strarr[m]);
    for(int l=0;l<n;l++) // for testing filenames starting at n with filetypes starting at 0 of strarr
    {
    if(stest.equals(strN[l][0])) // comparison
    {
    res+=strN[l][1]+"\n"; // output string
    }
    else if(l==n)
    res=res+"unknown\n";
    }}
    System.out.println(res);
    }
    
    public static String strarrAfterPeriod(String str) // for fetching the string after last period of filename
    {
    Scanner speriod=new Scanner(str).useDelimiter(".");
    String sper=speriod.next();
    while(speriod.hasNext())
    sper=speriod.next();
    return sper;
    }}
    Last edited by pbrockway2; 12-31-2011 at 01:01 AM. Reason: code tags corrected

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default Re: Simple File extension program

    The code tags are [code] ...[/code]

  3. #3
    Deepwoods is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default Re: Simple File extension program

    Quote Originally Posted by pbrockway2 View Post
    The code tags are [code] ...[/code]
    Thanks for the info.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default Re: Simple File extension program

    It's not your whole problem, but

    Java Code:
    for(int m=n;m<sumnq;m++) 
    {
        String stest=strarrAfterPeriod(strarr[m]);
        for(int l=0;l<n;l++)
        {
            if(stest.equals(strN[l][0])) 
            {
                res+=strN[l][1]+"\n"; 
            }
            else if(l==n)  // <----- this condition can never be true
                res=res+"unknown\n";
        }
    }
    You would help yourself if you followed some standard coding conventions: start the class name with a capital letter, make variable names descriptive and never use l, use braces even around one line blocks of code.

    Also, check everything. Eg, use System.out.println() to see what strarrAfterPeriod() is actually returning (and what you are passing to it), otherwise if is not doing what you expect you won't ever know that.

Similar Threads

  1. Replies: 3
    Last Post: 06-21-2011, 05:51 PM
  2. Text file extension?
    By chrisLU5 in forum New To Java
    Replies: 4
    Last Post: 05-14-2011, 01:20 AM
  3. File Extension Filter
    By heartysnowy in forum New To Java
    Replies: 9
    Last Post: 10-09-2010, 02:33 PM
  4. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 12:17 PM
  5. Regex for file extension
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-31-2008, 04:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •