Question

In java, ask the user for a starting point and ending point. Sort the user list but only display the users within the entered range.

We are learning to use generics and just finished learning the different types of sorting methods such as insertion sort and quicksort.

Please describe the process of how you went about it and how you were able to sort it. The spacing on the text file given to us to work with are separated by tabs.

-----------------------------------------------

//given text file. I displayed it in a table like this so most of it could fit so it wouldn't take as much vertical space. This is only the first 50 and last 50 lines of a 1000 lined text file. I couldn't fit all of it.

user-database.txt

username    password
jfearey0   sDWHDP
cchinnick1 Zrk84YZikwD
gtregien2  zfyqEren
mjahner3   3PGFVitPvC3R
bkinworthy4    Wn18F5m
zjedras5   SAdfRJitsfE
acostain6  B7M1Or
wgomme7    XuVXpKvE7o
csouthby8  Do2CwKsKv
epaddingdon9   NPpWx7f
rkibbeya   sgJ09U8q
eburnettb  4zjgwVjEaXK2
kvinckc    uAzQkZia
mfarlowed  W5F7VcRab
mhinchone  2NSPg8C8K6ty
kshaplandf i0Hj8k
cliddyg    aSyfdUvsl
lelgyh cHcgIWwkhN
epearseyi  hNaNvKhMcG5P
gtuftj 9OiZLVP8gbew
pobrollachaink cQxUopR
hmeadowcroftl  Vu6cfw9FzSwj
kselburnm  hou84RcJlOm
mstubbesn  3UemLmDR2
ktomlino   exIdx6Z
zrodderp   1nJdHPkYZl0
jryallq    0wYSkh3m
etrathanr  3JwzTt2t
cbilles    o1GCJL7b
rhamblyt   zaw9exneq
tmilleu    8mWaiufrHBNk
troysonv   PhpguzTGLjJt
adeloozew  vjAwEqHdG3M
groberdsx  9pBCN9
oodonovany lK7PbC
wcavnorz   igbkTRfuId2
rzanitti10 4qUkW4l96kqH
swilcocks11    NLsCCV9
sthalmann12    QhHNcHcrEKih
wself13    7MKyR4
bgrinley14 Dx7bTnkiiS5q
djohnys15  u2QViAy
apringle16 CxvSrFhv
apullin17  5Yc7Cf1a
jcreevy18  4iRKr3mwIYj
flamborne19    r4gfnEq
ztreppas1a 4mpQTqMRZ
mdownton1b cOlT62
bpavelin1c 262lQFDLWcj
...
...
rmackellarqc    TzxLGo
tgemsonqd  kq2SLslR
jstickelqe XJRVVSCI
tdalbeyqf  UGbPumqpG
mguillonqg 7YOzLJm8i0X
cfeveryearqh   6EZZNQgTP
wmckimmeyqi    IOkKQHre3ERm
nredholesqj    uFUD1ZBmu
jbubearqk  7IrBzezU
ttenbrugql E1n6aU96l4bZ
hwestmerlandqm yjpORg
ppflegerqn 8MZtXui
efratczakqo    Vvz5NQCMQy0
mbehnqp    JutFJOkpcF7E
bbenneqq   RAtDf9SCAd
pwikeqr    x75O6yRq
ldanilenkoqs   aEjjEnqW
tstanettqt hlFnSF
eraithmillqu   27mKwA9c8D
ckeatleyqv 6Li0xh
dringwoodqw    PFlV898oj07
gbiggsqx   S9HIfo47
bsutterfieldqy 7ANqKpH9e6Y
dsouthardqz    k2dRBOFC
mhatrickr0 zRxrlAkX
aboaysr1   EMlwj8UIgsFc
acampanar2 Z9jiJ8P5AdY
rmuffr3    lfJJswN
cdewsr4    2bq4TN
dbauserr5  TDDIuQtkE
cchongr6   t9LpGWbyi4
rhalesr7   XhouLr3RRy
ephibbr8   ZFwGht
cderrettr9 3fOPjsM8C
sdreverra  wpygRTf
jtruettrb  eCfBl2
uhusonrc   BypvE43Mu5C7
lcotesfordrd   oodwULJh0YT
gwinkellre U5JLvXTWIch
gcolbrunrf RmB7Vli
rsimondrg  Nha67NZbdIIV
bburmasterrh   uz8tDPT2
sdugaldri  y09ruuq
lborgnerj  eZVWbipm
clibrerosrk    vVqOlNVYAYr
balgaterl  1uQGHD9a
lfewrm HaWPhJHDrdK0
gsallern   MPN2Ee1FwPU
krollinsonro   75oxXO
tlyddiardrp    CQ2EZEWhNT
nplumleyrq b1vyem6hz
eensleyrr  EmnTyCOrm

