Replacing Arrays within ArrayLists, I think?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 704 Views - Last Post: 02 August 2012 - 12:13 PM Rate Topic: -----

#1 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:31 AM

import java.util.*;
import java.io.*;

public class Problem1
{
	public static void main( String args[] ) throws Exception
	{
			// close/reuse this file handle on the next file
			BufferedReader infile = new BufferedReader( new FileReader( "student2courseNums.txt" ) );

			// you declare all needed ArrayLists and other variables from here on.
			ArrayList<String>studentScores = new ArrayList<String>();

			while(infile.ready())
			{
				studentScores.add(infile.readLine());
			}
			infile.close();
			
			BufferedReader infile1 = new BufferedReader( new FileReader( "courseNum2CourseName.txt" ) );
			ArrayList<String>courseName = new ArrayList<String>();
			while(infile1.ready())
			{
				String[] tokens = infile1.readLine().split(" ");
				String iD = tokens[0];
				ArrayList<String>mirror = null;
				{
					mirror = new ArrayList<String>(Arrays.asList(tokens[1]));
					for( int i=0; i<mirror.size(); ++i)
						mirror.set(i, mirror.get(i) + " " + iD);
					courseName.addAll(mirror);
				}
			}
			infile1.close();
			
			Collections.sort(studentScores);
			for(String line : studentScores)
				System.out.println(line);
			
			System.out.println();
				
			Collections.sort(courseName);
			for(String course : courseName)
				System.out.println(course);


	} // END MAIN


I'm trying to print out:

Hector DatabaseManagementSystems AlgorithmDesign AppliedCryptographyNetwork DataCommunicationsComputerNetworks
Jeffy AlgorithmDesign SoftwareEngineering
Johnny IntroAI AIProgramming SoftwareDesignMethodology InterfaceDesignMethodology
Keisha IntroComputerArchitecture IntroCompilerDesign IntroHighPerformanceComputingSystems AdvancedSystemsSoftware DataCommunicationsComputerNetworks
Mario SoftwareEngineering IntroComputerGraphics IntroAI GameDesign
Reddy DatabaseManagementSystems SoftwareEngineering IntroAI DatabaseManagementSystems
Sarada IntroComputerArchitecture IntroCompilerDesign IntroHighPerformanceComputingSystems AdvancedSystemsSoftware DataCommunicationsComputerNetworks

as my final output along with the other bits I already have. If you were to run my program as it is now it would run fine. You would need to do java Problem1 student2CourseNums.txt courseNum2CourseName.txt

The student2CourseNums.txt is:
Hector CS1555 CS1510 CS1653 CS1652
Mario CS1530 CS1566 CS1571 CS1666
Sarada CS1541 CS1622 CS1645 CS1651 CS1652
Reddy CS1555 CS1530 CS1571 CS1555
Johnny CS1571 CS1573 CS1631 CS1635
Keisha CS1541 CS1622 CS1645 CS1651 CS1652
Jeffy CS1510 CS1530

The courseNum2CourseName.txt is:
CS1510 AlgorithmDesign
CS1530 SoftwareEngineering
CS1538 IntroSimulation
CS1541 IntroComputerArchitecture
CS1555 DatabaseManagementSystems
CS1566 IntroComputerGraphics
CS1571 IntroAI
CS1573 AIProgramming
CS1622 IntroCompilerDesign
CS1631 SoftwareDesignMethodology
CS1635 InterfaceDesignMethodology
CS1645 IntroHighPerformanceComputingSystems
CS1651 AdvancedSystemsSoftware
CS1652 DataCommunicationsComputerNetworks
CS1653 AppliedCryptographyNetwork Security
CS1655 SecureDataManagementWebApplications
CS1666 GameDesign
CS1671 HumanLanguageTechnologies

Any idea's how I can get the output I earlier stated along with the output I currently get? I currently get this output by the way:
CS1510 AlgorithmDesign
CS1530 SoftwareEngineering
CS1538 IntroSimulation
CS1541 IntroComputerArchitecture
CS1555 DatabaseManagementSystems
CS1566 IntroComputerGraphics
CS1571 IntroAI
CS1573 AIProgramming
CS1622 IntroCompilerDesign
CS1631 SoftwareDesignMethodology
CS1635 InterfaceDesignMethodology
CS1645 IntroHighPerformanceComputingSystems
CS1651 AdvancedSystemsSoftware
CS1652 DataCommunicationsComputerNetworks
CS1653 AppliedCryptographyNetwork Security
CS1655 SecureDataManagementWebApplications
CS1666 GameDesign
CS1671 HumanLanguageTechnologies

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing Arrays within ArrayLists, I think?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:33 AM

Don't use parallel arrays. Design a Course class and store a List<Course> instead. My tutorial Moving Away From Parallel Arrays provides a good introduction.
Was This Post Helpful? 0
  • +
  • -

#3 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:38 AM

I appreciate the tip, but we aren't allowed to create classes for this program.

View Postmacosxnerd101, on 02 August 2012 - 11:33 AM, said:

Don't use parallel arrays. Design a Course class and store a List<Course> instead. My tutorial Moving Away From Parallel Arrays provides a good introduction.

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:39 AM

Use a TreeMap<Integer, String> then to relate the Course number to name. The TreeMap will order by the key as well, which is the course number.
Was This Post Helpful? 0
  • +
  • -

#5 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:42 AM

If I were you I'd read in the course name/number file first, and save those values into a map of some sore (e.g. Hashtable), where the key is the course number, and the course name is the value

//CS1510 AlgorithmDesign
Hashtable<String, String> courses = new Hashtable<String, String>();
courses.put("CS1510 ", "AlgorithmDesign");


Now when you read in the student text file, you get get the lists of course numbers perstudent and then so something like this

//studentCourses is the array of courseNumbers for Hector
string output = "Hector ";
for( String courseNum : studentCourses )
{
   output += ", " + courses.get( coursNum );
}


Something along those lines might help you
Was This Post Helpful? 0
  • +
  • -

#6 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:44 AM

I'm sorry, but once again, I know that would be A LOT easier to just do mapping but since this is a basic course we are not allowed to use that. Only arrays and arraylists, which sucks.

View Postmacosxnerd101, on 02 August 2012 - 11:39 AM, said:

Use a TreeMap<Integer, String> then to relate the Course number to name. The TreeMap will order by the key as well, which is the course number.

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:46 AM

The problem with parallel arrays (and you're seeing this) is that when you modify one, the corresponding elements aren't modified as well. To implement sorting, you will have to swap corresponding elements in the other List. You will also have to implement sorting yourself. This is one reason I strongly advocate against parallel arrays. While you are under the constraints of the assignment, I would talk with your instructor about why this is such poor practice.
Was This Post Helpful? 1
  • +
  • -

#8 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:54 AM

Yeah I don't know why my instructor would have us do it this way and count it as 15% of our grade when there is an easier way to do it, but the thing is he hasn't taught mapping to the class yet. So he expects us to do it the longer harder way.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:55 AM

The long and hard way should still be OOP, not parallel arrays.
Was This Post Helpful? 0
  • +
  • -

#10 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:57 AM

But with the code I have written I just don't know how to get the desired output I wanted without mapping or having a class I created.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 11:58 AM

View Postmacosxnerd101, on 02 August 2012 - 02:46 PM, said:

The problem with parallel arrays (and you're seeing this) is that when you modify one, the corresponding elements aren't modified as well. To implement sorting, you will have to swap corresponding elements in the other List. You will also have to implement sorting yourself. This is one reason I strongly advocate against parallel arrays. While you are under the constraints of the assignment, I would talk with your instructor about why this is such poor practice.

I already answered that.
Was This Post Helpful? 0
  • +
  • -

#12 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 12:01 PM

Ehh, I am just utterly confused on what I need to sort or even where to start.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 12:03 PM

Instructor just want to see them realize that they have to swap all arrays in the swap method. Not that a bad exercise after all but we are still far from OO. Hope the course does not finish there.

And if you are not allowed to create class you won't create object neither, even further from OO as all your methods and variables will be static.

May be your instructor should teach COBOL :)
Was This Post Helpful? 1
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 12:03 PM

My mistake. I thought you were trying to sort the elements. This is a searching task instead. Given the numbers from your second file, you will want to search through the course numbers List to find the appropriate course number. The name of the course is at the given index in the other List.
Was This Post Helpful? 0
  • +
  • -

#15 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: Replacing Arrays within ArrayLists, I think?

Posted 02 August 2012 - 12:07 PM

Sadly, yes this is the ending of the course.

View Postpbl, on 02 August 2012 - 12:03 PM, said:

Instructor just want to see them realize that they have to swap all arrays in the swap method. Not that a bad exercise after all but we are still far from OO. Hope the course does not finish there.

And if you are not allowed to create class you won't create object neither, even further from OO as all your methods and variables will be static.

May be your instructor should teach COBOL :)


No I wasn't trying to sort. I wanted to print out the name of the student and replace the course numbers with their corresponding course name. That is what I have no idea on what to do or even start. Cause everything I try just crashes.

View Postmacosxnerd101, on 02 August 2012 - 12:03 PM, said:

My mistake. I thought you were trying to sort the elements. This is a searching task instead. Given the numbers from your second file, you will want to search through the course numbers List to find the appropriate course number. The name of the course is at the given index in the other List.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2