I am supposed to read points from a file and accept those points as an array so that I can implement Grahams Scan, however my program only seems to accept the first line of points can anyone help?
import java.io.*;
import java.util.*;
public class Graham {
private static int[] get_arr(String input) throws Exception
{
String squirtle;
String[] wartortle;
int[] arr;
//Gets the file
BufferedReader in = new BufferedReader(new FileReader(input));
squirtle = in.readLine();
//delimits numbers with a comma for eventual output
wartortle = squirtle.split(",");
arr = new int[wartortle.length];
//parses values into array
for (int i = 0; i < wartortle.length; i++)
{
arr[i] = Integer.parseInt(wartortle[i]);
}
return arr;
}
public static void main(String[] args) throws Exception
{
//initializes answer which is used to see if the user wants to repeat the prgm
String answer;
do
{
String input;
int[] arr;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a source filename such as C:\\Users\\user_\\Documents\\file.txt, or if it's in the same folder as this class just file.txt");
input = scanner.next();
System.out.println("The points");
//Used for outputting file contents
BufferedReader br = new BufferedReader(new FileReader(input));
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
//Gather array
arr = get_arr(input);
//Prints array
System.out.println(Arrays.toString(arr));
System.out.println("Thank you for using this program, if you would like to run it again press y if not press anything else");
answer = scanner.next();
}
//whenever the user inputs y the program runs again
while (answer.equals("y")) ;
}
}
my text file called garr.txt looks like
1,2
2,3
3,4
4,5
5,6
But only accepts 1,2
The problem in the code is you read only single line from your input file. You should read all lines from the file. You can Change get_arr method as follows: