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

Thread: creating a new method that's not outputting correct result

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default creating a new method that's not outputting correct result

    I'm trying to create a new method called getTotal which is supposed to take all the numbers in the array and add each of them up to get a total of all the numbers. I was supposed to print each number in the array out in rows of 4 scores each which is done correctly. However, when I try and add the scores array to get a total I get a bunch of 0's out of nowhere. The output is below the code. What am I doing wrong?

     
    import java.io.*;
     
     
    public class staticMethods{
     
    public static void main(String[] args) throws IOException{
     
    	File dataFile = new File("C:\\Documents and Settings\\Mark\\Desktop\\Scores.txt");
    	BufferedReader br = new BufferedReader(new FileReader(dataFile));
     
    	String scoresString = new String();
    	String[] scoresArray = new String[36];
    	int[] scoresIntArray = new int[36];
     
    	scoresString = br.readLine();
    	scoresArray = scoresString.split(" ");
     
    	for(int i = 0; i < scoresArray.length; i++)
    	{
     
    		scoresIntArray[i] = Integer.parseInt(scoresArray[i]);
    		String displayRows = new String();
     
     
    			if (i < scoresArray.length) {
                    displayRows += scoresArray[i++];
                }
     
                if (i < scoresArray.length) {
                    displayRows += " " + scoresArray[i++];
                }
     
               if (i < scoresArray.length) {
                    displayRows += " " + scoresArray[i++];
                }
     
     
     
                if (i < scoresArray.length) {
                    displayRows += " " + scoresArray[i];
                }
     
                System.out.println(displayRows);
     
     
     
    	}
    	br.close();
    	getTotal(scoresIntArray);
     
    }
     
     
    			public static void getTotal(int[] scores)
    				{
     
    					int total = 0;
     
    					for(int i = 0; i <scores.length; i++)
    					{
    						System.out.println(scores[i]);
    						total = total + scores[i];
     
     
     
    					}
     
    					System.out.println("The total of the array is " + total);
     
     
     
    			}
     
     
    }




     
    --------------------Configuration: <Default>--------------------
    67 64 93 81
    92 98 13 75
    89 81 56 88
    99 71 80 97
    58 78 74 84
    21 64 72 69
    78 87 84 72
    96 83 68 62
    88 90 23 75
    67
    0
    0
    0
    92
    0
    0
    0
    89
    0
    0
    0
    99
    0
    0
    0
    58
    0
    0
    0
    21
    0
    0
    0
    78
    0
    0
    0
    96
    0
    0
    0
    88
    0
    0
    0
    The total of the array is 688


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: creating a new method that's not outputting correct result

    It appears that you're incrementing i several times in the same iteration, hence it's exiting quicker. If all of them are occurring under the same condition, then combine them under the same if block.

    if (i < scoresArray.length) {
    displayRows += scoresArray[i++];
    }

    if (i < scoresArray.length) {
    displayRows += " " + scoresArray[i++];
    }

    if (i < scoresArray.length) {
    displayRows += " " + scoresArray[i++];
    }



    if (i < scoresArray.length) {
    displayRows += " " + scoresArray[i];
    }

    Each of those i++'s is incrementing the variable i.
    i++ is the same as i = i + 1;

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating a new method that's not outputting correct result

    I changed it but I still don't understand how I'm overincrementing. When I do the four score row... I thought it's printing scoresIntArray[0] then incrementing then going to 1 then to 2 and so on until it reaches the 4th which is the current i. Then it increments after that as it repeats the loop. Or am I misunderstanding something?


    edit: I think I fixed it. Thank you for your help!

    import java.io.*;
     
     
    public class staticMethods{
     
    public static void main(String[] args) throws IOException{
     
    	File dataFile = new File("C:\\Documents and Settings\\Mark\\Desktop\\Scores.txt");
    	BufferedReader br = new BufferedReader(new FileReader(dataFile));
     
    	String scoresString = new String();
    	String[] scoresArray = new String[36];
    	int[] scoresIntArray = new int[36];
     
    	scoresString = br.readLine();
    	scoresArray = scoresString.split(" ");
     
    	for(int i = 0; i < scoresArray.length; i++)
    	{
     
    		scoresIntArray[i] = Integer.parseInt(scoresArray[i]);
     
    	}
     
    		String displayRows = new String();
    		for(int i = 0; i < scoresIntArray.length; i++)
    		{
    			displayRows += scoresIntArray[i++] + " " + scoresIntArray[i++] +  " " + scoresIntArray[i++] + " " + scoresIntArray[i] + "\n";
     
                }
    	System.out.println(displayRows);
     
     
     
    	br.close();
    	getTotal(scoresIntArray);
     
    }
     
     
    			public static void getTotal(int[] scores)
    				{
     
    					int total = 0;
     
    					for(int j = 0; j <scores.length; j++)
    					{
    						System.out.println(scores[j]);
    						total = total + scores[j];
     
     
     
    					}
     
    					System.out.println("The total of the array is " + total);
     
     
     
    			}
     
     
    }
    Last edited by orbin; June 22nd, 2012 at 11:57 PM.

  4. #4
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating a new method that's not outputting correct result

    I'm having trouble with another method I'm trying to create. I'm trying to find a position of a number in the array but I previously sorted it in the getMedian method. I thought when I sorted it in the median method, it only stays sorted for the median method. However, whenever I try to find a position of a number in the array it finds it in the sorted array, but I want to find it from the original array.

    import java.io.*;
    import java.util.Arrays;
     
     
    public class staticMethods{
     
    public static void main(String[] args) throws IOException{
     
    	File dataFile = new File("C:\\Documents and Settings\\Mark\\Desktop\\Scores.txt");
    	BufferedReader br = new BufferedReader(new FileReader(dataFile));
     
    	String scoresString = new String();
    	String[] scoresArray = new String[36];
    	int[] scoresIntArray = new int[36];
     
    	scoresString = br.readLine();
    	scoresArray = scoresString.split(" ");
     
    	for(int i = 0; i < scoresArray.length; i++)
    	{
     
    		scoresIntArray[i] = Integer.parseInt(scoresArray[i]);
     
    	}
     
    		String displayRows = new String();
    		for(int i = 0; i < scoresIntArray.length; i++)
    		{
    			displayRows += scoresIntArray[i++] + " " + scoresIntArray[i++] +  " " + scoresIntArray[i++] + " " + scoresIntArray[i] + "\n";
     
                }
    	System.out.println(displayRows);
     
     
     
    	br.close();
     
    	getTotal(scoresIntArray);
    	getAverage(scoresIntArray);
    	getLowest(scoresIntArray);
    	getHighest(scoresIntArray);
     
    	getMedian(scoresIntArray);
    	getPosition(scoresIntArray, 93);
     
    }
     
     
    			public static void getTotal(int[] scores)
    				{
     
    					int total = 0;
     
    					for(int j = 0; j <scores.length; j++)
    					{
    						System.out.println(scores[j]);
    						total = total + scores[j];
     
     
     
    					}
     
    					System.out.println("\nThe total of the array is " + total);
     
     
     
    			}
     
    			public static void getAverage(int[] scores){
    				int total = 0;
    				int average = 0;
     
    				for(int i = 0; i < scores.length; i++)
    				{
    					total += scores[i];
    					average = (total / scores.length);
     
    				}
     
    				System.out.println("The average of the array is " + average);
     
     
    			}
     
    			public static void getLowest(int[] scores)
    			{
     
    				int lowest = 100;
     
    				for(int i = 0; i < scores.length; i++)
    					{
     
     
    				if (scores[i] < lowest)
     
    				{
    					lowest = scores[i];
     
    				}
     
    					}
     
    				System.out.println("The lowest number in the array is " + lowest);
    			}
     
     
    			public static void getHighest(int[] scores)
    			{
    				int highest = 0;
     
    				for(int i = 0; i < scores.length; i++)
     
    				{
    					if(scores[i] > highest)
    					{
    						highest = scores[i];
     
    					}
     
     
    				}
    				System.out.println("The highest number in the array is " + highest);
     
    			}
     
    		public static void getMedian(int[] scores)
    		{
     
    			int median = 0;
     
    			Arrays.sort(scores);
     
     
    			int middle = (scores.length / 2);
     
    			if ( 0 == scores.length / 2)
     
    			median = (scores[middle] + scores[middle+1])/2;
     
    			else if (0 != scores.length / 2)
    			{
    				median = scores[middle];
     
    			}
     
     
     
    	System.out.println("The median of the array is " + median);
     
    		}
     
     
    		public static void getPosition(int[]scores, int number)
    		{
     
    			for (int i = 0; i < scores.length; i++)
     
     
    				if(scores[i] == number)
    			System.out.println("The position of the number in the array is " + i);
     
     
     
     
     
     
     
     
    		}
    }

    How do I "unsort" it back to the original array?

Similar Threads

  1. Replies: 2
    Last Post: February 12th, 2012, 07:35 AM
  2. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 05:26 PM
  3. Help Creating a method for my jellyBean gussing game
    By CookiesForRandy in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 20th, 2010, 09:29 AM
  4. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 08:58 AM
  5. Crude method of using offset whilst using BufferedReader - Correct way?
    By Pr0ject-Rec0n in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 9th, 2010, 10:59 PM