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 2 of 2

Thread: Simple code apparently too hard for me...

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple code apparently too hard for me...

    Trying to write some code to allow a user type in some text and then type in how many characters per line they would like to appear in the terminal and completely stumped, not even sure if I'm going in the right direction with this.
    Here's what I got so far:

    import javax.swing.JOptionPane ;
     
    public class SendAMessage
    {
        public static void main(String[] args)
        {
            String message, numberInput ;
            int textMaxLen, i, c ;
     
            c = 0 ;
     
            message = JOptionPane.showInputDialog(null,"Message : ") ;
            numberInput  = JOptionPane.showInputDialog(null,"Characters per Message : ") ;
            textMaxLen = Integer.parseInt(numberInput) ;
            i = Integer.parseInt(numberInput) ;
     
            if( message.length() / textMaxLen <= 1) {
                System.out.println(message) ;
     
            }else{  
                for( c = 0 ; c != message.length() ; ) { 
                    if(i < textMaxLen) {
                        System.out.println(i) ;
                    }else{
                    System.out.println(message.substring(c, c+textMaxLen)) ;
                    c = c+textMaxLen ;
                    i = c ;
     
                    }
                    if(i < textMaxLen) {
                        System.out.println(i) ;
     
                 }
            } 
            }
        }
    }

    Any help would be greatly appreciated

    The main problem is that in order for this to work the number of characters per line needs to divide into the number or characters from the user input exactly, if there is 3 characters left over from the user input and the user specified he wanted 4 characters per line it will not print the last line and give an error message
    Last edited by Avopeb; October 29th, 2011 at 02:46 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Simple code apparently too hard for me...

    First thing you need to do is highlight your code

    [highlight=Java]*Put your java code here*[/highlight]


    To get this straight, you want it to run something like:

    Message: I like pie
    Characters Per Message: 1
    I
     
    l
    i
    k
    e
     
    p
    i
    e
    correct?

    If so, you should do a nested for loop
    Code
    for(int counter = 0; counter < charsInMessage.length; counter++)
    {
      for(int i = 0; i < numberOfCharsInEachLine; i ++)
      {
         //Print out your chars (1 at a time)
      }
      //Print out a blank space
    }
    or some other kind of nested loop.

    Loops: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. Having a hard time figuring out how to make my code work
    By iainnitro in forum Loops & Control Statements
    Replies: 2
    Last Post: September 6th, 2011, 07:48 AM
  2. new to java.... having a hard time trying to read code
    By Newbie_96 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 6th, 2011, 01:51 AM
  3. Need help with simple code
    By suxen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2011, 08:10 PM
  4. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM
  5. im dying here..on a simple code
    By Jason in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 28th, 2010, 10:33 PM

Tags for this Thread