Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,417 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,354 people online right now. Registration is fast and FREE... Join Now!




split() method

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

split() method

genuinek
14 Oct, 2008 - 09:00 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 23

Lets say I am using the split method to split a string into an array. Is their a way to split the string after the third comma in the string. And if so, could I parse that entire array into a double.

here's the example file I am working with:

LastName, FirstName, Exam1, Asg1, Asg2, Exam2, Asg3, Asg4
Christopher, Thomas, 78, 79, 82, 88, 78, 91
Cole, John, 100, 100, 100, 100, 100, 100
Karr, Arlen, 91, 86, 94, 100, 98, 93
McClurg, Andrew, 91, 87, 99, 87,,93
Noble, Rich, 84, 79, 85, 88, 90, 91
Rao, Sista, 91, 86, 94, 100, 98, 93
Stotz, Ralph, 81,83,,93, 78
Yi, Yu, 99, 88, 101, 76, 90, 94


User is offlineProfile CardPM
+Quote Post

pbl
RE: Split() Method
14 Oct, 2008 - 09:04 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Is Stotz, Ralph an error or you might have two ,, that follow each other without number between them ?

Can we see your code ?
User is offlineProfile CardPM
+Quote Post

genuinek
RE: Split() Method
14 Oct, 2008 - 09:15 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 23

Here is what im working with its gonna end up being a GUI grading system.

CODE
import java.io.*;
import java.util.*;
import javax.swing.*;

public class BaggettAsg3 extends JFrame
{
    public double Midterm;
    public double Final;
    public double assignment1, assignment2, assignment3, assignment4;

    public static double setRecords(String filename) throws IOException
    {
        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

    double[] Mid = new double[7];
    double[] Final = new double[7];
    double[] Asg1 = new double[7];
    double[] Asg2 = new double[7];
    double[] Asg3 = new double[7];
    double[] Asg4 = new double[7];
              
            inputFile.nextLine();
            String Student1 = inputFile.nextLine();
            String[] S1 = Student1.split("\\,");
            String Student2 = inputFile.nextLine();
            String[] S2 = Student2.split("\\,");
            String Student3 = inputFile.nextLine();
            String[] S3 = Student3.split("\\,");        
            String Student4 = inputFile.nextLine();
            String[] S4 = Student4.split("\\,");        
            String Student5 = inputFile.nextLine();
            String[] S5 = Student5.split("\\,");
            String Student6 = inputFile.nextLine();
            String[] S6 = Student6.split("\\,");
            String Student7 = inputFile.nextLine();
            String[] S7 = Student7.split("\\,");    
            String Student8 = inputFile.nextLine();
            String[] S8 = Student8.split("\\,");
                  
            inputFile.close();
                      
            
    }
    
public static void main(String [] args)
{
     String filename;
     filename = "Asg3Data.txt";
}
          
    
}


Right now I am splitting it after each comma. However, I want to split it after the second* comma, so I can parse the grades in the file to doubles.
User is offlineProfile CardPM
+Quote Post

genuinek
RE: Split() Method
14 Oct, 2008 - 09:31 PM
Post #4

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 23

any help would be appreciated.
User is offlineProfile CardPM
+Quote Post

g00se
RE: Split() Method
15 Oct, 2008 - 12:44 AM
Post #5

D.I.C Regular
Group Icon

Joined: 19 Sep, 2008
Posts: 455



Thanked: 38 times
My Contributions
This is how i'd do it. The methods I hint at here you would obviously have to write yourself:

CODE

        String[] nameInfo = marks.split("(?<=\\D),");
        Student s = new Student();
        s.setFirstname(nameInfo[1].trim());
        s.setLastname(nameInfo[0].trim());
        s.setMarks(getArrayAsNumbers(nameInfo[2].trim()));

User is offlineProfile CardPM
+Quote Post

genuinek
RE: Split() Method
15 Oct, 2008 - 04:01 PM
Post #6

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 23

thanks for the help but waht exactly does (?<=\\D) mean.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Split() Method
15 Oct, 2008 - 05:30 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(genuinek @ 15 Oct, 2008 - 05:01 PM) *

thanks for the help but waht exactly does (?<=\\D) mean.

You were splitting on "\" and ","
He splits on "?", "<", "=", "\" and "D"

I would rather use a Scanner for that type of thing
User is offlineProfile CardPM
+Quote Post

genuinek
RE: Split() Method
15 Oct, 2008 - 06:23 PM
Post #8

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 23

but why all those im only wanting to split after each comma. Im just stumped on how to parse the numbers into doubles.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Split() Method
15 Oct, 2008 - 06:30 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
So just

String[] S1 = Student1.split(",");

User is offlineProfile CardPM
+Quote Post

