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 two 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
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; }}