Results 1 to 4 of 4
Thread: String Parsing Problem
- 12-05-2010, 10:39 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
String Parsing Problem
I have the following data:
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 '.'Java Code:Maths abc10 4 def08 6 ; English hrd45 3 ngh05 10 ; .
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 -
Any Ideas? Thanks!Java Code:Maths abc10 4 Maths def08 6 English hrd45 3 English ngh05 10
-
Let's see your code attempt and what it's outputting.
... and welcome to the Java-forums.org.
- 12-06-2010, 12:43 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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!
- 12-06-2010, 09:59 AM #4
Well I could not understand your requirement fully .... Just see if the below approach is of any help ..
ofcourse the code is not optimized ...... :pJava 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; } }Last edited by Vinod Mukundan; 12-06-2010 at 10:03 AM.
_______________________________________________
give me beans .........
Similar Threads
-
Problem with string parsing.
By dc0m in forum New To JavaReplies: 5Last Post: 08-31-2010, 06:25 PM -
problems with parsing a string into int,
By bigj in forum New To JavaReplies: 3Last Post: 01-06-2010, 10:32 PM -
Parsing string and simple calculation
By sapina007 in forum Advanced JavaReplies: 4Last Post: 08-21-2009, 12:07 PM -
Multi Delimeter String Parsing
By yuriyl in forum Advanced JavaReplies: 5Last Post: 07-13-2009, 03:34 PM -
parsing numbers in a string
By rsoler in forum Advanced JavaReplies: 4Last Post: 03-31-2009, 06:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks