I've been scratching my head on this all day trying to figure out how to do this. I have tried multiple different ways, but I feel I've been going about this all wrong. My assignment is to read 50 integers from a .txt file and put the contents into a sorted array, then list the highest/lowest/average number, but I can hardly get past step one.
These are the 50 numbers in the text file
64 61 169 113 81 61 206 176 39 100 22 200 128 152 59 165 67 116 165 72 26 149 58 204 188 69 203 94 96 134 83 122 192 85 62 159 35 162 95 92 126 66 66 203 187 18 132 182 181 175
In this file I've managed to get the "proj8" file to at least print.
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.io.*;
public class ect7{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("proj8.txt"));
String line = br.readLine();
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
In this file I've managed to get the "proj8" file to print and somewhat order them, but insanely so.
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.io.*;
public class ect73{
public static void main(String[] args) throws IOException {
//int [] myArr = new int[50];
//FileReader fr = new FileReader("proj8.txt");
BufferedReader br = new BufferedReader(new FileReader("proj8.txt"));
List<String> lines = new ArrayList<String>();
String line = null;
//String line = br.readLine();
while ((line = br.readLine()) != null) {
lines.add(line);
Collections.sort(lines);
//System.out.println(line);
System.out.println(lines);
}
br.close();
//return lines.toArray(new String[lines.size()]);
}
}
I know I'm doing this all wrong but I have no idea how to do this right. I need to be able to input the .txt file into a sorted array in integer and then list the highest/lowest/average number. Any help is good help, but the more simple the code the better.
javatag?ints, or How do I extract anintfrom aString), as is this looks like a "Can you do my homework" question.