genuinek
RE: Split() Method
15 Oct, 2008 - 06:55 PM
Post #10

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 23

thx. now the numbers I want to parse would start at S1[2] etc.. right? How would I do that?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Split() Method
15 Oct, 2008 - 08:04 PM
Post #11

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(genuinek @ 15 Oct, 2008 - 07:55 PM) *

thx. now the numbers I want to parse would start at S1[2] etc.. right? How would I do that?

int number = Integer.parseInt(s[2]);
User is offlineProfile CardPM
+Quote Post

genuinek
RE: Split() Method
19 Oct, 2008 - 10:44 PM
Post #12

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 23

Well after making adjustments this is what I got. However, I want to know if I am going in the right direction and if not what I can do to fix it.
CODE
import java.io.*;
import java.util.*;
import javax.swing.*;

public class Asg3
{
  
    public static  class studentRecords extends JFrame
    {
        public double Assignment1, Assignment2, Assignment3, Assignment4;
        public double Midterm;
        public double Final;
        public double numericGrade;
        public char letterGrade;
        public int choice;
        public double[] Asg = new double[31];
        public double[] Mid = new double[7];
        public double[] Fin = new double[7];
        
        public void Record() throws IOException
        {
            File grades = new File("Asg3Data.txt");
            Scanner inputFile = new Scanner(grades);
            
            inputFile.nextLine();
            String Student1 = inputFile.nextLine();
            String[] S1 = Student1.split(",");
            String Student2 = inputFile.nextLine();
            String[] S2 = Student2.split(",");
            String Student3 = inputFile.nextLine();
            String[] S3 = Student3.split(",");        
            String Student4 = inputFile.nextLine();
            String[] S4 = Student4.split(",");        
            String Student5 = inputFile.nextLine();
            String[] S5 = Student5.split(",");
            String Student6 = inputFile.nextLine();
            String[] S6 = Student6.split(",");
            String Student7 = inputFile.nextLine();
            String[] S7 = Student7.split(",");    
            String Student8 = inputFile.nextLine();
            String[] S8 = Student8.split(",");
            Asg[0] = Double.parseDouble(S1[3]);
            Asg[1] = Double.parseDouble(S1[4]);
            Asg[2] = Double.parseDouble(S1[6]);
            Asg[3] = Double.parseDouble(S1[7]);
            Asg[4] = Double.parseDouble(S2[3]);
            Asg[5] = Double.parseDouble(S2[4]);
            Asg[6] = Double.parseDouble(S2[6]);
            Asg[7] = Double.parseDouble(S2[7]);
            Asg[8] = Double.parseDouble(S3[3]);
            Asg[9] = Double.parseDouble(S3[4]);
            Asg[10] = Double.parseDouble(S3[6]);
            Asg[11] = Double.parseDouble(S3[7]);
            Asg[12] = Double.parseDouble(S4[3]);
            Asg[13] = Double.parseDouble(S4[4]);
            Asg[14] = Double.parseDouble(S4[6]);
            Asg[15] = Double.parseDouble(S4[7]);
            Asg[16] = Double.parseDouble(S5[3]);
            Asg[17] = Double.parseDouble(S5[4]);
            Asg[18] = Double.parseDouble(S5[6]);
            Asg[19] = Double.parseDouble(S5[7]);
            Asg[20] = Double.parseDouble(S6[3]);
            Asg[21] = Double.parseDouble(S6[4]);
            Asg[22] = Double.parseDouble(S6[6]);
            Asg[23] = Double.parseDouble(S6[7]);
            Asg[24] = Double.parseDouble(S7[3]);
            Asg[25] = Double.parseDouble(S7[4]);
            Asg[26] = Double.parseDouble(S7[6]);
            Asg[27] = Double.parseDouble(S7[7]);
            Asg[28] = Double.parseDouble(S8[3]);
            Asg[29] = Double.parseDouble(S8[4]);
            Asg[30] = Double.parseDouble(S8[6]);
            Asg[31] = Double.parseDouble(S8[7]);
            Mid[0] = Double.parseDouble(S1[2]);
            Mid[1] = Double.parseDouble(S2[2]);
            Mid[2] = Double.parseDouble(S3[2]);
            Mid[3] = Double.parseDouble(S4[2]);
            Mid[4] = Double.parseDouble(S5[2]);
            Mid[5] = Double.parseDouble(S6[2]);
            Mid[6] = Double.parseDouble(S7[2]);
            Mid[7] = Double.parseDouble(S8[2]);
            Fin[0] = Double.parseDouble(S1[5]);
            Fin[1] = Double.parseDouble(S2[5]);
            Fin[2] = Double.parseDouble(S3[5]);
            Fin[3] = Double.parseDouble(S4[5]);
            Fin[4] = Double.parseDouble(S5[5]);
            Fin[5] = Double.parseDouble(S6[5]);
            Fin[6] = Double.parseDouble(S7[5]);
            Fin[7] = Double.parseDouble(S8[5]);
              
            
        }
        
