Why??

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 9
Reputation: kuay is an unknown quantity at this point 
Solved Threads: 0
kuay kuay is offline Offline
Newbie Poster

Why??

 
0
  #1
Jun 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,020
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 150
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Why??

 
0
  #2
Jun 22nd, 2009
Why not?
(Post code indented and with code=java tags and explain why the output is wrong - ie what did you expect?)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: Why??

 
0
  #3
Jun 22nd, 2009
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.
  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.
(
  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
  1. if(output ==-2999)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC