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 1 of 3 123 LastLast
Results 1 to 25 of 51

Thread: Output of numbers are incorrect sometimes.

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

    Default Output of numbers are incorrect sometimes.

    Hi I am trying to do a simple java code which outputs a number if i add 4 letters. When I do different combinations like d-c+a+b it gives me a inccorect number like 118.0. Can someone tell me where in my code my calculations are wrong..

    Thank you

    the ValVarPairs.txt contains these numbers-> a=100,b=5,c=10,d=13

    and below is the code I have.

    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];
      for (i = 0; i <= 2; i++){
       if (operators[i] == '+')
         result = result + values[i+1];  
       else
         result = result - values[i+1];
      }
      System.out.println(result);  
     }
    }


  2. #2
    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.

    Can you copy the contents of the command prompt window from when you execute the program that shows how the program is executed and what the program prints out when it executes?

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

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

    Default Re: Output of numbers are incorrect sometimes.

    Thank you for your quick reply Norm.

    I copied the output I received in Console:

    Enter letters and operators:
    d-c+a+b
    118.0


    Is this what you wanted?

  5. #4
    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.

    gives me a inccorect number like 118.0
    What is the correct answer?
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

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

    Default Re: Output of numbers are incorrect sometimes.

    In the console when I did d-c+a+b it gave me 118.0
    It was suppose to give me 108

  8. #6
    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.

    Time to start debugging to see where the code is going wrong. Add some println statements to print out the values of the variables as the code executes so you can see where the code is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

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

    Default Re: Output of numbers are incorrect sometimes.

    hi i just started doing java this week , how would I do a println statment and where would I add it?
    thanks

  11. #8
    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.

    how would I do a println statment
    Here is a sample from your code:
      System.out.println(result);
    I'd change it to include an id string so you know which println's output you are seeing:
      System.out.println("1res="+result); // print value of result with id
    Change the 1 to a different value for each println you add.

    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.

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

    soupi (October 24th, 2013)

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

    Default Re: Output of numbers are incorrect sometimes.

    so I would add System.out.println to if (j <= 2) operators[j++] = k;
    and
    operators[i] = '+'; ?

  14. #10
    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.

    Start with printing out the value of the one variable: result and see what is printed. If that doesn't help, you can always add more println statements for other variables.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

  16. #11
    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 can you give me another example of where I would put the first print statment in my code?

  17. #12
    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.

    Put a println statement AFTER EVERY line where the variable result is assigned a value:
      result = someValue;  //  this statement assigns result a value
      System.out.println("1res="+result); // print value of result with id
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

  19. #13
    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 still couldnt debug it, do you know why its giving me wrong input?

  20. #14
    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.

    Don't give up on debugging the code. You will have to know how to debug for all the programs that you write.

    What did the debug println statements print out?
    Copy and paste what is printed out and the new version of the program that has the println() statements so we can correlate the print out with where in the program it was printed.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

  22. #15
    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; }

    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("1res="+result);
    else
    result = result - values[i+1];
    System.out.println("1res="+result);
    }
    System.out.println("1res="+result);
    }
    }

  23. #16
    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.

    What printed out?

    All the println()s have the same id String: "1res" so you won't be able to tell which println printed which line of output. Change them so that are all different: 1res, 2res, 3res, 4res etc

    Don't forget to wrap the posted code in code tags.

    ONE PROBLEM I see in the code is that there aren't {}s to enclose the code in the if and else statements. You should ALWAYS wrap code in if and else statements in {} to prevent the problem this code is having.
    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. #17
    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
    1res=100.0
    2res=100.0

    it is suppose to be 400 for the output.

    --- Update ---

    Also your saying after I put if or else put {} right after?

  26. #18
    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 need to update the code and post the new version with the changed id strings and the added {}s following the if and else statements.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

  28. #19
    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; }
     
      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];  
     
     
       else {
         result = result - values[i+1];
      }
      System.out.println("2res="+result);  
     }
    }
    }
    }

  29. #20
    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.

    There are 2 places where result gets assigned a value that don't have println() statements in the code in post#19 that did have them in post#15. You need to have println() statements after ALL the places where the value of result is change.

    The else now has {}s but the if does not. You need to add {}s for the if also.

    Also the poor formatting of the code makes it very hard to see which } goes with its pairing {. There should not be two }s one directly above the other.
    The nested one should be indented 3-4 spaces.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

  31. #21
    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; }
     
      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("2res="+result);  
     }
    }
    }
    }

    I tried to run it and the code keeps going.
    btw thank you for your patience.

  32. #22
    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.

    What prints out when the program executes?

    The ids are not unique. I see res2 in two different places.
    Also there isn't a println() in the else

    Also there needs to be a println() outside the loop for the final value of result.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

  34. #23
    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; }
     
      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);  
     
      }
     }
    }
     
     
    {System.out.println("4res="+result);  
    }

    I did what u asked and the last } has a red X next to it

  35. #24
    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.

    Why is there a { on the next to last line?

    Check that all the }s have a pairing {.

    In general there should NOT be any code on the same line following a {.
    Also only put one statement per line. The println statements should be on their own lines, not on the same line with another statement.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soupi (October 24th, 2013)

  37. #25
    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; }
     
      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);  
    }
         {
    System.out.println("4res="+result);  
    }

    I am still getting the x's this is getting so frustrating. Maybe I shouldn't attempt to learn Java. I just dont know

Page 1 of 3 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