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

Thread: Need help to format input.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help to format input.

    When i input a number in my append i need to have 3 - in it. for example when i have this number 12345678 I need it to write to file 123-45-678. I would think i need to use the format thing but need a nudge in the right direction.

     ja.append(add0+"+");


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help to format input.

    Append of what? A string? What is ja? I don't understand the rules you describe. I recommend breaking what you need into these rules, its easier for us and a computer to understand.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help to format input.

    String ja = textArea;
    add0 = input number variable;

    I need the input number to be 12345678; (input from user 8 numbers)
    but when its output (write to .dat) it gets written as 123-45-678:
    How do i accoplish this in code.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help to format input.

    Quote Originally Posted by miller4103 View Post
    String ja = textArea;
    add0 = input number variable;

    I need the input number to be 12345678; (input from user 8 numbers)
    but when its output (write to .dat) it gets written as 123-45-678:
    How do i accoplish this in code.
    What did you try? Presuming you want a dash after the third and fifth number - did you try a loop? Have you tried breaking the input string into substrings?

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help to format input.

    I have never heard about substrings. What are they. Also i will try a loop right now. I dont know why i didnt think of a loop although i dont know how to get it to do that.
    would i use an if-for or while loop? and how do i get the loop to count the numbers?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help to format input.

    I have never heard about substrings. What are they.
    The API is worth studying for all classes that you use. See String (Java Platform SE 6) and the substring method.

    Also i will try a loop right now. I dont know why i didnt think of a loop although i dont know how to get it to do that.
    would i use an if-for or while loop? and how do i get the loop to count the numbers?
    Write it out slowly on paper - loop through the string one character at a time and consider at which positions you need to insert

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help to format input.

    Thank you very much for the nudge.
     ja.append(add0.substring(0,3)+"-"+add0.substring(3,5)+"-"+add0.substring(5)+"\n");

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help to format input.

    You're welcome

Similar Threads

  1. Validating user input if it is in the correct format.
    By mamaass in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2012, 03:42 PM
  2. How to use format outpout
    By mael331 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2011, 03:14 PM
  3. Format Problems
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 27th, 2010, 09:48 PM
  4. How to format?
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 01:07 PM
  5. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM