Results 1 to 4 of 4
Thread: Simple File extension program
- 12-31-2011, 12:29 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
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
- 12-31-2011, 01:01 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Simple File extension program
The code tags are [code] ...[/code]
- 12-31-2011, 01:16 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
- 12-31-2011, 01:22 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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"; } }
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
-
How do you make a file save with a default file extension?
By Dark in forum New To JavaReplies: 3Last Post: 06-21-2011, 05:51 PM -
Text file extension?
By chrisLU5 in forum New To JavaReplies: 4Last Post: 05-14-2011, 01:20 AM -
File Extension Filter
By heartysnowy in forum New To JavaReplies: 9Last Post: 10-09-2010, 02:33 PM -
save file based on file extension
By masa in forum AWT / SwingReplies: 4Last Post: 05-11-2010, 12:17 PM -
Regex for file extension
By gapper in forum New To JavaReplies: 1Last Post: 01-31-2008, 04:59 PM
Bookmarks