Results 1 to 3 of 3
- 10-09-2012, 02:13 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 5
- Rep Power
- 0
Comparing 3 int value and ordering descendingly
So, thanks for reading. This is where I am at and was just wondering if I'm on the right track. I don't know too much about arrays yet, so I'm trying to avoid using if necessary. I have a program set so far to read a file and display the number of times romeo, juliet and montegue are display and also to right after display their order of time from highest to smallest. I'm having trouble displaying the last part. Is there an easier way than what I'm doing?
Java Code:public class ReadLines { public static void main(String[] args)throws IOException { // This program will read all lines of README.TXT // Make the file available FileReader freader = new FileReader("README.TXT"); BufferedReader inputFile = new BufferedReader(freader); String strLine; int [] anArray; int romeoLines = 0; int julietLines = 0; int montLines = 0; int max, mid, min; // Read the first line. String r = "Romeo"; String j = "Juliet"; String m = "Montague"; while ((strLine = inputFile.readLine()) != null) { if (strLine.contains(r)) { romeoLines++; } if (strLine.contains(j)) { julietLines++; } if (strLine.contains(m)) { montLines++; } } System.out.println("There were " + romeoLines + " lines where Romeo was mentioned"); System.out.println("There were " + julietLines + " lines where Juliet was mentioned"); System.out.println("There were " + montLines + " lines where Montague was mentioned"); { if (romeoLines > julietLines && romeoLines > montLines) System.out.println(); if (julietLines < romeoLines && julietLines < montLines) System.out.println(); if (montLines < romeoLines && montLines < julietLines) System.out.println(); } } }Last edited by Fubarable; 10-09-2012 at 02:25 AM. Reason: code tags added
- 10-09-2012, 02:18 AM #2
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: Comparing 3 int value and ordering descendingly
Wrap the code up in some code tags, and I will take a look. To hard to read without them. [code] ...code... [/code].
Last edited by Fubarable; 10-09-2012 at 02:25 AM. Reason: made code tags look better
- 10-09-2012, 06:56 AM #3
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: Comparing 3 int value and ordering descendingly
Not that it matters, but you have the last set of if statements written as if they are nested, which they are not. Therefore they should all be at the same indentation. As for actually sorting them, from highest to lowest, what you have at the end would not work. You are just going to display which has the highest occurances, and the other two wouldn't print at all. To me, reading from a file is more complex then using an array, so if you were able to write this code, you should feel confident enough to use arrays. You never NEED to use arrays, but they make life a lot easier. Not knowing how to use them is the perfect reason to start using them, therefore I will give you a brief description of how to insert them into your code.
Declare and initialize an array of three indexes at the beginning of your code:
You need to set each index to 0, the best way to do this is to use a for loop. Create a for loop that cycles three times, and have each index set to 0. Even though you know that there are only 3 indexes, you should get in the habit of using <arrayName>.length This displays the total indexes in an array. The for loop would look like:Java Code:int[] ArrayOccur = new int[3];
Hopefully you have seen a for loop used like this before, if not just think about what is happening. The first loop, ix would be 0, so when ArrayOccur is executed, it is actually executing ArrayOccur[0] = 0; The second loop ix would be 1, therefore when ArrayOccur is executed it is actually executing ArrayOccur[1] = 0; And so on...Java Code:for(int ix=0; ix<ArrayOccur.length; ix++) ArrayOccur[ix] = 0;
Use the array indexes the same way you used variables such as romeoLines. An example would be:
This will increase the array at index 0 by one; the same way that it would increase your variable.Java Code:ArrayOccur[0]++;
Now, you get to the end of your code and you have an array of three indexes, filled with occurances of your lines, pretty much in the same spot that your in now but using arrays. Congrats, now onto the sorting...
Sorting:
Alright, the most rudimentary way you could do this is just with simple if statements, but it would require 12 separate if statements. Say your variables were X, Y, and Z. There is one case when X is the greatest, when X is greater then Y and Z; and there is one case when X is the lowest, when X is less then Y and Z. But there are two cases when it is the middle, when it is less then Y and greater then Z, and when it is greater then Y and less then Z. So there are 4 different cases for X alone, so there would be 12 cases total. This is probably the simplest method to describe, you just need 12 separate if statements that each make two comparisons; and if evaluated to true, they output which is the greatest, middle, and lowest. If I were you, I would start of with the comparisons that would make each greatest, and then move on to the comparisons that would make each middle, and then on to the comparisons to make each lowest. This will give your program a more structured feel to it for the user; so as they don't see Romeo is middle, Juliet is greatest, and Montague is lowest.
Similar Threads
-
re-ordering problem please help me...
By gomdohri in forum New To JavaReplies: 1Last Post: 09-08-2011, 07:15 PM -
comparing Graphs and Comparing Matrix
By jetnor in forum New To JavaReplies: 0Last Post: 03-27-2011, 01:40 AM -
finding possible ordering set of vector
By sara12345 in forum New To JavaReplies: 7Last Post: 03-16-2010, 08:35 AM -
what is natural ordering???
By blueduiker in forum New To JavaReplies: 1Last Post: 02-24-2010, 10:51 AM -
getting all possible ordering of vector
By sara12345 in forum New To JavaReplies: 3Last Post: 01-08-2010, 10:21 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks