Help sorting Int values from Strings and returning top result
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Help sorting Int values from Strings and returning top result

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    Help sorting Int values from Strings and returning top result

    I have a java program that is scanning a text file with the following information:


    Advanced Martial Arts Diploma

    (Burbanks Ninja School)

    Adkins, Scott 72

    Black, Jack 44

    Carradine, David 81

    Chan, Jackie 75

    Chow, Stephen 77

    Chow, Yung-Fat 79

    Jaa, Tony 71

    Lee, Bruce 76

    Li, Jet 76

    Norris, Chuck 71

    Oedekerk, Steve 58

    Reeves, Keanu 62

    Uwais, Iko 75

    Yen, Donnie 80

    Yeoh, Michelle 79

    Zhang, Ziyi 75



    Pilot (Starfleet Academy)

    Archer, Jonathan 71

    Janeway, Kathryn 74

    Kirk, James T. 79

    Picard, Jean-Luc 85

    Pike, Christopher 80

    Riker, William 79



    Advanced Action Heroes (Disney U)

    Aladdin 91

    Benedict, Julius 45

    Brewer, Gordy 82

    Conan 60

    Duck, Donald 40

    Iincredible, Mr. 85

    Matrix, John 64

    Mouse, Mickey 51

    Owens, Ray 82

    Poppins, Mary 85

    Quaid, Douglas 75

    Richards, Ben 68

    Simba 80

    Slater, Jack 80

    Terminator, The 90

    Tully, Louis DNF



    Attire, Poise & Grace



    Bieber, Justin 33

    Bullock, Sandra 80

    Ciccone, Madonna 60

    Cyrus, Miley 40

    Gaga, Lady 50

    Lauper, Cindi 55

    Minaj, Nicki 45

    Sarkisian, Cher 75




    That's All, Folks!

    Here is my code so far to scan the text file and return the formatted values:

    Code:
        public static void main(String[] args) throws Exception {
            Scanner fileScanner = new Scanner(new File("lists.txt"));
            String line = fileScanner.nextLine().trim();
    
            String programName;
            String schoolName;
            String topStudent;
            int topGrade;
    
    
    
            while (!line.equals("That's All, Folks!")) {
                //extract a program's information
                System.out.println("");
                System.out.println(line.trim());
                line = fileScanner.nextLine().trim();
                line = line.replaceAll("\\s+", " ");
                System.out.println("");
                while (!line.isEmpty()) {
        
                    //deal with one student                
                    System.out.println(line);
                    line = fileScanner.nextLine().trim();
                    line = line.replaceAll("\\s+", " ");
                }
                //displays summary information for a program    
                line = fileScanner.nextLine().trim();
            }
        }
    I'm supposed to check these strings (some have one, and some have two names. One has a DNF instead of a grade). How would I go about only returning the name and top grade of each person in each program to look like below:

    Advanced Martial Arts Diploma
    (Burbanks Ninja School)
    David Carradine

    Pilot
    (Starfleet Academy)
    Jean-Luc Picard

    Advanced Action Heroes
    Disney U)
    Aladdin

    Attire, Poise & Grace
    Sandra Bullock

    ______________________

    This is where I'm absolutely stuck. I know that I'm probably supposed to parse the int value from the strings and compare them with an if statement, but I do not know how to code that. I need to also take into account the one DNF grade. Please help!

  2. #2
    Join Date
    Sep 2013
    Location
    Chennai
    Posts
    5

    Re: Help sorting Int values from Strings and returning top result

    Try this...


    I have used same input file what you have posted...

    Hope this will help you.



    package com.java_sample;

    import java.io.File;
    import java.util.Scanner;

    public class SortIntFromString {
    public static void main(String[] args) {
    try {
    Scanner fileScanner = new Scanner(new File("lists.txt"));
    String line = fileScanner.nextLine().trim();

    int emptyCounter = 0;
    String[] markStrArray = new String[50];
    int index = 0;

    while (!line.equals("That's All, Folks!")) {

    //Checking whether line is empty or not
    if (line.isEmpty()) {
    emptyCounter++;
    } else {
    emptyCounter = 0;

    //Checking whether line contains mark or not
    if (!line.matches(".*\\d.*")) {
    if (!line.contains("DNF")) { //Do DNF evaluate here
    System.out.println(line.trim());
    }
    } else {
    markStrArray[index++] = line.trim();
    }
    }

    //Finding max mark for each set, before start next set
    if (emptyCounter >= 3) {
    //Finding biggest mark
    String maxMarkStudInfo = "";
    int maxMark = 0;

    for (int i = 0; i < index; i++) {
    String[] info = markStrArray[i].split(" ");
    int mark = Integer.parseInt(info[info.length - 1]);

    if (mark > maxMark) {
    maxMark = mark;

    maxMarkStudInfo = (info.length < 3)
    ? info[0] : info[1] + " " + info[0].substring(0, (info[0].length()-1));
    }
    }

    System.out.println(maxMarkStudInfo);
    System.out.println("\n");

    //Reset counter and index
    emptyCounter = 0;
    index = 0;
    }
    line = fileScanner.nextLine().trim();
    }
    } catch (Exception e) {
    System.out.println("Error occured : ");
    e.printStackTrace();
    }
    }

    }

  3. #3
    Join Date
    Oct 2013
    Posts
    2

    Re: Help sorting Int values from Strings and returning top result

    Thank you so much! That worked perfectly!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center