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: need numbers to output onto a new line

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Location
    Ireland
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need numbers to output onto a new line

    hay guy...I'm having trouble with some code using an applet. I'm outputting a messgae asking a user to ener 4 number. When the numbers are entered and the user selects ok. An inforamtion messgae of the numbers entered will appear on the screen...now i have that part done but what if i want the numbers to be entered into a new line. In c++ you just enter \n. This doesn't work in this case.
    Example:
    user entered 1234
    I want 1
    2
    3
    4
    to be outputed on my information box

    */import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    /**
    *
    * 
    */
    public class NewJApplet extends javax.swing.JApplet {
     
    String number1;
     
     
    /** Creates a new instance of NewJApplet */
    public NewJApplet() {
    number1=JOptionPane.showInputDialog(null,"Please Enter Four Digits.",
    JOptionPane.QUESTION_MESSAGE); 
     
    JOptionPane.showMessageDialog(null,"The numbers you typed are.\n"+ number1+"\n"); 
     
    }
     
    }


    Any help would be great!!


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: need numbers to output onto a new line

    hi . first please put your codes on highlight tags >> [highlight=java] code [/highlight]

    you cant print those individualy EACH line.. because your String object(Variable) is a WHOLE string.
    so when you enter, say, 1234 , the number1 does not contain individual strings.. its a one whole intact STRING
    , when you wrote this statement >
    "The Numbers you typed are. "\n" + number1 + "\n",
    its like your telling java ok print a NewLine("\n) and print the values of number1 and print another NewLine "\n"
    you need to get each one of it.. and assign it individually into another STRING object and adding a new line

    ill provide the logic of the loop for you .
    for (int x = 0; x <= number1.length() - 1; x++) {
     
            eachNumber = eachNumber + ("" + "\n" + number1.charAt(x));
     
        }

    where eachNumber increments everytime it gets the value on your STRING Object number1 and adds a "\n" which is a new line, now go ahead how are you goint to print all the value of eachNumber object.

    sorry for bad english .. im a bit tired
    Last edited by chronoz13; April 27th, 2011 at 07:35 AM.

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. How to add line numbers in Eclipse
    By Brt93yoda in forum Java JDK & IDE Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 11:30 PM
  3. java - soot issue...counting the jimple line numbers??
    By volatile in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2010, 12:49 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM