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.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 51

Thread: Output of numbers are incorrect sometimes.

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    I am still getting the x's
    Please copy the full text of the compiler's error messages and paste it here.


    The {}s are still not correctly placed. Their positions should reflect the nesting level of the statements they enclose.
    The highest level statements should be to the left. Nested statements should be moved to the right 3-4 spaces.


    Some lines have extra {s before the statement. There should NOT be any code on the same line following a {. Move the statement to the next line so the { is very easy to see.
    If you don't understand my answer, don't ignore it, ask a question.

  2. The Following User Says Thank You to Norm For This Useful Post:

    soupi (October 24th, 2013)

  3. #27
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    error is below, can u show me the correct way to do this?

    Error

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete Block
    Syntax error, insert "}" to complete MethodBody

    at com.ecsgrid.testC.main(testC.java:68)

    package com.ecsgrid;
     
    import java.io.*;
     
    public class testC {
     
    public static void main(String[] args) {
      int i = 0,j = 0;
      double result, values[] = new double[4];
      char k, operators[] = new char[3];
      for (i = 0; i <= 2; i++) 
        operators[i] = '+';      // default is to add the values
     
      File myfile;
      StreamTokenizer tok;
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String InputText;
     
      i = 0;
      try {
        myfile = new File("C:\\VarValPairs.txt");
        tok = new StreamTokenizer(new FileReader(myfile));  
        tok.eolIsSignificant(false);
     
        while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)){
          if ((tok.ttype == StreamTokenizer.TT_NUMBER))
          {
            values[i++] = tok.nval;
          }
      }
      }
      catch(FileNotFoundException e) { System.err.println(e);  return; }
      catch(IOException f) { System.out.println(f); return; }
     
      System.out.println("Enter letters and operators:");
     
      try {
        InputText = in.readLine(); 
      }  
      catch(IOException f) { System.out.println(f); return; }
     
      for (i = 0; i < InputText.length(); i++)
      {
         k = InputText.charAt(i);
         if ((k == '+') || (k == '-'))
         {
     
           { if (j <= 2) operators[j++] = k;   
         }
      } 
     
      result = values[0];
      System.out.println("1res="+result);
     
      for (i = 0; i <= 2; i++)
      {
     
       if (operators[i] == '+')
      {
         result = result + values[i+1];    
         System.out.println("2res="+result);
     
       }
       else {
         result = result - values[i+1];   
         System.out.println("3res="+result);  
    }
         {

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    make sure all {s having a pairing }
    If the statements are properly formatted, it is a lot easier
    class AClass {
        void aMethod() {
            if(cond)  {
               // do something
               for(int i....) {
                    // inside for
               }  // end for(i)
               if(cond2) {
                   // inside if
               }
            }
         }  // end aMethod()
    } // end class

    I add comments to some of the ending }s so I can easily tell what block of code they are at the end of: class, methods and for() loops
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    soupi (October 24th, 2013)

  6. #29
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    package com.ecsgrid;
     
    import java.io.*;
     
    public class testC  {
     
      public static void main(String[] args) {
        int i = 0,j = 0;
        double result, values[] = new double[4];
        char k, operators[] = new char[3];
        for (i = 0; i <= 2; i++) {
          operators[i] = '+';      // default is to add the values
        }
        File myfile;
        StreamTokenizer tok;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String InputText;
     
        i = 0;
        try {
          myfile = new File("C:\\VarValPairs.txt");
          tok = new StreamTokenizer(new FileReader(myfile));  
          tok.eolIsSignificant(false);
     
          while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)){
            if ((tok.ttype == StreamTokenizer.TT_NUMBER))
            {
              values[i++] = tok.nval;
            }
          }
        }
        catch(FileNotFoundException e) { System.err.println(e);  return; }
        catch(IOException f) { System.out.println(f); return; }
     
        System.out.println("Enter letters and operators:");
     
        try {
          InputText = in.readLine(); 
        }  
        catch(IOException f) { System.out.println(f); return; }
        System.out.println("Your input is: " + InputText);
        for (i = 0; i < InputText.length(); i++)
        {
          k = InputText.charAt(i);
          if ((k == '+') || (k == '-')) {
            if (j <= 2)  { 
              operators[j++] = k;
            }    
          }
        }
        result = values[0];
        System.out.println("1res="+result);
     
        for (i = 0; i <= 2; i++)
        {
     
          if (operators[i] == '+')
          {
            result = result + values[i+1];    
            System.out.println("2res="+result);
          } else  {
            result = result - values[i+1];   
          }
     
        }
     
     
        System.out.println(result);  
      } //end of main method
    } //end of class

    I still have the same issue with the the numbers not coming correct

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    still have the same issue with the the numbers not coming correct
    Yes, nothing that was done up to now has changed the program's results. The purpose of debugging a program is to find out why it is not generating the correct results. When that is understood, then the code can be changed to make the results correct.

    Please post the contents of the console that shows the program's input and output.


    The code still does not have a println in the else statement near the end.

    Also it does not print out the final value of result with an id.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    soupi (October 24th, 2013)

  9. #31
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Can you show me how it is done, when I tried to follow your steps I shows error messages.

  10. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    That's what I am trying to do: show you how to debug the code and find the problem. To do that you need to add println statements, execute the program, copy the output and paste it here. Hopefully the print out will show what the problem is.

    can you do it
    What is the 'it' you are asking about?
    From post#8:
    Add a println that prints out the value of result after EVERY statement that changes the value of result so you can see what values the program is assigning to result every time its value is changed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    soupi (October 24th, 2013)

  12. #33
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Enter letters and operators:

    Your input is:
    1res=100.0
    2res=105.0
    2res=115.0
    2res=128.0
    128.0

    thats my output in the console.

  13. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    You didn't include the expression the program was to evalute???

    Given these values: a=100,b=5,c=10,d=13
    Looking at the printout and associating the contents of result with the values of the variables:
    1res=100.0 a
    2res=105.0 +b
    2res=115.0 +c
    2res=128.0 +d
    128.0 <<< This would be the results of a+b+c+d
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    soupi (October 24th, 2013)

  15. #35
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Enter letters and operators:
    A+a+a+a
    Your input is: A+a+a+a
    1res=100.0
    2res=105.0
    2res=115.0
    2res=128.0
    128.0

    it should be 400 but it still appeared to be 128.0

  16. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    That looks like the code does not find the values for the variable names in the expression but just uses the 4 values in the order that they were saved in the ValVarPairs.txt file.

    Try another expression besides: A+a+a+a and see what values are used.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    soupi (October 24th, 2013)

  18. #37
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    I think I solved it, what do you think?

    package com.ecsgrid;
     
    import java.io.*;
     
    public class testC {
     
        public static void main(String[] args) {
            int i = 0, j = 0;
            double result, values[] = new double[4];
            char k, operators[] = new char[3];
            for (i = 0; i <= 2; i++)
                operators[i] = '+'; // default is to add the values
     
            File myfile;
            StreamTokenizer tok;
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String InputText;
     
            i = 0;
            try {
                myfile = new File("C:\\VarValPairs.txt");
                tok = new StreamTokenizer(new FileReader(myfile));
                tok.eolIsSignificant(false);
     
                while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)) {
                    if ((tok.ttype == StreamTokenizer.TT_NUMBER))
                        values[i++] = tok.nval;
                }
                for (int l = 0; l < values.length; l++) {
                    System.out.println(values[l]);
                }
            } catch (FileNotFoundException e) {
                System.err.println(e);
                return;
            } catch (IOException f) {
                System.out.println(f);
                return;
            }
     
            System.out.println("Enter letters and operators:");
     
            try {
                InputText = in.readLine().toUpperCase();
            } catch (IOException f) {
                return;
            }
     
            if(InputText.length() > 0){
                operators = new char[InputText.length()];
            } else {
                System.out.println("No Operations specified");
                return;
            }
            for (i = 0; i < InputText.length(); i++) {
                k = InputText.charAt(i);
                operators[j++] = k;
            }
     
            result = 0; 
            for (i = 0; i < operators.length; i++) {
                if(i+1<operators.length)
                switch(operators[i]){
                case '+':
                    if(operators[i+1] != '+' && operators[i+1] != '-'){
                        result+=values[(int)operators[i+1] - (int)'A'];
                        i++;
                    }
                    break;
                case '-':
                    if(operators[i+1] != '+' && operators[i+1] != '-'){
                        result-=values[(int)operators[i+1] - (int)'A'];
                        i++;
                    }
                    break;
                default:
                    result = values[(int)operators[i] - (int)'A'];
                    break;
                };        }
            System.out.println(result);
        }
    }

  19. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Does the program work for several different input expressions?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #39
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    when I try to subtract more then 2 numbers it outputs only the subtraction of the first two numbers.
    for example
    100.0
    5.0
    10.0
    13.0
    Enter letters and operators:
    A-A-A-A
    -200.0

  21. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Is that the correct answer?
    If you don't understand my answer, don't ignore it, ask a question.

  22. #41
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    yes, thank you so much for all your help.
    I really appreciate it

  23. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Glad you got it working.

    --- Update ---

    What changes would you have to make to the program if the ValVarPairs.txt file were changed to contain these numbers-> a=100,b=5,x=10,y=13? Then the expressions could be: x-y+a+b
    If you don't understand my answer, don't ignore it, ask a question.

  24. The Following User Says Thank You to Norm For This Useful Post:

    soupi (October 24th, 2013)

  25. #43
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Quote Originally Posted by Norm View Post
    Glad you got it working.

    --- Update ---

    What changes would you have to make to the program if the ValVarPairs.txt file were changed to contain these numbers-> a=100,b=5,x=10,y=13? Then the expressions could be: x-y+a+b
    Wouldnt I change the ValVarPairs.txt file, so instead of c=10 d=13 I would exchange it with x=10, y=10

  26. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Try it and see what happens. Give x and y different values for clarity in showing test results.
    I think you will get a surprise.

    You'll need to get some help again to fix the code to make it work with these variable names.
    If you don't understand my answer, don't ignore it, ask a question.

  27. #45
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    ok, do you know how I would do something this similar in PHP?

  28. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Sorry, I don't know PHP.

    What happened when you changed the variables: c and d to x and y?
    If you don't understand my answer, don't ignore it, ask a question.

  29. #47
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    A+b+c+d worked still but,
    when I did A+b+x+y it showed this error.
    A
    +
    +
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 23
    at com.ecsgrid.testC.main(testC.java:68)

  30. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Yes, that looks like the error the code will get.

    What assumptions does the code make about the variables' names and the order they are defined in the ValVarPairs.txt file?
    If you don't understand my answer, don't ignore it, ask a question.

  31. #49
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Output of numbers are incorrect sometimes.

    that it goes in the order, anything after 'A' will be calculated

  32. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Output of numbers are incorrect sometimes.

    Are you saying that only the variables: a,b,c,d will work and that the order of their values given in the ValVarPairs.txt file must be in this order: a,b,c,d?

    That should be documented in the program.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 1
    Last Post: September 27th, 2013, 04:51 AM
  2. Incorrect output and other problems
    By lanmonster in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 14th, 2013, 10:38 AM
  3. Output doesn't display numbers
    By Sylis in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 14th, 2012, 11:51 PM
  4. need numbers to output onto a new line
    By oscar22 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2011, 07:32 AM
  5. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM

Tags for this Thread