Hello
my question how can I split array of string to another string using for loop
for example if i have this array of string joe,DE2300,A sam,RM4901,B allay,SM9800,D << name, course, grade
I want to split the array to another array
so the output will be
//I want this list to be in array called gradeBook
Name: joe course:DE2300 grade:A
Name: sam course:RM4901 grade:B
Name: allay course:SM9800 grade:D

Recommended Answers

All 12 Replies

have this array of string joe,DE2300,A sam,RM4901,B allay,SM9800,

That doesn't look like an array. It looks like a String.
To split that String up into separate Strings in an array look at the String class's split() method

want to split the array to another array

What type of array? What will go into each element in the new array.
I suspect you want to define a new class that holds data for a student. Then use the Strings that were split out of the input String to create an instance of the new class and then add that instance of the class to the new array.

I don't know how to create new array for the strings and then split it
can you show me the code of creating new array and split it

The String class's split() method creates the array by splitting then String.
Do a search here on the forum for sample code,
or ask google: java string split

Also read the API doc for the String class:
http://docs.oracle.com/javase/7/docs/api/

can you check my code if it correct
using java Jframe to greate GUI I created 2 class
first class has

    private String name;
    private String grade;
    private int course;

    public gradeBook (String name, String grade,String course) {
        this.name = name;
        this.grade = grade;
        this.course = course;

    }
    public String format() {
        String res = "";
        res = "name=" + getName() + ",";
        res = res + "course=" + getCourse() + "," "/n";
        res = res + "grade=" + getGrade() + ",";
 }
     public int getName() {
        return name;

    }

    public String getGrade() {

        return grade;
    }

    public String getCourse() {
        return course;
    }







my second class 


  private void btnCreategradeBookActionPerformed(java.awt.event.ActionEvent evt) { 
              String str = "QR56,sat,1530,BKK,,";
             String [] temp = str.split(",");
            txtOutput.append(temp.format); //I got red line under format why 
  }

here is the code

    private String name;
    private String grade;
    private int course;

    public gradeBook (String name, String grade,String course) {
        this.name = name;
        this.grade = grade;
        this.course = course;

    }
    public String format() {
        String res = "";
        res = "name=" + getName() + ",";
        res = res + "course=" + getCourse() + "," "/n";
        res = res + "grade=" + getGrade() + ",";
 }
     public int getName() {
        return name;

    }

    public String getGrade() {

        return grade;
    }

    public String getCourse() {
        return course;
    }







my second class 


  private void btnCreategradeBookActionPerformed(java.awt.event.ActionEvent evt) { 
              String str = "joe,DE2300,A,BKK";
             String [] temp = str.split(",");
            txtOutput.append(temp.format); //I got red line under format why 
  }

What happens when you compile it and execute it?
Are there any errors? If so, copy and paste them here if you have questions.
Does the program generate the correct results? If not, show the output and explain what the problem is.

I don't know why I got red line under .format

private void btnCreategradeBookActionPerformed(java.awt.event.ActionEvent evt) { 
              String str = "joe,DE2300,A,BKK";
             String [] temp = str.split(",");
            txtOutput.append(temp.format); //I got red line under format why

when I run the program theis error shows

    run:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
        at assignment2.Assignment2Form.btnCreateFlightsActionPerformed(Assignment2Form.java:133)
        at assignment2.Assignment2Form.access$100(Assignment2Form.java:6)
        at assignment2.Assignment2Form$2.actionPerformed(Assignment2Form.java:58)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6375)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6140)
        at java.awt.Container.processEvent(Container.java:2083)
        at java.awt.Component.dispatchEventImpl(Component.java:4737)
        at java.awt.Container.dispatchEventImpl(Container.java:2141)
        at java.awt.Component.dispatchEvent(Component.java:4565)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
        at java.awt.Container.dispatchEventImpl(Container.java:2127)
        at java.awt.Window.dispatchEventImpl(Window.java:2482)
        at java.awt.Component.dispatchEvent(Component.java:4565)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
        at java.awt.EventQueue.access$000(EventQueue.java:85)
        at java.awt.EventQueue$1.run(EventQueue.java:643)
        at java.awt.EventQueue$1.run(EventQueue.java:641)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
        at java.awt.EventQueue$2.run(EventQueue.java:657)
        at java.awt.EventQueue$2.run(EventQueue.java:655)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

txtOutput.append(temp.format); //I got red line under format why

The compiler can not find the field for the variable: temp
temp is a array of Strings. arrays have few fields. length is the ony one I know of.

What is that code supposed to do?

Also you have not declared txtOuput, and also format method is not described in the same class.

the code should change this string >> String str = "joe,DE2300,A,BKK";
. by split the string and call format to format the string to this
Name: joe course:DE2300 grade:A
Name: sam course:RM4901 grade:B
Name: allay course:SM9800 grade:D

To see what is in the array created by the split() method use the Arrays class's toString() method:
System.out.println("theArryName="+ java.util.Arrays.toString(theArrayName));
replace theArrayName with the name of the array in your code.

Once you have the data in the array, you can extract it into the Strings you want to see.

thank you for ur help ^^ I have never used toString before . I found another way ,but I will try to use toString .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.