Output should be as show below:
and files are attached.
1. Here is example output for input file electricity.txt:
number of integers in file "electricity.txt" = 4
index = 0, element = 1877
index = 1, element = 1923
index = 2, element = 1879
index = 3, element = 2000
2. Here is example output for input file groceries.csv:
number of integers in file "groceries.csv" = 6
index = 0, element = 3
index = 1, element = 12
index = 2, element = 1
index = 3, element = 1
index = 4, element = 5
index = 5, element = 1
3. Here is example output for input file 1000.txt:
number of integers in file "1000.txt" = 1001
index = 0, element = 1000
index = 1, element = 2
index = 2, element = 3
index = 3, element = 5
index = 4, element = 7
index = 5, element = 11
index = 6, element = 13
index = 7, element = 17
index = 8, element = 19
index = 9, element = 23
index = 10, element = 29.....
............................................
.....index = 995, element = 7877
index = 996, element = 7879
index = 997, element = 7883
index = 998, element = 7901
index = 999, element = 7907
index = 1000, element = 7919
I am having trouble reading the integers an putting them into an array...below is the code I have so far.
import java.util.Scanner;
import java.io.File;
import java.util.StringTokenizer;
import java.util.InputMismatchException;
import java.io.FileNotFoundException;
public class KashiwabaraNicole3test
{
/*******************************************************************************
* Outputs integers from user input external files.
********************************************************************************/
public static void main(String[] commandlineArguments) {
//Error Checking For Command Line Arguments..
if(commandlineArguments.length == 0){
System.out.println("Please enter the file name " +
"as the 1st commandline argument.");
}
else{
//connect to file (does NOT create new file)
for(int i=0; i<commandlineArguments.length; i++)
{System.out.println();
File file = new File(commandlineArguments[i]);
Scanner inputFromFile = null;
try {
inputFromFile = new Scanner(file);
}
catch (FileNotFoundException exception) {
//Print error message.
//In order to print double quotes("),
//the escape sequence for double quotes (\") must be used.
System.out.print("ERROR: File not found for \"");
System.out.println(commandlineArguments[0]+"\"");
}
//if made connection to file, read from file
//keeps looping if file has more lines..
while (inputFromFile.hasNextLine()) {
//get a line of text..
String line = inputFromFile.nextLine();
try
{
Integer element = scan.nextInt();
for (int i=element;i<arraySize;i++)
{
element[i] = element[i];
}
}
catch(InputMismatchException exception)
{
}
}
String [] elementList = KashiwabaraNicole3test.createOutput(file);
KashiwabaraNicole3test.displayArray(elementList);
}//end of "if" for connecting to file
}//end of "else" for commandlineArguments
}//end of main() method
public static String [] createOutput (File myFile)
{
//need to declare "array"
Scanner scan = new Scanner(myFile);
try
{
Integer element = scan.nextInt();
for (int i=element;i<arraySize;i++)
{
element[i] = element[i];
}
}
catch(InputMismatchException exception)
{
}
return arraySize;
}
public static void displayArray(String [] anArray)
{
int num = 0;
Integer lengthOfArray = anArray.length;
System.out.println("number of intergers in file " + commandlineArguments + " = " + lengthOfArray);
for (int i = 0; i < lengthOfArray; index++)// Initialize the array values
{
Integer lengthOfArray = anArray[i].length();
System.out.println("index = " + num + " , element = " + anArray[i]);
num++;
}
}
}
bbcode