d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Reading Certain Parts Of A File
Add Reply New Topic New Poll
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 8 2015 02:49pm
Ill preface this with the assignment questions:
Code
A file contains information about books (title, author, ISBN number). Write a program that determines the number of distinct authors for the books in this file.
A file contains information about students (name, student number, number of hours, gpa). Write a program that creates 3 lists of these students: one sorted by name, one sorted by gpa, and one sorted by the number of hours. The program should display these lists.
A file contains information about students (name, student number, number of hours, gpa). Write a program that lists the frequency (in decreasing order) of occurrence of students names.


All 3 files are different but formatted the same for the most part.
Im thinking read the file in, maybe tokenize the file, and the perform the requested operations?

Is there better logic to this or am i on the right track?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 8 2015 05:45pm
Language? File format (CSV, XML, JSON)? Number of entries? Preferred backend?

If this is a large number of entries it would be trivial to read them into a SQL database and work from there.

If the data is CSV or other formmated, then just read into a hash and store each hash in an array.

Code

IO.foreach('file.txt') do |line|
line = line.split(";")
hash = { :name => line[0], :studentid => line[1], :hours => line[2] }
storage_array.push(hash)
end

#sort students by name alphabetically
storage_array.sort_by { |hash| hash[:name] }


Of course you probably won't have the builtins like ruby does, but the concept should remain the same.
Member
Posts: 1
Joined: Mar 8 2015
Gold: 0.00
Mar 8 2015 06:14pm
Also working on this, I figured out how to do the first part. This is in java by the way and we are supposed to use a simple text file. For the first part just read in the text file into a string using BufferedReader, split the commas, and then apply this logic:
if(s1.startsWith(" ") && !s1.contains("0") && !s1.contains("6"))
{
set.add(s1);
}

Any idea on how to do the second part?

Also I will copy and paste the three different text files:

For #1 we are using:

The Book of Even Primes, Bubba Dawg, 6666666666666
Algorithms, Thomas Cormen, 9780262033848
Design Patterns, GOF, 0000201633612
C++ Programming Language, Bjarne Stroustroup, 0000201889544
Applying UML and Patterns, Craig Larman, 0000131489062
Combinatorics for Computer Science, Gill Williamson, 0000881750204
Partying in Athens, Bubba Dawg, 0000000000000
Software Engineering, Roger Pressman, 0000072853182
Tom Sawyer, Mark Twain, 5763542378602
What is a Classroom, Bubba Dawg, 0606060606060
Huckleberry Finn, Mark Twain, 2389476109875
Data Structures in C++, Timothy Budd, 0000201308797
The Prince and the Pauper, Mark Twain, 7835654290127
My 10 Years as a Freshman, Bubba Dawg, 7654987654320


For #'s 2 AND 3 we are using:

Jane Doe, 365487645, 56, 2.87
John Smith, 765432987, 87, 3.12
Bubba Dawg, 666666666, 167, 0.01
Barbara Brilliant, 999999999, 102, 3.99
John Smith, 888888888, 22, 3.71
Jane Doe, 555555555, 48, 3.34
Billy Bob Hogg, 000000000, 108, 0.1
Barbie Doll, 345654321, 99, 2.3
Ken Doll, 111111111, 67, 0.2
Bill Gates, 222222222, 105, 2.03
Madonna, 777777777, 3, 1.4
Paul Politician, 000000001, 236, 0.0
John Smith, 444444444, 42, 2.89


This post was edited by Thatdude1 on Mar 8 2015 06:21pm
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 8 2015 06:39pm
Quote (Thatdude1 @ 8 Mar 2015 19:14)
Also working on this, I figured out how to do the first part. This is in java by the way and we are supposed to use a simple text file. For the first part just read in the text file into a string using BufferedReader, split the commas, and then apply this logic:
if(s1.startsWith(" ") && !s1.contains("0") && !s1.contains("6"))
{
set.add(s1);
}

Any idea on how to do the second part?

Also I will copy and paste the three different text files:

For #1 we are using:

The Book of Even Primes, Bubba Dawg, 6666666666666
Algorithms, Thomas Cormen, 9780262033848
Design Patterns, GOF, 0000201633612
C++ Programming Language, Bjarne Stroustroup, 0000201889544
Applying UML and Patterns, Craig Larman, 0000131489062
Combinatorics for Computer Science, Gill Williamson, 0000881750204
Partying in Athens, Bubba Dawg, 0000000000000
Software Engineering, Roger Pressman, 0000072853182
Tom Sawyer, Mark Twain, 5763542378602
What is a Classroom, Bubba Dawg, 0606060606060
Huckleberry Finn, Mark Twain, 2389476109875
Data Structures in C++, Timothy Budd, 0000201308797
The Prince and the Pauper, Mark Twain, 7835654290127
My 10 Years as a Freshman, Bubba Dawg, 7654987654320


