Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Feb 27th, 2005, 7:39 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 348
Rep Power: 4 cwl157 is on a distinguished road
objects in java

I have all the objects written. However, when i run it it gives a NoSuchElement exception. I barely know what im doing can someone please help? Thanks
Here are the classes
Number class
import java.io.*;
import java.util.*;

public class Number
{
    Number()
    {
    }
    
 //gets a single input from the user, and returns it as a String
 String getInput()
 {
  java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
  java.io.BufferedReader console = new java.io.BufferedReader(input);

  try
  {
   return console.readLine();
  }
  catch(java.io.IOException e)
  {
   System.err.println("Bad NEWS!!!");
   return "";
  }
 }
 
  int getValue(String result)
    {
        Operator o = new Operator();
        int num = 0;
        String check = "";
        StringTokenizer tok = new StringTokenizer(this.getInput());
        do
        {
           check = tok.nextToken();
           if(!check.equals("+") || !check.equals("-") ||!check.equals("*") || !check.equals("/") || !check.equals("%"))
              num = Integer.parseInt(check);
           else
               o.getOp(result);
        }while(tok.hasMoreTokens());
        
        return num;
    }
 
 
 //Uses the StringTokenizer to break up the numeric experession and sends the tokens to the performOperation method
 int doOperation(String Exper)
 {
  int result=0; int num =0; String operation ="";
  StringTokenizer numEx = new StringTokenizer(Exper, "+-/%*", true);
  result = Integer.parseInt(numEx.nextToken());
  if(!numEx.hasMoreTokens())
  {
  return result;
  }
  do
  {
   operation = numEx.nextToken();
   num = Integer.parseInt(numEx.nextToken());
   result = Number.performOperation(result, num, operation);
  }while(numEx.hasMoreTokens());
  return result;
 }
 
 //performs the operation op on num1 and num2 returning the result as an int to result int the doOperation class
   static int performOperation(int num1, int num2, String op)
   {
    if(op.equals("+"))
    {
      return num1 + num2;
    }
    else if(op.equals("-"))
    {
     return num1 - num2;
    }
   else if(op.equals("*"))
   {
     return num1 * num2;
   }
  else if(op.equals("/"))
  {
   return num1 / num2;
  }
    else if(op.equals("%"))
   {
     return num1 % num2;
  }
  else
  {
   System.err.println("Not an operation: " + op);
   return -1;
  }
 }
}

Operator class
import java.io.*;
import java.util.*;

public class Operator
{
     Number t = new Number();
    String result = t.getInput();
    
    Operator()
    {
        this.getOp(result);
    }
    
    char getOp(String result)
    {
        char op = '+';
        String check = "";
        StringTokenizer tok = new StringTokenizer(t.getInput());
        do
        {
           check = tok.nextToken();
           if(check.equals("+"))
               op = '+';
           if(check.equals("-"))
               op = '-';
           if(check.equals("*"))
               op = '*';
           if(check.equals("/"))
               op = '/';
           if(check.equals("%"))
               op = '%';
        }while(tok.hasMoreTokens());
        
        return op;
    }
}

CalculatorOO this is the class that runs the program with main
import java.io.*;
import java.util.*;

public class CalculatorOO
{
    public static void main(String args[])
    {
        String cont = "";
        Number n = new Number();
        StringTokenizer result = new StringTokenizer(n.getInput());
        Operator op = new Operator();
        
        do
        {
          String exp = n.getInput();
          System.out.println(op.getOp(exp));
          System.out.println(n.getValue(exp));
        }while(result.hasMoreTokens());
         do
        {
         System.out.println("Please enter a numeric expression: ");
         System.out.print("Do you want to enter another expression (y/n): ");
         cont = n.getInput();
        }while(cont.equalsIgnoreCase("y"));
        System.out.println("Goodbye");
    }
}
cwl157 is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC