Results 1 to 12 of 12
  1. #1
    murcy is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default help with string formatting

    ive just started learning java (ive had a little experience in python but im pretty much a complete beginner)

    I need help with the following exercise

    Write a program to inspect the string value of the variable Input and report the result to the variable Output. The Input is supposed to contain a single balanced pair of brackets i.e. the string is of the form "...(...)...". The variable Output should be assigned the contents of the bracketed part of a string or "bad string" if there is some problem with the brackets. For example
    abc(pqrs)yz gives pqrs, but
    abcpqrsyz
    abc(pqrsyz
    abcpqrs)yz
    abc)pqrs(yz
    abc(pq(rs)yz
    abs(pq)rs)yz
    abc(p(qr)s)yz
    abc(p)qr(s)yz all give the result: bad string


    I need the simplest solution (not the best) i.e Im not interested in classes etc. In theory it should be possible with just these:
    s.length,
    s.substr( start, length ),
    s.substring( from, to ),
    s.indexOf(subs),
    s.lastIndexOf(subs),
    also s.indexOf(subs, from),
    s.lastIndexOf(subs, from)
    s.charAt(),
    s.toUpperCase(), s.toLowerCase()

    these are the functions we are currently practising/have been given but I havent got a clue how to start this....

    thanks for help

  2. #2
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: help with string formatting

    Quote Originally Posted by murcy View Post
    I need the simplest solution (not the best)...
    I am pretty sure that is the motto at Microsoft!


    Quote Originally Posted by murcy View Post
    ...but I havent got a clue how to start this....
    I would probably suggest starting off with something along the lines of:
    Java Code:
    public class programName{
      public static void main(String[] args){
        //insert code here
      }
    }
    You are going to have to have some kind of code written to get help here. Either that or have a pretty precise question to ask. I don't know what you are asking other then "How do I do my homework?". And "In the simplest, easiest, and shittiest way possible." You are not exactly radiating the 'Help Me' vibe here man. My best advice to you is to ditch the 'I don't care' attitude and put in a little bit of effort, and you will probably see some results in the way of true help.
    Last edited by penguinCoder; 10-14-2012 at 02:53 AM.

  3. #3
    murcy is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: help with string formatting

    thank you for taking the time to respond... last time i checked one doesn't get homework at university. secondly i cant use the class system because we technically haven't been taught it yet.

    here some code ive thrown together

    var s1 = prompt("Enter string")
    var a = s1.indexOf("(")

    //alert (s1.indexOf("("))
    if (s1.indexOf("(") > -1){
    //alert("found (")
    var a = s1.indexOf("(")

    if (s1.indexOf(")")> a)
    {
    //alert(s1.indexOf(")"))
    //alert("found ( and )")
    var b = s1.indexOf(")")
    var s2 = s1.substring(a+1,b)
    alert(s2)
    }
    }

    else if (s1.indexOf(")") == -1)
    {
    alert("bad string")
    }
    // else
    //alert("error")

    else
    alert("bad string")


    this works to a degree, the problems start when one inputs

    abc(pq(rs)yz
    abs(pq)rs)yz
    abc(p(qr)s)yz
    abc(p)qr(s)yz

    because the program prints out what ever is between the first set of brackets instead of returning "bad string"

    the only way I can see of changing this is to loop through the strings and search to see if there is more than 1 ) or ( and if there is return "bad string"

    the problem is we havent been taught to loop yet - i only know its possible from my python back ground so I cant use it for this

    im pretty stuck, perhaps there is a simpler answer im over looking

  4. #4
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: help with string formatting

    Quote Originally Posted by murcy View Post
    ive just started learning java
    I believe you actually have just started learning JavaScript. Although it is roughly based on the same syntax, Java and JavaScript are not the same thing.

    Quote Originally Posted by murcy View Post
    last time i checked one doesn't get homework at university.
    Really? I attend ASU, and regularly get about 35 hours of homework EVERY week; though I am currently enrolled for 19 credits. You don't get any homework at all? What to you do, just listen to people lecture about things and never get any hands on experience? This cannot be correct, you have got to be yanking my chain, right?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: help with string formatting

    You can iterate over the string and each time you encounter an "(" increment a count. Each time you encounter an ")" decrement the count. At the end if your count is zero you have balanced brackets. However, it will not work for "ab)cd(ef" that yields a count of zero but the brackets are in the wrong order. To overcome this you can add an extra condition should the count ever fall below zero.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: help with string formatting

    In fact the OP wants a single balanced pair so the count should never get to two either.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: help with string formatting

    In that case 2 separate counts would be needed both which need to be 1 at the end. Using my above algorithm "a(b)c(d)e" would pass as the count would never fall below zero or go above 1. But then again "a)b(c" would pass.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: help with string formatting

    Thinking about it I would use 4 variables:

    A - position of the first open bracket.
    B - position of the first close bracket.
    C - position of the next open bracket after the first open bracket (should there be one).
    D - position of the next close bracket after the first close bracket (should there be one).

    The string is only valid if:
    A >= 0
    A < B
    C = -1
    D = -1

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: help with string formatting

    I actually prefer your first approach. In a string like ...)...(... the first ) would put the count to -1 and so fail.

    One "sweep" ftw!

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: help with string formatting

    My first approach works if you need to find balanced brackets. However, if the task is to find a single match pair of brackets "a(b)c(d)e" would pass as the count would end at zero. So there needs to be some other mechanism to make sure that there is only a single pair of brackets.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: help with string formatting

    Oh yes, you're right. Not to mention abcde which would have a count of zero.

    I still like a single pass, though. Perhaps (=3 and )=-2. Fail as soon as count<0 or count>3, and at the end check count==1.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: help with string formatting

    Jos will probably come along and post a math formula to solve this problem.

Similar Threads

  1. String formatting
    By sunde887 in forum New To Java
    Replies: 5
    Last Post: 01-23-2011, 11:24 AM
  2. Formatting String data from database
    By computerbum in forum JDBC
    Replies: 1
    Last Post: 04-28-2010, 01:27 PM
  3. String and number formatting
    By computerbum in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 05:06 PM
  4. Formatting String
    By ersachinjain in forum Advanced Java
    Replies: 13
    Last Post: 10-21-2009, 09:26 PM
  5. formatting String
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 07:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •