Results 1 to 19 of 19
  1. #1
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default questions about split and String

    Java Code:
    public class test{
    	public static void main(String args[]){
    	String splitString = "Bob:Stev:David:John";
    	String[] names = splitString.split(":");
    	for(String name:names){
    		String[] msg=names;
    		for (int i=0;i<splitString.length();i++)
    	   System.out.println(msg[i]);
    	}
    	}
    }
    I want to save the whole characters like Bob and Stev and David and John by array.
    am i getting the wrong way in this case?
    I wonder if i use the subString is the best idea for saving whole name?
    Could you please tell me what the way can save the whole name each by each.

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    	at test.main(test.java:8)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    You have a lot of redundant code. What exactly are you trying to do? Why do you loop through names, then store that entire array in another variable, then print out the array inside an inner loop? And why does the inner loop iterate splitString.length() times?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    You have a lot of redundant code. What exactly are you trying to do? Why do you loop through names, then store that entire array in another variable, then print out the array inside an inner loop? And why does the inner loop iterate splitString.length() times?
    sorry,the above example is from where i have copied for learning
    but i fixed it already,,but there is still a little problem, "for (name:names)"is it just like for loop?

    and i wanna know what i am wrong below.

    Java Code:
    import java.util.StringTokenizer;
    public class Lab9Q2b {
    	public static void main(String[] args) {
    		String s = "Go to the main menu. Quick!";
    		StringBuffer buf = new StringBuffer(s);
    		buf = buf.reverse();
    		s=buf.toString();
    	    StringTokenizer st = new StringTokenizer(s," ");
    		String array[] = new String[st.countTokens()];
    	     while (st.hasMoreTokens()) {
    	    	 for(int a=0;a<=array.length-1;a++){
    	    	 array[a] = st.nextToken();
    	    	 }
    	     }
    		for (int i=buf.length()-1;i>=0;i--)
    		System.out.println(array[i]+" ");
    	}
    
    }
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26
    at Lab9Q2b.main(Lab9Q2b.java:16)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,845
    Rep Power
    19

    Default

    Which line is line 16?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    I have asked you questions that, if you answer them, you'll figure out your problem. Saying "i copied it" doesn't really help anybody out. You have to understand what your code is doing, and why it's happening.

    Read also: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Greetings!

    As mentioned above, it's unclear what you're actually trying to achieve. If you're wanting to parse through a string and print out each word individually, there are better ways of doing this.

    However, to answer your question, the problem is with your last for loop. If you want to print the contents of "array", you should iterate over that variable. You are using buf as your loop count max, which is unrelated.

    Java Code:
    for(String element : array){
        System.out.println(element);
    }

  7. #7
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Or, I suppose more specific to your example...

    Java Code:
    for (int i=array.length()-1;i>=0;i--)
    		System.out.println(array[i]+" ");

  8. #8
    edocavaj is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    maybe it is also good to say that when doing:
    Java Code:
    String[] myNames = "Bob:Stev:David:John".split(":");
    The string array contains all the names like so:
    myNames[0] => "Bob";
    myNames[1] => "Stev";
    myNames[2] => "David";
    myNames[3] => "John";

  9. #9
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    thanks for all reply!

    i dont know it can straightly add a .split(" ") in String
    i thought the .split(" ") must be new a subject on first.

    thanks for teaching!


    Java Code:
    public class Sum {
       public static void main( String args[] ) {
    		String[] num = "5 6 7 8 9 10 11 12 13 14".split(" ");
    		int[] saveNumber = new int[num.length];
    		for (int i=0;i<num.length;i+=3){
    			for (int j=0;j<num.length;j++){
          saveNumber[j] = Integer.parseInt(num[i]);
    		}
    			}
    		for (int s=0;s<num.length;s++)
    		      System.out.println("The sum is " + saveNumber[s]);
    
    	}
    }

    I would like to save the integer of each when the string is changed to integer but why the output is below:
    what i am making a mistake?
    The sum is 14
    The sum is 14
    The sum is 14
    The sum is 14
    The sum is 14
    The sum is 14
    The sum is 14
    The sum is 14
    The sum is 14
    The sum is 14
    Last edited by bantony02; 04-07-2011 at 04:10 PM.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Once again you have redundant code. Get rid of the outer for loop.

  11. #11
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Once again you have redundant code. Get rid of the outer for loop.
    but i want to use this to save the array number
    saveNumber[0]=Integer.parseInt(array[0])
    saveNumber[1]=Integer.parseInt(array[3])
    saveNumber[2]=Integer.parseInt(array[6])
    saveNumber[3]=Integer.parseInt(array[9])

    if I get rid of the outer for loop,how can i get this?

    the output i want is 5,8,11,14 not just ascending the number
    Last edited by bantony02; 04-08-2011 at 07:41 AM.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Okay, but that's not what a nested loop does. If you want some order, use that order. A nested loop doesn't do that. It looks like you want to increment some variable by 3 each iteration of the loop?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Okay, but that's not what a nested loop does. If you want some order, use that order. A nested loop doesn't do that. It looks like you want to increment some variable by 3 each iteration of the loop?
    yes,I do want to increment some variable by 3 each iteration of the loop for the output, but there is only a repeated output. I have no idea

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Quote Originally Posted by bantony02 View Post
    yes,I do want to increment some variable by 3 each iteration of the loop for the output, but there is only a repeated output. I have no idea
    What's stopping you from using another variable? Or by using basic multiplication with your looping variable?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by bantony02 View Post
    yes,I do want to increment some variable by 3 each iteration of the loop for the output, but there is only a repeated output. I have no idea
    I'd highly recommend that you simply debug your code and step through it to see what it's doing. You would learn a lot by doing this, and I think that the solution to your problem would come to you at that point.

  16. #16
    bantony02 is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    class Line {
    private String name;
    private Station[] stops;
    private int[] distance;
    private int[] fare;
    private double passengerFarePerKm;
    private double cargoFarePerKm;
    private int crossBorderSurcharge;
    private int reservationSurcharge;
    public Line(String lineName,String stations,String fares) {
        this.name=lineName;
        String name="";
        String array[] =stations.split(":| ");
        for(int i=0;i<array.length;i+=3)
        name+=array[i]+" "+array[i+1];
        String names[]=name.split(" ");
        for (int a=0;a<=names.length-1;a++){
        stops[a]= new Station(names[a],names[a+1]);
        }
        for(int b=2;b<distance.length;b+=3){
        distance[b] = Integer.parseInt(array[b]);
        }
        array=fares.split(":");
        for(int c=0;c<fare.length;c++)
        fare[c]= Integer.parseInt(array[c]);
        this.passengerFarePerKm=fare[0];
        this.cargoFarePerKm=fare[1];
        this.crossBorderSurcharge=fare[2];
        this.reservationSurcharge=fare[3];
    }
    
        public String getName(){return name;}
        public Station[] getStops(){return stops;}
        public double getPassengerFarePerKm(){return passengerFarePerKm;}
        public double getCargoFarePerKm() {return cargoFarePerKm;}
        public int getCrossBorderSurcharge(){return crossBorderSurcharge;}
        public int getReservationSurcharge(){return reservationSurcharge;}
        public String toString(){return "Line: "+ getName() + "\n" + getStops() ;}
    }
    class Station {
        private String name;
        private String country;
        public Station(String name,String country) {
        this.name=name;
        this.country=country;
        }
        public String getName(){return name;}
        public String getCountry(){return country;}
        public String toString(){return "name: "+name+"country: "+country;}
    }
    public class TestLine {
    public static void main(String[] args) {
    Line line = new Line("Paris-Zurich-Munich", "Paris:France:0 Lyon:France:450 Geneve:Switzerland:600 Zurich:Switzerland:870 St.Gallen:Switzerland:950 Memmingen:Germany:1070 Munich:Germany:1180", "0.06:0.01:30:10");
        Station stations[] = line.getStops();
        System.out.println("All stations:");
        for(int i=0; i<stations.length; i++)
        System.out.print(stations[i] + "   ");
        System.out.println("\n");
        }
    }
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    **at Line.<init>(TestTrain.java:18)
    **at TestTrain.main(TestTrain.java:54)
    what am I wrong?
    I have tried many time. I dont know what the problem is here
    Last edited by bantony02; 04-09-2011 at 12:25 PM.

  17. #17
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    public class Test{
    	public static void main(String args[]){
    	String splitString = "Bob:Stev:David:John";
    	String[] names = splitString.split(":");
    
    	for(String name : names){		
                       System.out.println(name);
    	}
    	}
    }
    Is this what you wanted?
    Last edited by Maximus-EVG; 04-16-2011 at 03:42 AM. Reason: ...crap I should read whole topic 1st :/

  18. #18
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    Gotta love spoonfeeding...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #19
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. String split help
    By MiddleBlocker in forum New To Java
    Replies: 4
    Last Post: 03-11-2011, 02:57 AM
  2. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  3. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 02:23 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

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