Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

CAN AN EXPERT HELP FIX MY CODE

here is my code

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class User implements Comparable<User> { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } @Override public int compareTo(User other) { int cmp = this.password.length() - other.password.length(); if (cmp != 0) { return cmp; } cmp = this.password.toLowerCase().compareTo(other.password.toLowerCase()); if (cmp != 0) { return cmp; } return this.username.toLowerCase().compareTo(other.username.toLowerCase()); } @Override public String toString() { return String.format("%20s%20s", password, username); } public static void main(String args[]){ List<User> l= new ArrayList<>(); try{ BufferedReader br= new BufferedReader(new FileReader(new File("user-database.txt"))); String st, username, password; while((st=br.readLine())!=null){ username= st.split("\t")[0]; password= st.split("\t")[1]; l.add(new User(username,password)); } br.close(); BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); Collections.sort(l); System.out.println("Enter a starting point and ending point"); String inp[]= in.readLine().trim().split(" "); int start= Integer.parseInt(inp[0]); int end= Integer.parseInt(inp[1]); for(int i=start;i<end;i++){ User u= l.get(i); System.out.println(u); } } catch(IOException e){ System.out.println(e.getMessage()); } } }

it should display

Enter a starting point and ending point\n
493 562ENTER
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....

TJyeWJWUv bneamesmm\n
TLONniHHE aaltyok\n
tpXu8iQGW bwillimotcu\n
tQ8JiPSD5 ecurrorbu\n

 

however. it is off one line and producing an extra line in the beginning and lacks a line at the end and instead displays:

Enter a starting point and ending point\n
493 562ENTER
fMTEc9nPe smcconway7w\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...
TJyeWJWUv bneamesmm\n
TLONniHHE aaltyok\n
tpXu8iQGW bwillimotcu\n

i have tried changing 

for(int i=start;i<end;i++){
User u= l.get(i);
System.out.println(u);
}

to 

for(int i=start;i<=end;i++){
User u= l.get(i);
System.out.println(u);

and 

for(int i=start-1;i<end;i++){
User u= l.get(i);
System.out.println(u);
}

and i have tried changing loop iterations 

if (end > l.size()) {
end = l.size();
}
for (int i = start; i < end; i++) {
User u = l.get(i);
System.out.println(u);
}

but those solutions do not work, can another solution be provided to help fix my code, can an expert help fix my code so that it removes the first line in bold and adds the last line in bold to display correctly, I am just one line off

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education