Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple media type program

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple media type 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 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;
    }}
    Last edited by helloworld922; December 30th, 2011 at 05:56 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simple media type program

    I don't understand why your output does not include what is printed by this line:
    System.out.println("enter N and Q :");

    Can you post the FULL contents of the command prompt console from when you execute the code?
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    To see what your code is doing, you need to add lots of printlns to print out the values of the variables as they change and to show the logic flow.
    Last edited by Norm; December 31st, 2011 at 07:36 AM.

Similar Threads

  1. Media Player Help in java
    By Tajinder in forum Java SE APIs
    Replies: 2
    Last Post: September 27th, 2011, 12:00 PM
  2. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  3. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  4. Stream with servlet in windows media
    By migalvcon in forum Java Servlet
    Replies: 0
    Last Post: May 19th, 2010, 05:03 AM