-----------------------------------------------

//Output

Test Case 1

Standard Input                 Files in the same directory
0 3
  • user-database.txt
Enter a starting point and ending point\n
              0FVynO            nmckie2w\n
              0tfzt1         slaverock30\n
              24iDoX          csidsaffks\n
 

. . .

Test Case 4

Standard Input                 Files in the same directory
493 562
  • user-database.txt
Enter a starting point and ending point\n
           fpCqTjEfk          cjacmardpr\n
           gAmfepkQX           cbidgodc4\n
           GheSmEhte          pianillipm\n
           GMHoz13Ko          lmcamishdl\n
           Gr3yzaUYQ           sheeranc8\n
           Gv2zAbRx6           tcraddy8c\n
           h7FUxSdUz            rrosingb\n
           HDLFE0tEN             rfryd1l\n
           HIlCcbD87             htongo8\n
           hXB6xBgNW           bfogdeneg\n
           HzqustkgH          mhansardpv\n
           I44S3hCp9        ydranfield80\n
           IE749MAQu         dhouseman9a\n
           IWA54iHTY          ddongallp2\n
           j2mGL93OM           bdamantgk\n
           J3OR5poLZ          ewortmang8\n
           JKnLGhvj4            zajeaneh\n
           jlSNMqTn6        sstrowlgerim\n
           JQAUPZkev       gmacgaugheyl1\n
           jU2Cu8XJB       cyanyushkin77\n
           k4M2TWOM4        sgrinvalds3p\n
           kHHgzpjSR         dberriman4l\n
           klPXCs37v        tsalisburyps\n
           kSlAPYDL7       ofeldmessere3\n
           l3LhZPWjb          dmathers8u\n
           l6uqi7nMu    okristoffersenj9\n
           LVRJEWkyP        dgainsfordkv\n
           M0h2VMLrZ       cmacilurick9c\n
           m9i9TN6BK       espranklingb7\n
           mFcRNqYZL           awonfore0\n
           mHxrnKDXD           gnibley9w\n
           mocvC208z       lkonzelmann7j\n
           MpFjyUyC5         aspadazzi99\n
           N1Cuz9TTF            sguyotgu\n
           NcU0NECXU        kmcillrickls\n
           NKFImM5Sb           mjuliennd\n
           nKws2Qb14          mkohlertjj\n
           NnIyaIitY          cdowdellfe\n
           NojYCiYP0           nadamikaf\n
           NTrkUygXB     cfahrenbacherfa\n
           o4XAi7p9h            eextil8n\n
           O8f8DPaC2           gknoblea9\n
           OfWBB4vWu       nsmitherhamil\n
           oguGg6cGe         ddecastrifz\n
           OTvvhNLQC          chaylandl0\n
           OvtYc7luY             ljebbgo\n
           oWZSCHRaI      vbirchenheadh6\n
           Oy57hHSXO      clethabridge5u\n
           pOb8xCLFo           mansell4g\n
           qmdTiuZUY        apettipherkg\n
           qXJHweywX         ajosupeit5e\n
           qzIWWFh0h         aknowldenm2\n
           R7DafjM5o          estallan56\n
           RnG27Kv6R         rcassellsh9\n
           Rzx4IXDdu        bflanniganpy\n
           s9L7Qu7wg          clacheze1v\n
           SaKBGrmqP          kruggierep\n
           ScG1cU86I          ballcoatbf\n
           ScNtqdaKU         kbrenston9t\n
           Sgrt9yJZu          dbrisleydn\n
           sH45XC2N6            bmitiehb\n
           sY96e5dvF         ccarnihanab\n
           t3d2RLHRK           apemblem5\n
           TDDIuQtkE           dbauserr5\n
           TDEmhrjja      pcopperwaite9n\n
           TJyeWJWUv           bneamesmm\n
           TLONniHHE             aaltyok\n
           tpXu8iQGW         bwillimotcu\n
           tQ8JiPSD5           ecurrorbu\n

Test Case 5

Standard Input                 Files in the same directory
700 780
  • user-database.txt
