Jump to content

What is wrong with this codes

- - - - -

  • Please log in to reply
21 replies to this topic

#1
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts
public class SubtractFile {


    public static void main(String[] a) throws IOException {

        edmw("\\thesubtraction.txt");

    }


    static void edmw(String filename) throws IOException {

        Reader r = new BufferedReader(new FileReader(filename));

        StreamTokenizer stok = new StreamTokenizer(r);

        stok.parseNumbers();

        double finalresult = 0;

        int count = 0;


        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {

            if (stok.ttype == StreamTokenizer.TT_NUMBER) {

                if (count == 0) {

                    finalresult = stok.nval;

                } else {

                    finalresult -= stok.nval;

                }

                count++;

            }

            else  {

                System.out.println("Nonnumber: " + stok.sval);

                stok.nextToken();

            }

            System.out.println("The result of subtraction is " + finalresult);

        }


    }

}

What is wrong with these code ? I am trying to perform an action whereby the 1st integer minus away the 2nd integer. Followed by the 2nd integer minus away the 3rd integer in the file.

#2
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
how are you compiling the program, are you using the cmd prompt or are you using an ide?

#3
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
i using a ide ...

#4
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I dont see a problem with your method. Maybe its your file thats the problem(wrong location).

Try to use the command line to compile the code. Have your input file in the same location as the compiled code(you should change the file from "\\thewhatever.txt" to only "thewhatever.txt").

If you insist on using an ide:
Depending on the ide put the txt file in with your class files(not the same with all ides) but still change \\thesubtraction.txt to only thesubtraction.txt.

#5
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Sorry about the previous post, there are some problems. You never go to the next token the number continually subtracts causing an infinite loop.

Here is the code based of from yours to modify(It is close to what you want):

import java.io.*;


public class SubtractFile {


    public static void main(String[] a) throws IOException {

        edmw("thesubtraction.txt");

    }


    static void edmw(String filename) throws IOException {

        BufferedReader r = new BufferedReader(new FileReader(filename));

        StreamTokenizer stok = new StreamTokenizer(r);

		

        double finalresult = 0.0;

        int count = 0;

        double numA, numB = 0.0;


        // this is where you process the next token until end of file	

        while (stok.nextToken() != StreamTokenizer.TT_EOF) {


           // this was good error check

           if (stok.ttype == StreamTokenizer.TT_WORD) {

                System.out.println("Nonnumber: " + stok.sval);

           } 


           // edit the inner if statements for your desired results

	if (stok.ttype == StreamTokenizer.TT_NUMBER) {

                if (count % 2 == 1) {

                    numA = stok.nval;

	         finalresult = numB - numA;

	         System.out.println("The result of subtraction is " + finalresult);

                } else if(count % 2 == 0){

                    numB = stok.nval;

                }

            }

            // you want to increment the count here

            count++;

        }

        r.close();// always close the buffered reader

     }// end of method

}



#6
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts

mr mike said:

Sorry about the previous post, there are some problems. You never go to the next token the number continually subtracts causing an infinite loop.

Here is the code based of from yours to modify(It is close to what you want):

import java.io.*;


public class SubtractFile {


    public static void main(String[] a) throws IOException {

        edmw("thesubtraction.txt");

    }


    static void edmw(String filename) throws IOException {

        BufferedReader r = new BufferedReader(new FileReader(filename));

        StreamTokenizer stok = new StreamTokenizer(r);

		

        double finalresult = 0.0;

        int count = 0;

        double numA, numB = 0.0;


        // this is where you process the next token until end of file	

        while (stok.nextToken() != StreamTokenizer.TT_EOF) {


           // this was good error check

           if (stok.ttype == StreamTokenizer.TT_WORD) {

                System.out.println("Nonnumber: " + stok.sval);

           } 


           // edit the inner if statements for your desired results

	if (stok.ttype == StreamTokenizer.TT_NUMBER) {

                if (count % 2 == 1) {

                    numA = stok.nval;

	         finalresult = numB - numA;

	         System.out.println("The result of subtraction is " + finalresult);

                } else if(count % 2 == 0){

                    numB = stok.nval;

                }

            }

            // you want to increment the count here

            count++;

        }

        r.close();// always close the buffered reader

     }// end of method

}


Yup, the result is more better that the previous. But the problem I'm now facing is that the 1st number - 2nd number and it directly skipped to 3rd number - 4th number. Which left out 2nd number - the 3rd number. Did I done anything wrong ?

#7
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Look at this part of the code. Modify the nested "if else if " statement to get your desired result.


if (stok.ttype == StreamTokenizer.TT_NUMBER) {

   if (count % 2 == 1) {

         numA = stok.nval;

         finalresult = numB - numA;

         System.out.println("The result of subtraction is " + finalresult);

   } else if(count % 2 == 0){

      numB = stok.nval;

   }

}



#8
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts

mr mike said:

Look at this part of the code. Modify the nested "if else if " statement to get your desired result.


if (stok.ttype == StreamTokenizer.TT_NUMBER) {

   if (count % 2 == 1) {

         numA = stok.nval;

         [COLOR="#f4a460"]finalresult = numB - numA;[/COLOR]

         System.out.println("The result of subtraction is " + finalresult);

   } else if(count % 2 == 0){

      [COLOR="#f4a460"]numB = stok.nval;[/COLOR]

   }

}


I had try to modify this 2 but still unable to achieve the result.

#9
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Can you show your work? Do you know what the modulo symbol is doing?

#10
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts
if (stok.ttype == StreamTokenizer.TT_NUMBER) {

   if (count % 2 == 1) {

         numA = stok.nval;

         numB = stok.nval;

         finalresult = numC - numB - numA;

         System.out.println("The result of subtraction is " + finalresult);

   } else if(count % 2 == 0){

      numc = stok.nval;

   }

}

I dont get what does that modulo does.

#11
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
% means modulo or remainder 4/2= 0 remainder, 5/2 = 1 remainder, ect.

The code I showed you uses modulo to subtract the first digit from the second, third from fourth, fith from sixth and so on.
You dont want modulo, I just gave you that example so that you can see what the program is doing and introduce some
basics. You should change the inner if statement for your desired result:
if your count = 0
store a value
count++
else
do the math and print result

Hope this helps.

#12
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts
I still don't get it. If I'm using modulo and let's say the value is 92. Then 92/2 = 0 so it will not minus any value ?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users