Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: String being 'cut off in for loop

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String being 'cut off in for loop

    Sorry for the crappy title but i honestly have no clue on how i would shortly word this problem.
    *Editing accidentally posted ;P

    So basically i have a string which is inputted which in this case is:
    69,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,

    Now the code which is processing this is:
    		String[] lineTest = itemList.split(",");
    		String line = "";
    		int j = 0;
    		for(int i = 0; i < lineTest.length; i++){
    			if(j < 9){
    				line+=lineTest[i];
    				j++;
    			}else{
    				line += "\n";
    				j = 0;
    			}
    		}

    The line which is inputted is 36 'inputs' long which excludes the commas since they are being split.
    However at the end of the process i am left with only 32, as shown here.
    Also if any of this code looks 'scrubby' or not efficient is because I'm fairly new to programming and thought i would try do this without any external references haha not a good idea when you're tired! xD

    69########
    #########
    #########
    ######

    It's probably a really simple fix that i just can't see due to sleep deprivation, Hahah thanks in advance

    --- Update ---

    Nevermind i fixed it by just adding a 'key?' to the string at the end of the splits which allowed me to get this result
    69,#,#,#,#,#,#,#,
    #,#,#,#,#,#,#,#,
    #,#,#,#,#,#,#,#,
    #,#,#,#,#,#,#,#,

    Surprisingly a lot cleaner than my previous code hahah

    		String[] split1 = itemList.split("<NWL>");
     
     
    		for(int i = 0; i < split1.length; i++){
    			System.out.println(split1[i]);
    		}


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: String being 'cut off in for loop

    It's good that you fixed it.
    For future reference, if you have an issue like this, the first thing you should do to debug is to put in a logical debug print statement. Since your concern is the size before and after the split, I would have put the following line immediately after the String split:
    System.out.println("Before: "+itemList.length()+", After: "+split1.length);

    This would have told you if the String was being split correctly, which was your main concern.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String being 'cut off in for loop

    Thanks i'll keep that in mind!
    I figured out that the issue was i was iterating through the array before it was being sent and it was counting to 9 then adding the break but i forgot to add the ninth array value before the <NWL> so it was actually sending 8 when it should of been sending 9. Like i said silly error. :3

Similar Threads

  1. [SOLVED] For loop to create string.
    By clyxee in forum Loops & Control Statements
    Replies: 21
    Last Post: December 12th, 2013, 09:40 AM
  2. HttpURLConnection doesnt quite cut it
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 23rd, 2012, 08:48 AM
  3. Re: Problem with BufferedReader : readLine() cut the line
    By caveden in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2012, 08:46 AM
  4. Problem with BufferedReader : readLine() cut the line
    By mfgagne73 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 3rd, 2012, 10:18 AM
  5. Can't get lines to cut at 60 characters and continue on next line right
    By JavaN00b in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 22nd, 2011, 09:44 AM