Enter a starting point and ending point\n
          Zg6O2VwCy4            sellit2h\n
          ZiFV7HlE7x            gdarcye5\n
          zrvUTCm4Ak           afrouded1\n
          ZZIbDB6wkn           vkesley1o\n
         0k7S0fqrj5e          rhutchines\n
         0YhBkYQad5j           mieldenq9\n
         0yZJIkI2RS8        mdudlestonb6\n
         19WZBAHfxcG          ocaughandt\n
         1nJdHPkYZl0            zrodderp\n
         1QZP9k7sv9B          dbrighamdc\n
         1wUbPJReFSy           mgolderol\n
         262lQFDLWcj          bpavelin1c\n
         2oQvZgKXXOX           feagleso7\n
         3DBnOA65EMH           nmashrolh\n
         3DMNRaTVUya             lsachdz\n
         3qnLtHosEjJ          cwhostonkc\n
         3Vzfp7DoIYz         lbigadikegh\n
         4iRKr3mwIYj           jcreevy18\n
         4SMLsOVcPjW            pohearfb\n
         68ysB1EPpUm           dboggisjf\n
         7ANqKpH9e6Y      bsutterfieldqy\n
         7YOzLJm8i0X          mguillonqg\n
         8c6sms5h0bh         amcdonald5w\n
         8I8okKjF8So           tleydenee\n
         8iG5Cmt3CEv          sselburnju\n
         8IRBACVVF9b        gcullinane6t\n
         8J3t8btjJpB          nduckelscg\n
         8lKhiRFGiv6         gsearston7b\n
         94pkEKTRpk3        rstandeven1g\n
         9LYPgW2CHVT          eelsmerehp\n
         9w8n0eY7D7j       abeckinsall4o\n
         aC22JXJL353          rsaddleriy\n
         AKvWFsTxqz0            csoulegw\n
         aNBuTqiQCYU          fmcileenjq\n
         AOSR6kydPFj          wclayill66\n
         BiTFuYnLZwv        akingsnodecz\n
         BJmJD4e7OrG          awignalle9\n
         bluQNDGT02y          ndurbannnr\n
         BuSHu3BDLgE       mweatherdono6\n
         CNXLm0JCPMl       lferrarelloly\n
         CP3C5O6w6HH          dspavinslt\n
         Cr1Ns4usZam        poakenfull52\n
         D1A7jrQHOWg           ahassin5q\n
         DBI1J7sEvYz           csackurdp\n
         DsdeuFDOQRZ           hroback5j\n
         e2fdPmbFC7I         bcarabinedw\n
         e2QWQ5cruaq            mlocal8q\n
         E3iw5DrRw6b            hlavis3t\n
         E9v0mq0N69e         emaccourtlw\n
         EBD3HdoeANW           nflores6f\n
         eG2bMlcXMi1           xocrevyhg\n
         eri2TzOftqU          mgoodman37\n
         ewwJ0BE8U4h           bkaplandr\n
         fni5SyUMpDB        tmarioneauhk\n
         Ftt2YG5eUAM           bminall3h\n
         gajJAcjtKSn          kliggensfv\n
         gba2sLRXcTC         kchetwyndbg\n
         GDS9BfBUZLI          wcringleof\n
         GJzHodAzO6y          rbeadham86\n
         gz2oh9VIWpH          tharmson5n\n
         hET4lkPrMDt            aflooki6\n
         hou84RcJlOm           kselburnm\n
         hXZwYTbsq3E          eclowneynv\n
         IE8HOlvdx9R          aladewighh\n
         igbkTRfuId2            wcavnorz\n
         J2iYGO8mamJ          mlanninger\n
         JkvUmerhCtg         ogutridge60\n
         JlSOoGCIZ73          craymontlr\n
         JqHXMqcCL9I     rkrysztowczyk9k\n
         LqjHT81to2M             kureylq\n
         Lush2c3j3RW           mrhymerq1\n
         M68kh5KDu2G            sboult9h\n
         MHI4SUoS94x           rdilnot8x\n
         MiKp5piLGOL         rcourtesej2\n
         Mn0ebEtZ8r4          sscobbiemn\n
         MPN2Ee1FwPU            gsallern\n
         mtusap1gROi           aphayrefr\n
         MwWHXy3m4Cq        dmaddaford29\n
         n290lUZMbJY         dgrassick43\n
         NGkAqPCkIDe           clewson7f\n

-----------------------------------------------

output on the screen because the formatting looks weird once I uploaded the question. The formatting is the same for all test problems.

Test Case 2 Standard Input Files in the same directory 10 • user-database.txt Enter a starting point and ending point\n OFVyn

