thank you very much Pertex for the rush help! I was desparate, as I was having a huge problem with troubleshooting all of my errors, so hoped I could get some help in time before assignment due, from anyone here, so thank you very much Pertex!
I got some help from a class~school mate and my java teacher, and I eventually got my program to work. Most of my mistakes were simple-stupid syntax, as I've not learned how to do them correctly yet, had to do a lot of research (which I now understand better how to look up the errors in the oracle java doc~resource).
I didn't define my Parameters, I got confused with how to do the Type Casting (Data Type Conversions), I used 'hasInt' where I was to use 'nextInt', and I was confused how to work with the Scanner File Object (didn't know what defining to do with it for my parameter variable for it, I tried 'string'... didn't know it was, the Class itself: Scanner), I think that was most of my syntax errors... Oh ya, I couldn't get the 'pow' and 'sqrt' to work... (you can see my comments in my post program below about this, if interested).
I then had some logic errors, I had to take care of...
and the big logic error was that I was getting '0' returned-displayed for my 'mean' and 'standard deviation', and it took me literally plugging in 'msg' [Java: System.out.print("HK_1......99"); ] everwhere to figure it out... as I looked and looked, totally baffled at why I was getting the zeroes.
the cause was that the Scanner was at the EoF (End of File)... so when it went to do the 'mean' and 'standard deviation' functions... there was no 'hasNext' (=true), so my 'mean' and 'standard deviation' functions were not running, thus the ' mean ' and 'standard deviation ' were still as their initial values of 0.
then, I had a small logic error with the mean and std dev functions, in using initial counter at 1 and counter++, when I needed to use counter at 0 and ++counter .... I hope anyways that my mean and standard dev are working correctly now... I hate math... I've no idea if my std dev is working right or not, laughs. I don't even know what standard deviation is, heh.
anyways, here's my working program:
// Packages~Libraries~Modules:
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import static java.lang.Math.pow; // this is for only needing to use ' pow() '
import static java.lang.Math.sqrt; // this is for only needing to use ' sqrt() '
// otherwise, I have to use ' Class_name.Function_name '
// aka for this case: ' Math.pow() ' and ' Math.sqrt() '
// the ' Math ' Class is apart of the ' lang ' package,
// which is auto-imported by the java program already
// Classes:
public class Lab04
{
// Class-Global Constants ~ Variables ~ Object Instantiation:
static Scanner console = new Scanner(System.in);
static double mean, standard_deviation;
static String file_data;
static final String file_name = "input.txt";
// Methods~Functions:
public static void main(String[] args) throws FileNotFoundException
{
Header();
Crediting_Helpers();
Purpose();
Program();
}
public static void Header()
{
// Header Info Displayment:
}
public static void Crediting_Helpers()
{
// Crediting Helpers Displayment:
}
public static void Purpose()
{
// Purpose Displayment:
System.out.println("The purpose of this program is to get input values "
+ "from a file and \ncalculate the mean and standard deviation, and"
+ " displaying the results \nto the user.\n\n");
}
public static void Program() throws FileNotFoundException
{
// Local Variable Definitions ~ Initializations ~ Object Instantiating:
Scanner read_file = new Scanner(new FileReader("input.txt"));
// just for segmenting these file prompts from rest of program:
System.out.println("----------------------------------------" +
"----------------------------------------\n\n");
// prompting the user that the 'input.txt' file is opened, and being
// read by the program:
System.out.println("The \'input.txt\' file has been opened, and is " +
"being read by the program.\n\n");
// Get, Concatenate~Compute, and return new file data values, storing
// them into variables for the later-on displayment:
file_data = Concatenate_File_Data(read_file);
// close the 'input.txt' File (due to being at the end of the file):
read_file.close();
if (file_data != null)
{
// re-create it, as now it's back to the beginning of file
Scanner read_file_again = new Scanner(new FileReader("input.txt"));
mean = Compute_Mean(read_file_again);
// close the 'input.txt' File (due to being at the end of the file):
read_file_again.close();
// re-create it, as now it's back to the beginning of file
Scanner read_file_again_again =
new Scanner(new FileReader("input.txt"));
standard_deviation =
Compute_Standard_Deviation(read_file_again_again, mean);
// close the 'input.txt' File:
read_file_again_again.close();
}
// prompt the user that the 'input.txt' file has been closed:
System.out.println("The \'input.txt\' file's data has been read and \n"
+ "stored, and the file has been closed.\n\n");
// no simple way of clearing the console screen with java, so I am just
// separating the file prompts from actual stock ticker program:
System.out.println("----------------------------------------" +
"----------------------------------------\n\n");
// checking+correcting variables for their displaying of the results,
// and the displayment of the results:
if (file_data == null)
{
System.out.printf("%s contains:%nInput: null (The file has no " +
"integer data that this program requires)%nMean: null%nStandard"
+ " Deviation: null%n%n", file_name);
}
else if (((mean == 0.0)||(mean >= 0.0))&&(standard_deviation == -1))
{
System.out.printf("%s contains:%nInput: %s%nMean: %.2f%nStandard "
+ "Deviation: (Error: imaginary numbers: square root of " +
"negative number)%n%n", file_name, file_data, mean,
standard_deviation);
}
else
{
System.out.printf("%s contains:%nInput: %s%nMean: %.2f%nStandard "
+ "Deviation: %.2f%n%n", file_name, file_data, mean,
standard_deviation);
}
}
public static String Concatenate_File_Data(Scanner read_file_parameter)
{
if (!read_file_parameter.hasNextInt())
{
return (null);
}
else
{
String local_string = String.valueOf(read_file_parameter.nextInt());
while (read_file_parameter.hasNextInt())
{
local_string +=
(" " + String.valueOf(read_file_parameter.nextInt()));
}
return(local_string);
}
}
public static double Compute_Mean(Scanner read_file_parameter)
{
int local_sum = 0, local_counter = 0;
while (read_file_parameter.hasNextInt())
{
local_sum += read_file_parameter.nextInt();
++local_counter;
}
if (local_sum == 0)
{
return((double)(local_sum));
}
else
{
return((double)(local_sum)/(double)(local_counter));
}
}
public static double Compute_Standard_Deviation(Scanner read_file_parameter,
double mean_parameter)
{
double local_sum = 0.0;
int local_counter = 0;
while (read_file_parameter.hasNextInt())
{
local_sum += Math.pow((double)(read_file_parameter.nextInt()) -
mean_parameter, 2.0);
++local_counter;
}
if (local_sum > 0.0)
{
return(Math.sqrt(local_sum/(double)(local_counter)));
}
else if (local_sum == 0.0)
{
return (local_sum);
}
else
{
return (-1);
}
}
}
----------------
this may be ignorant, but these languages (C++, Python, and Java), are horrible! Quest's XML~user_level, is so so so so wonderfully easy compared to these 3 languages. Alex and Co. have done such an amazing job with quest!
Now having experience with learning other languages, I can emphatically say that using quest to learn to program is much better than in trying to use at least these 3 languages to learn to program!
C++ and Python are easier for me, Java is the one I'm having most difficult with, so far. Though the classes are going at different speeds... so it's a bit unfair to compare the 3 languages, as I think we're the furthest into Java material vs the other 2.