        public void  setMidterm()
        {
         if (choice == 1)
         {
             Midterm = Mid[0] * .25;
            
         }
         if (choice == 2)
         {
             Midterm = Mid[1] * .25;
            
         }
         if (choice == 3)
         {
             Midterm = Mid[2] * .25;
            
         }
         if (choice == 4)
         {
             Midterm = Mid[3] * .25;
            
         }
         if (choice == 5)
         {
             Midterm = Mid[4] * .25;
            
         }
         if (choice == 6)
         {
             Midterm = Mid[5] * .25;
            
         }
         if (choice == 7)
         {
             Midterm = Mid[6] * .25;
            
         }
         if (choice == 8)
         {
             Midterm = Mid[7] * .25;
            
         }
            
        }
        
        public void setFinal()
        {
         if (choice == 1)
         {
             Final = Fin[0] * .50;
            
         }
         if (choice == 2)
         {
             Final = Fin[1] * .50;
            
         }
         if (choice == 3)
         {
             Final = Fin[2] * .50;
            
         }
         if (choice == 4)
         {
             Final = Fin[3] * .50;
            
         }
         if (choice == 5)
         {
             Final = Fin[4] * .50;
            
         }
         if (choice == 6)
         {
             Final = Fin[5] * .50;
            
         }
         if (choice == 7)
         {
             Final = Fin[6] * .50;
            
         }
         if (choice == 8)
         {
             Final = Fin[7] * .50;
            
         }
            
        }
        
        public void setAssignments()
        {
         if (choice == 1)
         {
             Assignment1 = Asg[0] * .25;
             Assignment2 = Asg[1] * .25;
             Assignment3 = Asg[2] * .25;
             Assignment4 = Asg[3] * .25;
         }
         if (choice == 2)
         {
             Assignment1 = Asg[4] * .25;
             Assignment2 = Asg[5] * .25;
             Assignment3 = Asg[6] * .25;
             Assignment4 = Asg[7] * .25;
         }
         if (choice == 3)
         {
             Assignment1 = Asg[8] * .25;
             Assignment2 = Asg[9] * .25;
             Assignment3 = Asg[10] * .25;
             Assignment4 = Asg[11] * .25;
         }
         if (choice == 4)
         {
             Assignment1 = Asg[12] * .25;
             Assignment2 = Asg[13] * .25;
             Assignment3 = Asg[14] * .25;
             Assignment4 = Asg[15] * .25;
         }
         if (choice == 5)
         {
             Assignment1 = Asg[16] * .25;
             Assignment2 = Asg[17] * .25;
             Assignment3 = Asg[18] * .25;
             Assignment4 = Asg[19] * .25;
         }
         if (choice == 6)
         {
             Assignment1 = Asg[20] * .25;
             Assignment2 = Asg[21] * .25;
             Assignment3 = Asg[22] * .25;
             Assignment4 = Asg[23] * .25;
         }
         if (choice == 7)
         {
             Assignment1 = Asg[24] * .25;
             Assignment2 = Asg[25] * .25;
             Assignment3 = Asg[26] * .25;
             Assignment4 = Asg[27] * .25;
         }
         if (choice == 8)
         {
             Assignment1 = Asg[28] * .25;
             Assignment2 = Asg[29] * .25;
             Assignment3 = Asg[30] * .25;
             Assignment4 = Asg[31] * .25;
         }
        
        }
        
        public double getMidterm()
        {
            return Midterm;
        }
        public double getFinal()
        {
            return Final;
        }
        public double getAssignments1()
        {
            return Assignment1;            
        }
        public double getAssignments2()
        {
            return Assignment2;
        }
        public double getAssignments3()
        {
            return Assignment3;
        }
        public double getAssignments4()
        {
            return Assignment4;
        }
        
        void getAverage()
        {
            numericGrade = Midterm + Final + ((Assignment1 + Assignment2
                    + Assignment3 +Assignment4)/4);
            
        }
        
        void getLetterGrade()
        {
            if (numericGrade >= 90)
                letterGrade = 'A';
            
            if (numericGrade >= 80 && numericGrade < 90)
                letterGrade = 'B';
            
            if (numericGrade >= 70 && numericGrade < 80)
                letterGrade = 'C';
            
            if (numericGrade >= 60 && numericGrade < 70)
                letterGrade = 'D';
            
            if (numericGrade < 60)
                letterGrade = 'F';
        }
    }
    
public static void main(String [] args)
{
    
    
}
          
    
}


This post has been edited by genuinek: 19 Oct, 2008 - 10:45 PM
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:59PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets