Results 1 to 10 of 10
Thread: Meet the code please
- 08-11-2010, 08:27 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 29
- Rep Power
- 0
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????????????????
- 08-11-2010, 08:36 AM #2
What output do you achieve instead?
I notice a few errors in your code, one of which is:
The second condition will never be true. s[i] equals "<" to enter the first if statement, therefore s[i].equals(">") will always be false.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("<")){
PS: Use [code] tags in the future to preserve indenting and make it easier to read for all of us.
- 08-11-2010, 10:49 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 29
- Rep Power
- 0
Thanks Zack for your attention, but I do not know how to use [code] tags,:(
- 08-11-2010, 01:44 PM #4
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 ~-'
- 08-11-2010, 08:54 PM #5
Cylab, I'm still curious as to the output your program is providing. And did you check over the if statements I pointed out?
- 08-12-2010, 12:23 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 29
- Rep Power
- 0
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
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 08-12-2010, 03:43 PM #8
Can you explain what this means?using content of stack, it is possible to control space. I just could not do.
What does "control space" means?
- 08-12-2010, 04:08 PM #9
Member
- Join Date
- Jun 2010
- Posts
- 29
- Rep Power
- 0
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
- 08-12-2010, 04:14 PM #10
Similar Threads
-
Nice to meet you, my dear developers,I got some questions
By wjcking in forum New To JavaReplies: 3Last Post: 03-10-2010, 05:51 PM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Meet and Interact with Top Recruiters and Recruiting companies of USA.
By esurvey in forum Jobs OfferedReplies: 0Last Post: 09-02-2009, 03:28 PM -
Meet and Interact with Top Recruiters and Recruiting companies of USA.
By esurvey in forum Jobs DiscussionReplies: 0Last Post: 08-31-2009, 09:09 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks