Results 1 to 10 of 10
  1. #1
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default Meet the code please

    import java.util.*;
    class Test1{

    public static void main(String[] args) throws Exception{

    Stack<String> stack = new Stack<String>();
    String[] s ={"<","blue",">","<","red",">","H","<","/","red",">","<","/","blue",">"};

    for(int i=0;i<s.length;i++){

    if(s[i].equals("<")&&!s[i+1].equals("/")){
    System.out.println("pushed: "+ stack.push(s[i+1]));

    if(s[i].equals(">")&&!s[i+1].equals("<")){
    System.out.println(" - "+s[i+1]);
    } else{
    System.out.println();
    }

    }else {
    if(s[i].equals("/")&&!s[i+1].equals(stack.peek())){
    System.out.println("error");
    }else{
    System.out.println("pop " + stack.pop());
    }
    }


    }
    }
    }

    Wanted output should be:
    pushed:blue
    pushed:red
    -H
    pop: red
    pop: red

    Somehow I can not get such order, any suggestions please????????????????

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    What output do you achieve instead?

    I notice a few errors in your code, one of which is:
    Java Code:
    		if(s[i].equals("<")&&!s[i+1].equals("/")){
    				System.out.println("pushed: "+ stack.push(s[i+1]));
    
    				if(s[i].equals(">")&&!s[i+1].equals("<")){
    The second condition will never be true. s[i] equals "<" to enter the first if statement, therefore s[i].equals(">") will always be false.

    PS: Use [code] tags in the future to preserve indenting and make it easier to read for all of us.

  3. #3
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks Zack for your attention, but I do not know how to use [code] tags,:(

  4. #4
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    not a viable excuse,
    it is literally "[code ] Actual Code [/code ]"

    Java Code:
    import java.util.*;
    class Test1{
    
    public static void main(String[] args) throws Exception{
    
    Stack<String> stack = new Stack<String>();
    String[] s ={"<","blue",">","<","red",">","H","<","/","red",">","<","/","blue",">"};
    
    for(int i=0;i<s.length;i++){
    
    if(s[i].equals("<")&&!s[i+1].equals("/")){
    System.out.println("pushed: "+ stack.push(s[i+1]));
    
    if(s[i].equals(">")&&!s[i+1].equals("<")){
    System.out.println(" - "+s[i+1]);
    } else{
    System.out.println();
    }
    
    }else {
    if(s[i].equals("/")&&!s[i+1].equals(stack.peek())){
    System.out.println("error");
    }else{
    System.out.println("pop " + stack.pop());
    }
    }
    
    
    }
    }
    }
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Cylab, I'm still curious as to the output your program is providing. And did you check over the if statements I pointed out?

  6. #6
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Zack, sorry; actually it is only portion of my code;

    import java.util.*;
    class Test1{

    public static void main(String[] args) throws Exception{

    Stack<String> stack = new Stack<String>();
    String[] s ={"<","color",">","<","red",">","<","white",">","H ","<","/","white",">","<","/","red",">","<","/","color",">"};
    boolean flag=true;

    for(int i=0;i<s.length;i++){

    if(s[i].equals("<")&&!s[i+1].equals("/")){
    System.out.println(stack.push(s[i+1]));
    }else if(s[i].equals(">")&&!s[i+1].equals("<")){
    System.out.println(s[i+1]);
    }else if(s[i].equals("/")&&s[i+1].equals(stack.peek())){
    stack.pop();
    // System.out.println("pop "+stack.pop());
    }else if(s[i].equals("/")&&!s[i+1].equals(stack.peek())){
    System.out.println(" error ");
    }else{flag=false;}

    }
    System.out.println(stack);
    }

    wanted output:
    color
    " " red
    " " white - H

    I was told that using content of stack, it is possible to control space. I just could not do.
    That is the only help I wish to get currently. I will be thankful for any suggestions for it.
    Thanks again

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, please use code tags so that we can see your formatted code.

    You don't need to create a new post but rather should edit your post above.

    To use code tags, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    using content of stack, it is possible to control space. I just could not do.
    Can you explain what this means?
    What does "control space" means?

  9. #9
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Read xml document and output with order of elements, so you can see which is child element or parent....
    E.g. as output sth like below;
    a
    " "b
    " "" "c
    " "" "d

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Your code needs some logic to detect at what level of nesting it is at and then to output spaces before the name of the element depending on that level.

Similar Threads

  1. Replies: 3
    Last Post: 03-10-2010, 05:51 PM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Replies: 0
    Last Post: 09-02-2009, 03:28 PM
  4. Replies: 0
    Last Post: 08-31-2009, 09:09 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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