Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Need Help in java. AGAIN! =)


    using stacks and arrays
    Given problem:
    String Input: {this[is(just)a]test}
    Elements inside the curly braces: [t, h, i, s, [, i, s, (, j, u, s, t, ), a, ], t, e, s, t]
    Elements inside the brackets: [i, s, (, j, u, s, t, ), a]
    Elements inside the parentheses: [j, u, s, t]

    output of my program:
    String Input: {this[is(just)a]test}
    Elements inside the curly braces: [t, h, i, s, [, i, s, (, j, u, s, t, ), a, ], t, e, s, t]
    Elements inside the brackets: [i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a, i, s, (, j, u, s, t, ), a]
    Elements inside the parentheses: [j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t, j, u, s, t]

    nag.balik2x ang output sa Elements inside the brackets ugElements inside the parentheses:
    here is my source codes:
    Code:
    import java.util.*;
    public class Brackets {
        private String input;
        private char [] array;
        private int index1=0,index2=0;
        private static Scanner in = new Scanner(System.in);
        private static Stack myStack = new Stack();
        public Brackets(){
            askInput();
            Process();
        }
        private void askInput(){
            System.out.println("String Input:");
            setInput(in.nextLine());
            setCharArray(new char [getInput().length()]);
        }
        private void Process(){
            for(int x =0; x<getInput().length();x++){
                char i = getInput().charAt(x);
                array[x] = i;
            }
            InsideTheCurlyBraces();
            InsideTheBrackets();
            InsideTheParentheses();
        }
        private void InsideTheCurlyBraces(){
            for(int x=0; x<getCharArray().length;x++){
                if(array[x]=='{'){
                    index1 =x;
                }
                if(array[x]=='}'){
                    index2 = x;
                }
                for(int y=index1+1; y<index2; y++){
                myStack.push(array[y]);
                }
            }
            
            System.out.println("Elements inside the curly braces: "+myStack);
            myStack.clear();
            index1 =0;
            index2 =0;
        }
        private void InsideTheBrackets(){
            for(int x=0; x<getCharArray().length;x++){
                if(array[x]=='['){
                    index1 =x;
                }
                if(array[x]==']'){
                    index2 = x;
                }
                for(int y=index1+1; y<index2; y++){
                myStack.push(array[y]);
                }
            }
            
            System.out.println("Elements inside the brackets: "+myStack);
            myStack.clear();
            index1 =0;
            index2 =0;
        }
        private void InsideTheParentheses(){
            for(int x=0; x<getCharArray().length;x++){
                if(array[x]=='('){
                    index1 =x;
                }
                if(array[x]==')'){
                    index2 = x;
                }
                for(int y=index1+1; y<index2; y++){
                myStack.push(array[y]);
                }
            }
            
            System.out.println("Elements inside the parentheses: "+myStack);
            myStack.clear();
            index1 =0;
            index2 =0;
        }
        private void setCharArray(char [] array){
            this.array = array;
        }
        private char [] getCharArray(){
            return array;
        }
        
        
        private void setInput(String input){
            this.input = input;
        }
        private String getInput(){
            return input;
        }
        
        public static void main(String args[]){
            Brackets b = new Brackets();
        }
        
    }




    anyone can help?tnx

  2. #2

    Default Re: Need Help in java. AGAIN! =)

    question sa bro, {this[is(just)a]test}, mao ra na ang pattern?
    1 curly, 1 bracket, 1 parenthesis at any order?

    or pwede ni?

    [{this{is[just](a){test}}(oh yeah)}]

  3. #3

    Default Re: Need Help in java. AGAIN! =)

    ang

    Code:
    for(int y=index1+1; y<index2; y++){
         myStack.push(array[y]);
    }
    naka Nested Loop which is unnecessary since nangita ra ka kung asa ang character.
    I-extract ang nested loop from the first loop like this.

    Code:
    for(int x=0; x<getCharArray().length;x++){
          if(array[x]=='('){
               index1 =x; //nangita ra ka sa first instance of (
          }
          if(array[x]==')'){
              index2 = x; //nangita ra ka sa last instance of (
         }
    }
    //gawas dapat kay mao man ang parameter nimo sa next loop.
    //mag balik2x jud na imu code kay nag loop pa sa first
    for(int y=index1+1; y<index2; y++){
         myStack.push(array[y]);
    }
    Btw, IMHO, it is better to reset at first, than sa last of the code like this.

    Code:
    private void InsideTheParentheses(){
            myStack.clear(); //reset here
            index1 =0; //reset here
            index2 =0; //reset here
            for(int x=0; x<getCharArray().length;x++){
                if(array[x]=='('){
                    index1 =x;
                }
                if(array[x]==')'){
                    index2 = x;
                }
            }
            for(int y=index1+1; y<index2; y++){
                myStack.push(array[y]);
            }
            System.out.println("Elements inside the parentheses: "+myStack);
        }
    Since nag initialize ka balik sa value para makita nimo kung naunsa imung value. IMHO.

  4. #4
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Need Help in java. AGAIN! =)

    Quote Originally Posted by stealthghost View Post
    question sa bro, {this[is(just)a]test}, mao ra na ang pattern?
    1 curly, 1 bracket, 1 parenthesis at any order?

    or pwede ni?
    bsag unsa bro. himo2x rato nako ang sample. kung unsai masud sa {}, [] ug () i output.

    [{this{is[just](a){test}}(oh yeah)}]
    Quote Originally Posted by stealthghost View Post
    ang

    Code:
    for(int y=index1+1; y<index2; y++){
         myStack.push(array[y]);
    }
    naka Nested Loop which is unnecessary since nangita ra ka kung asa ang character.
    I-extract ang nested loop from the first loop like this.

    Code:
    for(int x=0; x<getCharArray().length;x++){
          if(array[x]=='('){
               index1 =x; //nangita ra ka sa first instance of (
          }
          if(array[x]==')'){
              index2 = x; //nangita ra ka sa last instance of (
         }
    }
    //gawas dapat kay mao man ang parameter nimo sa next loop.
    //mag balik2x jud na imu code kay nag loop pa sa first
    for(int y=index1+1; y<index2; y++){
         myStack.push(array[y]);
    }
    Btw, IMHO, it is better to reset at first, than sa last of the code like this.

    Code:
    private void InsideTheParentheses(){
            myStack.clear(); //reset here
            index1 =0; //reset here
            index2 =0; //reset here
            for(int x=0; x<getCharArray().length;x++){
                if(array[x]=='('){
                    index1 =x;
                }
                if(array[x]==')'){
                    index2 = x;
                }
            }
            for(int y=index1+1; y<index2; y++){
                myStack.push(array[y]);
            }
            System.out.println("Elements inside the parentheses: "+myStack);
        }
    Since nag initialize ka balik sa value para makita nimo kung naunsa imung value. IMHO.
    try nako ni bro. update lng ko ugma

  5. #5
    Elite Member
    Join Date
    Jun 2010
    Gender
    Male
    Posts
    1,018

    Default Re: Need Help in java. AGAIN! =)

    Bai, I have to say, nagimprove gyud imong coding style.
    Mas OOP na tanawon.

    Otherwise kuwang ra nimo...
    Usa nimo i-add check sa kung existing na ba.
    Kung wala add, kung naa skip lang.

    Code:
    for(int y=index1+1; y<index2; y++){
        if(myStack.search(array[y]) == -1){
                myStack.push(array[y]);
        } }
    Mao ni solution if you don't want to change your algorithm.
    Otherwise, rather than looping the entire string again, you can just loop once and
    You can have state or boolean variables to denote what is inside what.
    Example, if you come across a open curly brace({), you turn insideCurlyBrace = true, then push all relevant characters inside a insideCurlyBraceStack, then turn insideCurlyBrace = false when you get the Close Curly Brace (}). Theoretically, a different state can be turned on in the middle of the loop and you push the character in different stacks at the same time. This also means you can have three different states set to true at the same time.
    Last edited by Klave; 02-24-2012 at 08:47 PM.

  6. #6
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Need Help in java. AGAIN! =)

    @stealthghost, ang pattern jud bro kai 1 curly, 1 bracket, 1 parenthesis in order like this = { [ ( ) ] },di man nuon ni assignment or project, exercise2x lng ba, mao nang nahan ko mu think outside the box kunohay.
    @klave, kuha na nako bro, pero pasabta ko sa ani na code
    Code:
    for(int y=index1+1; y<index2; y++){
        if(myStack.search(array[y]) == -1){
                myStack.push(array[y]);
        } }
    nganung == -1 man? hehehe tnx ninyu

  7. #7
    Elite Member
    Join Date
    Jun 2010
    Gender
    Male
    Posts
    1,018

    Default Re: Need Help in java. AGAIN! =)

    matud pa sa Stack nga documentation.
    Stack (Java 2 Platform SE v1.4.2)

    Ang i-return sa search nga function kung dili niya makit-an kay -1.
    Common ni siya basta mga search or lookup or matching nga function basta ang i-return kay ang index.
    Kung makitan within the list, kung ikapila siya example kaduha siya, mao sad ireturn (= 2).
    Otherwise, kung dili makit-an dili man sad pwede gamiton 0 kay sa ubang list or collection type nga object first object mana.
    Mao -1 meaning walay nakit-an.

  8. #8
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Need Help in java. AGAIN! =)

    ah mao diay. tnx bro

  9. #9

    Default Re: Need Help in java. AGAIN! =)

    habang tumatagal gumagaling ka na jairoh...

  10. #10

    Default Re: Need Help in java. AGAIN! =)

    kani maoy thread!!

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. Need help in JAVA plsss.. help 25problems..
    By FrozenBoi in forum Programming
    Replies: 89
    Last Post: 02-09-2010, 02:26 PM
  2. NEED HELP in JAVA EXCELAPI
    By rastaman81 in forum Programming
    Replies: 1
    Last Post: 05-11-2009, 09:17 PM
  3. need help in java programming: creating a folder
    By rastaman81 in forum Programming
    Replies: 4
    Last Post: 03-11-2009, 08:51 AM
  4. Need help in Installing Java programs in my Samsung D880
    By Soj in forum Software & Games (Old)
    Replies: 0
    Last Post: 04-24-2008, 06:34 PM
  5. Want to buy a new mp3 player. Need help in selecting one.
    By metdumangas in forum Gizmos & Gadgets (Old)
    Replies: 21
    Last Post: 01-04-2006, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top