Test Case 3 Standard Input Files in the same directory 10 30 • user-database.txt Enter a starting point and ending point\n 4y

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the code for the question. I tested with the 100 records you gave. So my output screen may not be the same as that when you 1000 records. But the program will work for your full file as well. Let me know if any issues, I'll help. Please do rate the answer if it helped. Thank you.
If you are using eclipse as your IDE, make sure you don't put the input file inside the src folder. Just put it directly under the project folder, otherwise you will get file not found error.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class SortUsers
{
   public static void main(String[] args){
       String filename = "user-database.txt";
       Scanner infile = null;
       try {
           infile = new Scanner(new File(filename));
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
       ArrayList<String> usernames = new ArrayList<String>();
       ArrayList<String> passwords = new ArrayList<String>();
       int start, end;
       Scanner input = new Scanner(System.in);
      
       System.out.println("Enter a starting point and ending point");
       start = input.nextInt();
       end = input.nextInt();

      
       //skip the first line which is column header line
       infile.nextLine();
      
       //read remaining data lines
       while(infile.hasNext()){
           usernames.add(infile.next());
           passwords.add(infile.next());
       }
       infile.close();
      
       //selection sort on passwords
       int minIdx;
       String temp;
       for(int i = 0; i < passwords.size();i++){
           minIdx = i;
           for(int j = i+1; j < passwords.size(); j++){
               if(passwords.get(j).compareToIgnoreCase(passwords.get(minIdx)) < 0)
                   minIdx = j;
           }
          
           //swap the passwords in i and minIdx positions
           temp = passwords.get(i);
           passwords.set(i, passwords.get(minIdx));
           passwords.set(minIdx, temp);
          
           //swap the corresponding usernames in i and minIdx positions so
           //the relationship between username and password preserved
           temp = usernames.get(i);
           usernames.set(i, usernames.get(minIdx));
           usernames.set(minIdx, temp);
       }
      
      
       for(int i = start; i < usernames.size() && i < end; i++)
           System.out.printf("%20s %20s\n", passwords.get(i), usernames.get(i));
   }
}

Console X <terminated> SortUsers [Java Application] E:\Progra Enter a starting point and ending point 03 BWYSkh3m jryalla 1nI

Add a comment
Know the answer?
Add Answer to:
In java, ask the user for a starting point and ending point. Sort the user list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java Program that reads the following: Ask the user for a balance. From the...

    Write a Java Program that reads the following: Ask the user for a balance. From the accounts.dat file, display all the accounts that have a balance of at least what the user entered. accounts.dat is a binary file. Each entry contains a credit card number (long), a balance (double), and a cash back flag (boolean). Question 1 10 points Ask the user for a balance. From the accounts.dat file.display all the accounts that have a balance of at least what...

  • Create a python script to manage a user list that can be modified and saved to...

    Create a python script to manage a user list that can be modified and saved to a text file. Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit) List of user accounts, error messages when appropriate Output text file consisting of pairs of username and passwords, separated by a colon (:) without...

  • ((Java)) Ask the user for some letters. Display, in alphabetical order, a list of email address d...

    ((Java)) Ask the user for some letters. Display, in alphabetical order, a list of email address domains from users that have a first name that begins with the inputted letters. A domain is the characters after the @ symbol. [email protected] Standard Input                 Files in the same directory Hea mock-data.csv Output == Domain Lookup ==\n Enter some letters\n Here are the domains of the users that begin with Hea\n free.fr\n Found 1 domain(s)\n Standard Input                 Files in the same directory He...

  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • In Java, using Collections, Ask the user for Strings until he or she types exit. Display...

    In Java, using Collections, Ask the user for Strings until he or she types exit. Display those String without duplicates in alphabetical order. Please provide comments to get a better understanding. //////////////////////////////////////////////////////////////////Required output\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Test Case 1 Standard Input                 cENTER bENTER bENTER cENTER aENTER aENTER exit Enter a line of text\n Enter a line of text\n Enter a line of text\n Enter a line of text\n Enter a line of text\n Enter a line of text\n Enter a line of...

  • In Java, using only recursion. Ask the user for a list of integers. Display the greatest...

    In Java, using only recursion. Ask the user for a list of integers. Display the greatest number in the sequence. Allow any non-integer to end the input. DO NOT USE LOOPS. We are currently learning recursion and can only use recursion. Please provide notes so to better understand. Please describe how inputting a string will end the input section and then go on to execute what number is greater within the entire user input. -------------------------------------------------------------------------------- Standard Input                 5 20...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT