Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 26th, 2007, 10:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default Question on do while loop

I feel like I'm beating my head against a wall.

In the code below I am trying to create a new "noteLine" if an element in a string array has a length of 45 or greater. However, in addition to this I am needing to create a new "noteLine" for each chunk of 45 chars.

Any help or direction would be greatly appreciated.

Here is my code:

public class SplitTestCode {
    public static void main (String args[]) {

        String note = "Case is significant in identifiers in Java, so if and If are considered to be quite different. \n"
            + "The language has a small set of reserved words such as if, while, etc. \n"
            + "They are all sequences of lower-case letters. \n"
            + "The Java language places no restrictions on what names you use for functions, variables, classes, etc. \n"
            + "However, there is a standard naming convention, which all the standard Java libraries follow, and which you must follow in this class.";

        String[] notes = note.split("\n");

        //System.out.println("Notes length is " + notes.length);

        String noteLine = null;

        for (int i = 0; i < notes.length; i++) {
            noteLine = notes[i];
            //System.out.println("Here is my note: " + noteLine + "\n" + "Length of this note: " + notes[i].length());

            do {
                            if (notes[i].length() < 45){
                                noteLine = notes[i].substring(0, notes[i].length()-1);
                                System.out.println("Here is my note: " + noteLine);
                            }
                            else{
                                noteLine = notes[i].substring(0, 45);
                                if (notes[i].length() > 45) {
                                    noteLine = notes[i].substring(45, notes[i].length()-1);
                                }else{
                                    noteLine = null;
                                }
                                System.out.println("Here is my note: " + noteLine);
                            }
            }while (notes[i] != null);
        }
    }
}
 
Old April 28th, 2007, 02:56 AM
Authorized User
 
Join Date: Oct 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if I understand you right, this will help you. Maybe the code is not the most effective one, but it works :)

import java.util.ArrayList;

public class SplitTestCode {
    public static void main (String args[]) {
      String noteElem;
      ArrayList newNotes = new ArrayList();
      String note = "Case is significant in identifiers in Java, so if and If are considered to be quite different. \n"
          +
          "The language has a small set of reserved words such as if, while, etc. \n"
          + "They are all sequences of lower-case letters. \n"
          + "The Java language places no restrictions on what names you use for functions, variables, classes, etc. \n"
          + "However, there is a standard naming convention, which all the standard Java libraries follow, and which you must follow in this class.";
      ArrayList notes = new ArrayList();
      String[] noteToAdd = note.split("\n");
      for (int i = 0; i < noteToAdd.length; i++) {
        notes.add(noteToAdd[i]);

      }

       String noteLine = null;

      for (int i = 0; i < notes.size(); i++) {
        noteElem = (String) notes.get(i);

        if (noteElem.length() == 45) {
            newNotes.add(noteElem);
          }
        else
        if (noteElem.length() > 45) {

          noteLine = (String) notes.get(i);
          newNotes.add(noteElem);
          notes.add(noteLine.substring(0, 45));
          notes.add(noteLine.substring(45, noteLine.length()));

        }

      }
      for (int i = 0; i < newNotes.size(); i++) {
      System.out.println(newNotes.get(i));
      System.out.println(((String) newNotes.get(i)).length());

      }
    }
}
 
Old May 3rd, 2007, 05:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks so much for the help. I'll give it a try.

Regards.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop question??? RinoDM Javascript 1 May 13th, 2008 07:35 PM
Sentinel Loop Question JMOdom C# 3 April 12th, 2007 01:19 PM
Collection-based for loop syntax question. drosenbaum BOOK: Beginning Java 2, JDK 5 Edition 0 March 5th, 2007 07:19 PM
Do While Not Loop Question hcweb Classic ASP Basics 3 January 12th, 2007 01:27 PM
simple loop question Adam H-W Classic ASP Basics 2 July 9th, 2004 01:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.