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

Thread: JAVA alternative to goto

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    Manchest UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA alternative to goto

    Hi guys I am have some difficulty jumping back to a line in the code given the there goto is not supported my set up is java 7.2 I have graduated from C programming to java hence the problem !
    The object is the remove the wrapping from the authors i a string containing an unknown number of authors , in this example i just repeat the same author but in real life they will be different, so what i need to do is pull out each and process it so i end up with the authors without the wrappings
    [{Dr}{Seuss}][{Dr}{Seuss}][{Dr}{Seuss}] initial string
    Dr Seuss, Dr Seuss, Dr Seuss required string with separating comas and non at the last author

    What is have done so far is to check the length of the string so i can run a loop till all chars have been processed
    Then i detected in that loop the occurrence of “ ] [ “ thus counting the number of authors
    Next I find the end of the string }] and the start of the last element in the string [{ so now i am in a position to create a sub string and put the results in an array next i reduce the author count by one and if non zero i need to get back to the top to start over and if the new reduced string was the original input
    [{Dr}{Seuss}][{Dr}{Seuss}] this being the sub string and then the loop needs to run again to give [{Dr}{Seuss}] and finally author count equals zero then i will drop out of the loop and process what has been put into the array to complete the desired output Dr Seuss, Dr Seuss, Dr Seuss
    I call the class from main like this
    public class PullOutAuthor {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
          unwrape unw = new unwrape("[{Dr}{Seuss}][{Dr}{Seuss}][{Dr}{Seuss}]");
        }
    }

    package pulloutauthor;
     
    /**
     *
     * @author Pete
     */
    public class unwrape {
     
        public unwrape(String authorstring) {
            System.out.println(authorstring);
     
            String Newstring = "";
            String Remaining = "";
     
            String[] authorStore;
            authorStore = new String[10]; // place to stor 10 authors
     
            int NumberOfAuthors = 1;
     This is where i need to get back to so the string gets processed again in it’s new form 
           int positionOfLastChar = 0;
            int positionOffirsChar = 0;
     
            int numChar = authorstring.length();// get the length of the string
     
            for (int i = 0; i < numChar - 1; i++) {
                // this section finds out how many authors there are
                char Mychar = authorstring.charAt(i);
                char Mychar2 = authorstring.charAt(i + 1);
     
                if (Mychar == ']' && Mychar2 == '[') {
                    NumberOfAuthors++;
                }
            }
            for (int j = 0; j < numChar - 1; j++) {
                char Mychar = authorstring.charAt(j);
                char Mychar2 = authorstring.charAt(j + 1);
     
                if (Mychar == '}' && Mychar2 == ']') {
                    positionOfLastChar = j;
                }
                if (Mychar == '[' && Mychar2 == '{') {
                    positionOffirsChar = j;
                }
            }
     
            for (int k = positionOffirsChar + 2; k < positionOfLastChar; k++) {
                char Mychar3;
                Mychar3 = authorstring.charAt(k);
                Newstring = Newstring + Mychar3;
            }
            System.out.println(NumberOfAuthors);
            System.out.println(numChar);
            System.out.println(positionOffirsChar);
            System.out.println(positionOfLastChar);
            authorStore[1] = Newstring;
            NumberOfAuthors--;
     
            if (NumberOfAuthors != 0) {
                System.out.println("more to do");
                Remaining = authorstring.substring(0, 26);
                System.out.println(authorStore[1]);
                System.out.println(Remaining);
     
            }
        }
    }


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Manchest UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA alternative to the C and C++ statement goto

    Hi guys I am have some difficulty jumping back to a line in the code given the there goto is not supported my set up is java 7.2 I have graduated from C programming to java hence the problem !
    The object is the remove the wrapping from the authors i a string containing an unknown number of authors , in this example i just repeat the same author but in real life they will be different, so what i need to do is pull out each and process it so i end up with the authors without the wrappings
    [{Dr}{Seuss}][{Dr}{Seuss}][{Dr}{Seuss}] initial string
    Dr Seuss, Dr Seuss, Dr Seuss required string with separating comas and non at the last author

    What is have done so far is to check the length of the string so i can run a loop till all chars have been processed
    Then i detected in that loop the occurrence of “ ] [ “ thus counting the number of authors
    Next I find the end of the string }] and the start of the last element in the string [{ so now i am in a position to create a sub string and put the results in an array next i reduce the author count by one and if non zero i need to get back to the top to start over and if the new reduced string was the original input
    [{Dr}{Seuss}][{Dr}{Seuss}] this being the sub string and then the loop needs to run again to give [{Dr}{Seuss}] and finally author count equals zero then i will drop out of the loop and process what has been put into the array to complete the desired output Dr Seuss, Dr Seuss, Dr Seuss
    I call the class from main like this
    public class PullOutAuthor {
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
          unwrape unw = new unwrape("[{Dr}{Seuss}][{Dr}{Seuss}][{Dr}{Seuss}]");
        }
    }

    package pulloutauthor;
     
    /**
     *
     * @author Pete
     */
    public class unwrape {
     
        public unwrape(String authorstring) {
            System.out.println(authorstring);
     
            String Newstring = "";
            String Remaining = "";
     
            String[] authorStore;
            authorStore = new String[10]; // place to stor 10 authors
     
            int NumberOfAuthors = 1;
     This is where i need to get back to so the string gets processed again in it’s new form 
           int positionOfLastChar = 0;
            int positionOffirsChar = 0;
     
            int numChar = authorstring.length();// get the length of the string
     
            for (int i = 0; i < numChar - 1; i++) {
                // this section finds out how many authors there are
                char Mychar = authorstring.charAt(i);
                char Mychar2 = authorstring.charAt(i + 1);
     
                if (Mychar == ']' && Mychar2 == '[') {
                    NumberOfAuthors++;
                }
            }
            for (int j = 0; j < numChar - 1; j++) {
                char Mychar = authorstring.charAt(j);
                char Mychar2 = authorstring.charAt(j + 1);
     
                if (Mychar == '}' && Mychar2 == ']') {
                    positionOfLastChar = j;
                }
                if (Mychar == '[' && Mychar2 == '{') {
                    positionOffirsChar = j;
                }
            }
     
            for (int k = positionOffirsChar + 2; k < positionOfLastChar; k++) {
                char Mychar3;
                Mychar3 = authorstring.charAt(k);
                Newstring = Newstring + Mychar3;
            }
            System.out.println(NumberOfAuthors);
            System.out.println(numChar);
            System.out.println(positionOffirsChar);
            System.out.println(positionOfLastChar);
            authorStore[1] = Newstring;
            NumberOfAuthors--;
     
            if (NumberOfAuthors != 0) {
                System.out.println("more to do");
                Remaining = authorstring.substring(0, 26);
                System.out.println(authorStore[1]);
                System.out.println(Remaining);
     
            }
        }
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAVA alternative to goto

    jumping back to a line
    Use a loop.

    BTW The code doesn't follow java naming conventions: class names start with uppercase, variables with lowercase letters.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: JAVA alternative to goto

    Have you thought of a different (simpler) approach to removing and replacing the unwanted characters?

    Java has a few things that can help, such as text.replace("][", "*");
    This will replace all occurrences of ][ with a * .

    text.replaceAll("[\\[\\]{}]","")
    will get rid of all the [, ], { and }.

    You can also split the text between the *'s into an array with String[] words= (text.split("\\*"));

    There are other ways to use the Java regex, so explore.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Location
    Manchest UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAVA alternative to goto

    Thank you shall do

Similar Threads

  1. If-goto Loop
    By AnilS23 in forum Loops & Control Statements
    Replies: 2
    Last Post: November 13th, 2012, 05:18 AM
  2. [SOLVED] Alternative to Enum
    By Doctor X in forum Java Theory & Questions
    Replies: 2
    Last Post: October 6th, 2012, 03:49 PM
  3. Need an alternative to java.awt.Robot
    By Contrary in forum Java Theory & Questions
    Replies: 11
    Last Post: March 24th, 2012, 09:19 AM
  4. alternative to JMF
    By olimpicco in forum Java Theory & Questions
    Replies: 0
    Last Post: December 19th, 2011, 09:50 PM
  5. JNI Alternative?
    By janusmccarthy in forum Java Native Interface
    Replies: 1
    Last Post: November 14th, 2009, 12:22 PM