Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:51:08+00:00 2026-06-12T17:51:08+00:00

I am supposed to take input from a text file which looks like this:

  • 0

I am supposed to take input from a text file which looks like this:

rno year rank
22 2004 12
18 2004 17
43 2005 15
43 2006 45
18 2005 23
22 2005 15
43 2004 16
22 2006 20

I am supposed to sort these and produce the output in the following order:

rno  2004  2005  2006
18    17     23        
22    12     15    20    
43    16     15    45  

If there is no rank available for the roll no. for the corresponding year then print a space

I was able to do this with the following code:

public class Test {
    public static void main(String args[]) throws FileNotFoundException{
        String[] rollNo=new String[20];
        String[] year=new String[20];
        String[] rank=new String[20];
        int count=1;
        int cnt=0;
        Scanner scn=new Scanner(new File("D:/abc.txt"));
        while(scn.hasNextLine()){
            Scanner sc=new Scanner(scn.nextLine());
                while(sc.hasNext()){
                if(count==1){
                    rollNo[cnt]=sc.next();
                    count++;
                }
                else if(count==2){
                    year[cnt]=sc.next();
                    count++;
                }
                else{
                    rank[cnt]=sc.next();
                    count=count-2;
                }
            }
            cnt++;
        }


            TreeSet<String> yr=new TreeSet<String>();
            for(int i=1;i<year.length;i++)
            if(year[i]!=null)
            yr.add(year[i]);
            TreeSet<String> rl=new TreeSet<String>();
            for(int i=1;i<rollNo.length;i++)
            if(rollNo[i]!=null)
            rl.add(rollNo[i]);
            Hashtable<String,String> y1=new Hashtable<String,String>();
            for(int ct=0;ct<year.length;ct++)
                if(year[ct]!=null)
                    if(year[ct].equals("2004"))
                        y1.put(rollNo[ct], rank[ct]);
            Hashtable<String,String> y2=new Hashtable<String,String>();
            for(int ct=0;ct<year.length;ct++)
                if(year[ct]!=null)
                    if(year[ct].equals("2005"))
                        y2.put(rollNo[ct], rank[ct]);
            Hashtable<String,String> y3=new Hashtable<String,String>();
            for(int ct=0;ct<year.length;ct++)
                if(year[ct]!=null)
                    if(year[ct].equals("2006"))
                        y3.put(rollNo[ct], rank[ct]);

            System.out.print(rollNo[0]);
            for(Object obj:yr)
                System.out.print("  "+obj);
                    System.out.println();
            for(Object obj:rl){
                System.out.print(obj+"   ");
            if(y1.containsKey(obj))
                System.out.print(y1.get(obj)+"    ");
            else
                System.out.print(" ");
            if(y2.containsKey(obj))
                System.out.print(y2.get(obj)+"    ");
            else
                System.out.print("    ");
            if(y3.containsKey(obj))
                System.out.print(y3.get(obj)+"    ");
            else
                System.out.print("    ");

            System.out.println();
            }

    }
}

But now the problem is that the hashtables for the years are hardcoded and I want to make this program dynamic meaning whatever the input (no of years, rank or roll no), I want my output in the same format for any input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:51:09+00:00Added an answer on June 12, 2026 at 5:51 pm

    I did it.

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;
    import java.util.StringTokenizer;
    import java.util.TreeMap;
    import java.util.TreeSet;
    public class Nu {
    
        public static void main(String[] args) throws FileNotFoundException{
            int lineCount=0;
            Scanner scn1=new Scanner(new File("D:/abc.txt"));
            while(scn1.hasNextLine()){
                scn1.nextLine();
                lineCount++;//count no of lines for array length
            }
            int count=1;
            int cnt=0;
            String[] yearss=new String[lineCount];//new array for years
            Scanner scn=new Scanner(new File("D:/abc.txt"));
            while(scn.hasNextLine()){//file yearss array
                Scanner sc=new Scanner(scn.nextLine());
                while(sc.hasNext()){
                    if(count==1){
                        sc.next();
                        count++;
                    }
                    else if(count==2){
                        yearss[cnt]=sc.next();//yearss array is filled
                        count++;
                    }
                    else{
                        sc.next();
                        count=count-2;
                    }
                }
                cnt++;
            }
    
            TreeSet<String> yr=new TreeSet<String>();//remove duplicate elements from years and sort them
            for(int i=1;i<yearss.length;i++)
                if(yearss[i]!=null)
                    yr.add(yearss[i]);//add elements to treeset
            TreeMap allRecord=new TreeMap();//treemap sorts keys
            try{
                File file=new File("D:/abc.txt");//used for file searching
                String[] yearArray=(String[])yr.toArray(new String[0]);//convert treeset to String array
                String roll="";
                String year="";
                String rank="";
                BufferedReader in =new BufferedReader(new FileReader(file));//Filereader reads character by character and bufferedReader reads blocks or streams of data
                String value=in.readLine();//read next line 
    
                while(value!=null){
                    StringTokenizer st=new StringTokenizer(value);//pass line into stringtokenizer
                    roll=st.nextToken();//add 1st string to roll
                    year=st.nextToken();//add 2nd to year
                    rank=st.nextToken();//add third to rank
                    TreeMap record=(allRecord.get(roll)!=null)?(TreeMap)allRecord.get(roll):null;//if roll != null then add roll to record else add null
                    //returns year and rank//   System.out.println(allRecord.get(roll));
                    if(record==null)//if null
                        record=new TreeMap();//if null create new instance
                        else
                        allRecord.remove(roll);//remove null from allrecord
                    record.put(year, rank);//put year and rank//every time record is refreshed 
                    allRecord.put(roll, record);//put 22 and record//treemap returns null if get(key)==null
                    value=in.readLine();//read next line
                }
    
                Set keyset=allRecord.keySet();//gets all  keys from allRecord
                Iterator i=keyset.iterator();//iterate keyset
                System.out.print("rno"+" ");
                for(int j=0;j<yearArray.length;j++)
                    System.out.print(yearArray[j]+"   ");
                //print all years
                System.out.println();
                while(i.hasNext()){
                    roll=(String)i.next();//get roll numbers
                    if(!roll.equals("rno"))
                    System.out.print(roll+"    ");
                    else
                        System.out.print("    ");
                    Map record=(Map)allRecord.get(roll);//get record using the rollno
                    for(int j=0;j<yearArray.length;j++){
                        rank=(String)record.get(yearArray[j]);
                        //get rank for the year
                        if(rank!=null){
                        System.out.print(rank+"    ");
                        }
                        else
                        {
                            System.out.print("       ");
                        }
                    }
                    System.out.println("");
                }
            }
            catch(Exception e){e.printStackTrace();}
        }
    }*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This calculator is supposed to take a typed input with spaces, (like 2 +
I am developing an OS X application that is supposed to take input from
This stored procedure is supposed to : Take the userid and Date as input
I am writing a program in C++ which is supposed to take an input
I wrote this little program to practice arrays, which is supposed to take in
The program is supposed to take a file, say data.dat, filled with a list
This is where the variables are defined, now its supposed to take in as
Update: It looks like the problem is when I'm reading the value from the
What is the default folder that classes that extend java.io.Reader take input from? For
Suppose i have array of characters. say char x[100] Now, i take input from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.