Jump to content

Facing some problems ...

- - - - -

  • Please log in to reply
5 replies to this topic

#1
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
as continue from my previous thread of how can I pick out the largest number out ..

I had come out a reference code (in this case I finding ages) and it works well ..
Here's the code :
private Map<String, Integer> peopleAge = new HashMap<String, Integer>();


	private void loadData() {

		peopleAge.put("alex", 21);

		peopleAge.put("john", 29);

		peopleAge.put("peter", 18);

		peopleAge.put("mary", 30);

		peopleAge.put("cindy", 18);

		peopleAge.put("chris", 17);

		peopleAge.put("david", 22);

	}


	private String whoIsYoungest() {

		int lastYoungestAge = 0;

		String lastYoungestPerson = null;


		for (String currentPerson : peopleAge.keySet()) {

			int currentPersonAge = peopleAge.get(currentPerson);

			System.out.println("Comparing " + currentPerson + " with age " + currentPersonAge);


			if (lastYoungestPerson == null) {

				// init to the 1st person

				System.out.println("Initialise youngest person to " + currentPerson);

				lastYoungestPerson = currentPerson;

				lastYoungestAge = currentPersonAge;

			} else if (currentPersonAge < lastYoungestAge) {

				System.out.println(currentPerson + " is younger than " + lastYoungestPerson);

				lastYoungestPerson = currentPerson;

				lastYoungestAge = currentPersonAge;

			} else {

				System.out.println(currentPerson + " is same or older than " + lastYoungestPerson);

			}


			System.out.println();

		}


		return lastYoungestPerson;

	}


	public static void main(String[] args) {

		WhoIsYoungerDemo demo = new WhoIsYoungerDemo();

		demo.loadData();

		System.out.println("***** The youngest person is " + demo.whoIsYoungest());

	}


}

But however I'm now stuck ... How can I possible modify this
try {

            FileWriter writer = new FileWriter(file);

            BufferedReader br1 = new BufferedReader(new FileReader(testfile));

            BufferedReader br2 = new BufferedReader(new FileReader(screeningbase));

            File[] listOfFiles = folder.listFiles();

            String strLine = null;


            if ((strLine = br1.readLine()) != null) {

                strLine = strLine.substring(strLine.indexOf(".") + 4);

                for (int i = 0; i < strLine.length(); i++) {

                    if (strLine.charAt(i) == '1') {

                        result1.set(i, true);

                    }

                }

                result1.set(strLine.length(), true);

            }


            while ((strLine = br2.readLine()) != null) {

                strLine = strLine.replaceAll(",", "").substring(strLine.indexOf(".") + 5);

                BitSet bitSet = new BitSet();


                for (int i = 0; i < strLine.length(); i++) {

                    if (strLine.charAt(i) == '1') {

                        bitSet.set(i, true);

                    }

                }

                bitSet.set(strLine.length(), true);

                result2.add(bitSet);

            }


            for (int i = 0; i < result2.size(); i++) {

                BitSet bitSet2 = result2.get(i);

                float nresult = bitSet2.cardinality();

                bitSet2.and(result1);

                writer.append(listOfFiles[i].getName());

                writer.append(",");

                writer.flush();


                for (int j = 0; j < bitSet2.length() - 1 || j < result1.length() - 1; j++) {


                    if (bitSet2.get(j)) {

                        writer.append("1");

                        writer.append(",");

                        writer.flush();

                    } else {

                        writer.append("0");

                        writer.append(",");

                        writer.flush();

                    }


                }


                float ntarget = result1.cardinality();

                float nlast = bitSet2.cardinality();

                nlast = (nlast / (ntarget + nresult - nlast));

                float round = Round(nlast, 3);

                writer.append(round);

                writer.append("\n");

          

            }
to something similar to the above ?
"listOfFiles" will represent the name(e.g. alex) and "round" will represent the age.

#2
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
any pro can help ?

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
What is the 2nd program supposed to do? Can you offer any context for it? What type of files is it reading? Can you be a little more specific on what you're trying to accomplish?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
As much i understand, his original task was to sort files - first by names and then some other criteria such as rounds (e.g. last modified date).
That is what the second program is doing.

Now to make that appear an easier problem for readers, he perceived the major problem is sorting strings (since files would be string names with pointers to actual files) along with some other criteria like age.
That is the first program.

I guess he is having difficulty mapping the first concept to second.

But i agree there are missing pieces. Since information about files are kept in a directory structure or a file allocation table. So i am not sure it that is that type out output he is looking for or is it something completely different.

#5
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts

fayyazlodhi said:

As much i understand, his original task was to sort files - first by names and then some other criteria such as rounds (e.g. last modified date).
That is what the second program is doing.

Now to make that appear an easier problem for readers, he perceived the major problem is sorting strings (since files would be string names with pointers to actual files) along with some other criteria like age.
That is the first program.

I guess he is having difficulty mapping the first concept to second.

But i agree there are missing pieces. Since information about files are kept in a directory structure or a file allocation table. So i am not sure it that is that type out output he is looking for or is it something completely different.


try {

            FileWriter writer = new FileWriter(file);

            BufferedReader br1 = new BufferedReader(new FileReader(testfile));

            BufferedReader br2 = new BufferedReader(new FileReader(screeningbase));

            File[] listOfFiles = folder.listFiles();

            String strLine = null;


            if ((strLine = br1.readLine()) != null) {

                strLine = strLine.substring(strLine.indexOf(".") + 4);

                for (int i = 0; i < strLine.length(); i++) {

                    if (strLine.charAt(i) == '1') {

                        result1.set(i, true);

                    }

                }

                result1.set(strLine.length(), true);

            }


            while ((strLine = br2.readLine()) != null) {

                strLine = strLine.replaceAll(",", "").substring(strLine.indexOf(".") + 5);

                BitSet bitSet = new BitSet();


                for (int i = 0; i < strLine.length(); i++) {

                    if (strLine.charAt(i) == '1') {

                        bitSet.set(i, true);

                    }

                }

                bitSet.set(strLine.length(), true);

                result2.add(bitSet);

            }


            for (int i = 0; i < result2.size(); i++) {

                BitSet bitSet2 = result2.get(i);

                float nresult = bitSet2.cardinality();

                bitSet2.and(result1);

                writer.append(listOfFiles[i].getName());

                writer.append(",");

                writer.flush();


                for (int j = 0; j < bitSet2.length() - 1 || j < result1.length() - 1; j++) {


                    if (bitSet2.get(j)) {

                        writer.append("1");

                        writer.append(",");

                        writer.flush();

                    } else {

                        writer.append("0");

                        writer.append(",");

                        writer.flush();

                    }


                }


                float ntarget = result1.cardinality();

                float nlast = bitSet2.cardinality();

                nlast = (nlast / (ntarget + nresult - nlast));

                float round = Round(nlast, 3);

                writer.append(round);

                writer.append("\n");

          

            }
Ya right, I'm stuck of mapping this code to the 1st code. The type of file it will be reading is a .csv format. The content inside the file will be something like :

| File Name1 | 1 | 0 | 1 | .... | round n.o1 |
| File Name2 | 1 | 0 | 1 | .... | round n.o2 |
| File Name3 | 1 | 0 | 1 | .... | round no.3 |

As you know csv file format every number is separated by commas. The final result will
be will be it will pick out the largest number contained in the round n.o . E.g. round n.o 2 contain the largest number. So File Name2 and round n.o 2 in the text area of my program.

#6
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
anyone can help me ?:(




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users