Results 1 to 4 of 4
  1. #1
    jamess is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Exclamation String Parsing Problem

    I have the following data:

    Java Code:
    Maths abc10 4 def08 6 ;  
    English hrd45 3 ngh05 10 ; .
    The word at the beginning is a keyword which is also in an enum, the following data are username and login pairs (there can be an unlimited number of these. The data for each keyword is terminated by ';' and the data is terminated by '.'

    I'm using the scanner class but I can't get it to loop so that I can store it in a class to be used for statistical analysis - the output should be like below -

    Java Code:
    Maths abc10 4   
    Maths def08 6   
    English hrd45 3  
    English ngh05 10
    Any Ideas? Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    jamess is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Here's the code

    Java Code:
    import java.util.Scanner;
    
    public class Statistics {
    	
    	public enum Courses {Programming, Logic, Hardware, DiscreteMathematics, MathematicalMethods, ComputerSystems, Graphics};
    
    	public static void main(String[] args) {
    		Statistics stats = new Statistics();
    		stats.parseInput();
    	}
    	
    	private void parseInput(){
    		Course cs = new Course(); 
    		Scanner sc = new Scanner(System.in);
    		String token;
    		int index = 0;
    		while (sc.hasNext()) {
    			token = sc.next();
    			if (isCourseToken(token)) {
    				Courses coursename = Courses.valueOf(token);
    				cs.coursename = coursename;
    			} else {
    				if (isInteger(token)) {
    					int mark = toInteger(token);
    					cs.marks[index] = mark;
    					cs.grades[index] = toGrade(mark);
    				} else {
    					cs.students[index] = token;
    				}
    			}
    			index++;
    		}
    	}
    	
    	private boolean isCourseToken(String token) {
    		try {
    			Courses.valueOf(token);
    		} catch (IllegalArgumentException exc) {
    			return false;
    		}
    		return true;
    	}
    	
    	private boolean isInteger(String token) {
    		try {
    			Integer.parseInt(token);
    		} catch (NumberFormatException exc) {
    			return false;
    		}
    		return true;
    	}
    	
    	private int toInteger(String token) {
    		return Integer.parseInt(token);
    	}
    	
    	private char toGrade(int mark) {
    		String grades = "FFFEDCBAA++";
    		return grades.charAt(mark);
    	}
    }
    
    class StudentData {
    	String login;
    	int mark;
    }
    
    class Course {
    	Statistics.Courses coursename;
    	String[] students;
    	int[] marks;
    	char[] grades; 
    }
    
    class Data {
    	int mark;
    	String login;
    	String coursecode;
    	StudentData studentrecord = new StudentData();
    	StudentData[] studentrecords;
    }

    It's not really outputting anything yet as I have only attempted the parsing bit so far. Thanks!

  4. #4
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by jamess View Post
    I have the following data:

    Java Code:
    Maths abc10 4 def08 6 ;  
    English hrd45 3 ngh05 10 ; .
    Java Code:
    Maths abc10 4   
    Maths def08 6   
    English hrd45 3  
    English ngh05 10
    Any Ideas? Thanks!

    Well I could not understand your requirement fully .... Just see if the below approach is of any help ..

    Java Code:
      Scanner sc = new Scanner(System.in);
            String token;
            ArrayList<String> list = new ArrayList<String>();
            StringBuilder  sb = new StringBuilder();
          
            while (!(token = sc.next()).equals(".")) {     
                if(!token.equals(";")) {
                          sb.append(token+",");
                }else{
                   list.add(sb.toString());
                    sb = new StringBuilder();
                }
            }
    
            for(String t : list){
                System.out.println(t);
                String[] det = t.split(",");
                int i=3;
                while(i<=det.length){
                    System.out.print(det[0]+" ");
                    System.out.print(det[i-2]+" ");
                    System.out.println(det[i-1]);
                    i=i+2;
                }
            }
    ofcourse the code is not optimized ...... :p
    Last edited by Vinod Mukundan; 12-06-2010 at 10:03 AM.
    _______________________________________________
    give me beans .........

Similar Threads

  1. Problem with string parsing.
    By dc0m in forum New To Java
    Replies: 5
    Last Post: 08-31-2010, 06:25 PM
  2. problems with parsing a string into int,
    By bigj in forum New To Java
    Replies: 3
    Last Post: 01-06-2010, 10:32 PM
  3. Parsing string and simple calculation
    By sapina007 in forum Advanced Java
    Replies: 4
    Last Post: 08-21-2009, 12:07 PM
  4. Multi Delimeter String Parsing
    By yuriyl in forum Advanced Java
    Replies: 5
    Last Post: 07-13-2009, 03:34 PM
  5. parsing numbers in a string
    By rsoler in forum Advanced Java
    Replies: 4
    Last Post: 03-31-2009, 06:05 AM

Tags for this Thread

Posting Permissions

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