943,870 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 281
  • Java RSS
Jun 22nd, 2009
0

Why??

Expand Post »
import java.util.Scanner;


public class stack {

/**
* @param args

*/
public static void main(String[] args) {


int result ;
int output ;
Scanner num = new Scanner(System.in);
int input = num.nextInt();

for(int j = 0;j<input;j++)
{
int output1 = 0;
output = num.nextInt();

for(int i = 0;i<output;i++)
{

if(output == 1 - 3000){
output = output1;
return;

}
if(output!=1-3000){

if(output ==('+')){
result = (output1 + output1);
result++ ;
System.out.println(result);
}
else if(output==('-')){
result = (output - output);
result++;
System.out.println(result);
}

else if(output == ('*')){
result = (output * output);
result++;
System.out.println(result);
}
}
}
}
}
}


Please Tell me
Why error.
input
9
7 2 – 5 2 * 36 - *
output
-130
Reputation Points: 9
Solved Threads: 0
Newbie Poster
kuay is offline Offline
9 posts
since Jun 2009
Jun 22nd, 2009
0

Re: Why??

Why not?
(Post code indented and with code=java tags and explain why the output is wrong - ie what did you expect?)
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Jun 22nd, 2009
0

Re: Why??

I understood what you are trying to do but I think you should write it again. because it wont work. ok here how you should write it.
1 ) make a loop that turns until there is no token left on scanner.
java Syntax (Toggle Plain Text)
  1. for(int j = 0;j<input;j++)
and this is not it. input variable stores first interger token. which is 7 so it will loop 7 times
2) if next token is number put next token in stack if it is not number it shuld be + - * or / then pop 2 number from stack and do the calculation and put result back in stack.at the and you should have only one. number and it will be result.
note) if at the end ,there is more than one number in stack given string is not valid. when you got an operator from scanner and there is less than two number in stack string is not valid. and if string you got is not number nor operator string is not valid. good luck.
(
java Syntax (Toggle Plain Text)
  1. if(output == 1 - 3000)
I dont know what this is and you probably dont know what this is either.this is same thing as
java Syntax (Toggle Plain Text)
  1. if(output ==-2999)
Reputation Points: 31
Solved Threads: 5
Light Poster
yilmazhuseyin is offline Offline
48 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to modify the code to Creating multiple instances of a Class <<Very basic>>
Next Thread in Java Forum Timeline: nested Arraylists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC