Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help, thanks.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help, thanks.

    In my code I am having problems with it recognizing the stack, and also passing it down parameters as well as recognizing everything in my 2nd method.

    import java.util.Scanner;
    import java.io.*;
    import java.util.Stack.*;
    import java.util.StringTokenizer;
     
    class Proj31610{
     
    public static void main(String args[]){
    String data=args[0];
    File filereader=new File(data); 
    Scanner in=new Scanner(filereader);
     
    String collect;
    Stack<Integer> holditdown=new Stack<Integer>();   //Float for the div?
     
    while(in.hasNext()){
     
    collect=in.nextLine();
    givmehabreak(collect, holditdown);
     
    		   }//end of the while loop 
     
     
     
    }//end of main method
     
     
    public void givmehabreak(string collects, Stack carrys){
    String tolken1, tolken2;
    int tolkentaker;
    StringTokenizer st= new StringTokenizer(collects);
     
    while(st.hasMoreTokens()){                    // ?
    tolken1=st.nexttoken;
    tolken2=st.nexttoken;
    		      }
    try{
    tolkentaker=Integer.parseInt(tolken2);                      //?
    }
     
    catch(ParseException e){
    System.out.println("There was no Integer here");
    }
     
    if(tolken1.equalsIgnoreCase("CLR"))
    clrStack(carrys);
     
    else if(tolken1.equalsIgnoreCase("HlT")){
    System.out.println("Machine Halted sir. ");
    System.out(0);
    					}
    else if(tolken1.equalsIgnoreCase("nEG"))
    negstack(carrys);
     
    else if(tolken1.equalsIgnoreCase("AdD"))
    addStack(carrys);
     
    else if(tolken1.equalsIgnoreCase("Sub"))
    subStack(carrys);
     
    else if(tolken1.equalsIgnoreCase("MuL"))
    mulStack(carrys);
     
    else if(tolken1.equalsIgnoreCase("dIv"))
    try{
    	divStack(carrys);
    }
    catch (DivideByZeroException e){
     
    System.out.println(" You cannot divide... anything by zero. ");
    }
     
    else if(tolken1.equalsIgnoreCase("LDC"))
    ldcStack(tolkentaker, carrys);
     
    else if(tolken1.equalsIgnoreCase("INp"))
    inpStack(carrys);
     
    else if(tolken1.equalsIgnoreCase("OuT"))
    outStack(carrys);
     
    else if(tolken1.equalsIgnoreCase("NlN"))
    System.out.println("");
     
    else if(tolken1.equalsIgnoreCase("dBg"))
    dgbStack(carrys);
     
    else
    System.out.println("Warning - illegal command '" + collects + "' ignored, good day sir. ");
     
     
    }//end of givemehabreak 
     
     
    public void clrStack(stack hey){
     
    while(hey.size() > 0)
    hey.pop();
     
    }//end of clear stack method
     
    public void negStack(stack reception){
    int reeves;
    if(reception.size() >0){
     
    reeves=reception.pop();
    reeves=reeves*-1;
    reception.push(reeves);
    }
     
    else
    System.out(0);
     
    }//end of negate stack method
     
    public void addStack(stack reception){
    int manny, harris, td;
     
    if(reception.size() > 2){
    manny=reception.pop();
    harris=reception.pop();
    td=manny+harris;
    reception.push(td);
    			}
    else
    System.out(0);
     
    }//end of add stack method
     
    public void subStack(stack reception){
    int rose, sat, nostats;
     
    if(reception.size() > 2){
    sat=reception.pop();
    rose=reception.pop();
    nostats=sat-rose;
    reception.push(nostats);
    			}
     
    else
    System.out(0);
    }//end subtract stack method 
     
    public void mulStack(stack reception){
    int twentwo, fourfive, lega;
     
    if(reception.size() > 2){
    twentwo=reception.pop();
    fourfive=reception.pop();
    lega=fourfive*twentwo;
    reception.push(lega);
    			}
     
    else
    System.out(0);
     
    }//end of multiply stack method
     
    public void divStack(stack reception){
    int ss, vc, dw;
     
    if(reception.size() > 2){
    ss=reception.pop();
    vc=reception.pop();
    dw=vc/ss;
    reception.push(dw);
    			}
    else
    System.out(0);
     
    }//end of divide stack method
     
    public void ldcStack(int locken, stack reception){
     
    reception.push(locken);
     
    }//end of LDC stack
     
     
     
    public void inpStack(stack reception){
    int masta;
     
     
    Scanner toldya= new Scanner(System.in);
    System.out.println("Enter that. ");
    masta=toldya.nextInt();
    reception.push(masta);
     
     
    }//end of input method
     
     
    public void outStack(stack reception){
    int tells;
     
    if(reception.size() > 1){
    tells=reception.pop();
    System.out.println(tells);
    			}
     
    else
    System.out(0);
     
     
    }//end of output, new line method
     
     
     
    public void dbgStack(stack reception){      //Will it just leave the stack empty?
     
    System.out.println(reception);
     
    }//end of Debug stack
     
    }//end of class Proj31610
    Errors I get alot of cannot find symbols, I confused.
    Last edited by JavaPF; October 10th, 2011 at 05:44 AM. Reason: You must use highlight or code tags!!

  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Please help, thanks.

    There are so many problems with this code.

    You don't have all the correct imports imported and you have made silly mistakes with method capitalization.
    For example, st.nexttoken(); needs to be st.nextToken();

    Certain methods need to be static etc.

    What IDE do you use? I personally use Eclipse and this points out all these issues clearly.
    Your going to need to go through this and touch up on your basic Java knowledge. Good luck.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.