For #'s 2 AND 3 we are using:

Jane Doe, 365487645, 56, 2.87
John Smith, 765432987, 87, 3.12
Bubba Dawg, 666666666, 167, 0.01
Barbara Brilliant, 999999999, 102, 3.99
John Smith, 888888888, 22, 3.71
Jane Doe, 555555555, 48, 3.34
Billy Bob Hogg, 000000000, 108, 0.1
Barbie Doll, 345654321, 99, 2.3
Ken Doll, 111111111, 67, 0.2
Bill Gates, 222222222, 105, 2.03
Madonna, 777777777, 3, 1.4
Paul Politician, 000000001, 236, 0.0
John Smith, 444444444, 42, 2.89


Im attempting a HashMap for all parts so i can reuse the code for all three parts. Ill post it in a sec with an explanation
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 8 2015 07:36pm
Code
public static void main(String[] args) throws IOException {
Map<String, String> myMap1 = new HashMap<String, String>();
List<Map<String, String>> myMap = new ArrayList<Map<String, String>>();

String fileName = "src/books.txt";
List<String> line = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());

for(int i = 0; i < line.size(); i++) {
String[] parts = line.get(i).split(", ");
myMap1.put("Book", parts[0]);
myMap1.put("Name", parts[1]);
myMap1.put("ID", parts[2]);
myMap.add(i, myMap1);
i++;
//System.out.println(myMap);
}


Im having a problem with the for loop. It wont write to myMap after the first loop trip. Throws the error:
Code
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 1


It does exactly what i want it to on the first trip and then its like it can no longer add to myMap
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 8 2015 07:41pm
change: myMap.add(i, myMap1);
to: myMap.add(myMap1);

http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-int-E-

Quote
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())


your strategy looks weird though.

This post was edited by carteblanche on Mar 8 2015 07:43pm
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 8 2015 07:46pm
Quote (carteblanche @ 8 Mar 2015 20:41)
change: myMap.add(i, myMap1);
to: myMap.add(myMap1);

http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-int-E-



your strategy looks weird though.


output:
Code
[{Book=The Book of Even Primes, ID=6666666666666, Name=Bubba Dawg}]
[{Book=Design Patterns, ID=0000201633612, Name=GOF}, {Book=Design Patterns, ID=0000201633612, Name=GOF}]
[{Book=Applying UML and Patterns, ID=0000131489062, Name=Craig Larman}, {Book=Applying UML and Patterns, ID=0000131489062, Name=Craig Larman}, {Book=Applying UML and Patterns, ID=0000131489062, Name=Craig Larman}]
[{Book=Partying in Athens, ID=0000000000000, Name=Bubba Dawg}, {Book=Partying in Athens, ID=0000000000000, Name=Bubba Dawg}, {Book=Partying in Athens, ID=0000000000000, Name=Bubba Dawg}, {Book=Partying in Athens, ID=0000000000000, Name=Bubba Dawg}]
[{Book=Tom Sawyer, ID=5763542378602, Name=Mark Twain}, {Book=Tom Sawyer, ID=5763542378602, Name=Mark Twain}, {Book=Tom Sawyer, ID=5763542378602, Name=Mark Twain}, {Book=Tom Sawyer, ID=5763542378602, Name=Mark Twain}, {Book=Tom Sawyer, ID=5763542378602, Name=Mark Twain}]
[{Book=Huckleberry Finn, ID=2389476109875, Name=Mark Twain}, {Book=Huckleberry Finn, ID=2389476109875, Name=Mark Twain}, {Book=Huckleberry Finn, ID=2389476109875, Name=Mark Twain}, {Book=Huckleberry Finn, ID=2389476109875, Name=Mark Twain}, {Book=Huckleberry Finn, ID=2389476109875, Name=Mark Twain}, {Book=Huckleberry Finn, ID=2389476109875, Name=Mark Twain}]
[{Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}]
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper


2 issues i see, its skipping every other line in the .txt file but thats probably because my delimiter is ", " and doesnt have \n as well...each line doesnt end in a comma.
and as it goes thru the loop it seems to be writing subsequent lines i times?

edit: put the print statement of myMap outside the loop and got:
Code
[{Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}, {Book=The Prince and the Pauper, ID=7835654290127, Name=Mark Twain}]
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper
The Prince and the Pauper


so its overwritting every trip

This post was edited by Wacko on Mar 8 2015 07:47pm
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 8 2015 09:05pm
Solved this, Abduct is an eye opener as usual. Tends to see things way before i do ;) much thanks
http://pastebin.com/HGxatSZm

finished code^
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll