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

Thread: Large number while adding arrays

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Large number while adding arrays

    I am trying to add the arrays in the method anArray, but when i try to see the value, which should be 134/3 , it is returning "Their Test Sum = 33500" instead of the value of 134/3, why is it printing such a large number?


    public class FinalProject {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)throws IOException {
            //Choice Of Options:
            //File I/O *
            //Arrays       
            //"String Class" Substr, CharAt etc.        
            int testScores = readFile();
            System.out.println("My Test Scores Avg: " + testScores);
            anArray();
        }
        public static int readFile () throws IOException {
            FileInputStream fstream = new FileInputStream("c:/scores.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //Reading File
            String strLine;
            int myScores = 1;
            while ((strLine = br.readLine ()) != null) {
                myScores = Integer.parseInt(strLine);
                return myScores;
            }
            return myScores;
        }
        public static void anArray () {
            int[] hisTest;
            hisTest = new int [3];
            hisTest[0] = 33;
            hisTest[1] = 50;
            hisTest[2] = 51;
     
     
            System.out.println("Their Test Sum = " + hisTest[0] + hisTest[1]
                    + hisTest[2]/3);
        }
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Large number while adding arrays

    This does not look correct:

        public static int readFile () throws IOException {
            FileInputStream fstream = new FileInputStream("c:/scores.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //Reading File
            String strLine;
            int myScores = 1;
            while ((strLine = br.readLine ()) != null) {
                myScores = Integer.parseInt(strLine);
                return myScores;
            }
            return myScores;
        }

    The code is guaranteed to return after reading one line given the return statement inside of the while loop. Why is that there?
    Also, consider putting in println statements to debug your code and to see what the program is actually reading in.

    Also curious: why are you using a FileInputStream and not a FileReader wrapped by the BufferedReader?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Large number while adding arrays

    "Their Test Sum = 33500" instead of the value of 134/3,
    Did you copy that correctly? I get: Their Test Sum = 335017
    Here is what the code is doing: "33" + "50" + 51/3

    If you want the + to be an arithmetic operation vs String concatenation, wrap the expression in ()s
    If you don't understand my answer, don't ignore it, ask a question.

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

    curmudgeon (December 6th, 2012)

Similar Threads

  1. Adding the integers of a number
    By irishkid in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 3rd, 2012, 02:39 AM
  2. [SOLVED] ARRAYs - Display number of students who failed
    By roronoaz in forum Java Theory & Questions
    Replies: 6
    Last Post: July 2nd, 2012, 09:51 AM
  3. [Help] Problem with Class arrays, adding/editing and deleting
    By Grant_Searle in forum Object Oriented Programming
    Replies: 7
    Last Post: December 16th, 2011, 10:10 AM
  4. My program keeps lagging when dealing with large arrays.
    By chrynelson in forum Java Theory & Questions
    Replies: 4
    Last Post: October 21st, 2011, 04:57 PM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM