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?
Code :
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);
}
}
Code :
--------------------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
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;
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!
Code :
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);
}
}
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.
Code :
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?