Results 1 to 12 of 12
  1. #1
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Please help reading input from text file...

    Hello all! This is my first post and will much appreciated if someone can help out...

    I need to read input from text file "points.txt" which has a list of coordinates with the name in front. I have already completed it, however I was wondering how I could use the specified coordinated read from the text file to calculate distance from each point to the point (10, -2, 4).


    Prompt:

    The first line of the input file contains the number of points contained in the file. Then each point is defined by a string name and three integer coordinates with commas in between each value. Only one point will appear on a line and there will not be any blank lines between points. Your program must be able to load any number of points. Test cases with 100 to 1000 entries may be used to test your program.

    Here is the points.txt:

    27
    Yoda, -99, 99, -99
    Geordi, 165, -179, -195
    C-3PO, 47, 39, 59
    Troi, 245, 345, 555
    Q, -1010101, 0, 0
    Tarkin, -98, 3, 17
    Jean-Luc, 80, 160, 240
    Han, 40, 2, -4
    Jabba, 100, -100, 50
    Worf, 0, 300, -300
    Leia, -12, -2, -2
    Vader, -1, 0, 3
    Chewbacca, -70, -70, -70
    Spock, 909, 89, 438
    Luke, 9, 81, -2
    Lando, 45, 100, 88
    Greedo, -90, 98, 2
    Obi-Wan, 0, 101, 0
    Fett, 25, 100, 75
    Data, 501, -420, 325
    Guinan, 502, -611, 894
    Wesley, -220, -220, 885
    R2-D2, -80, 53, 61
    Riker, -200, 177, 141
    Ackbar, -50, 150, -2
    Beverly, 440, 440, 440
    Yar, -499, -761, 314

    My code:
    Java Code:
     List<String> coordinateList = new ArrayList<String>(); // creating new array to store words into
    		 BufferedReader br = null;
    		 try{
    			 br = new BufferedReader(new FileReader("points.txt")); //open the file
    			 String point;
    			 while((point = br.readLine())!= null){ // read lines until we get null
    				 coordinateList.add(point); // add the points to the coordinateList array
    			 }
    		 }catch(IOException e){
    			 e.printStackTrace();
    		 }finally{
    			 try{
    				 br.close(); // close file
    			 
    			 }catch(IOException ex){
    				 ex.printStackTrace();
    			 }
    		 }
    		 String[] points = new String[coordinateList.size()]; // initialize new string array equal to the size of coordinateList 
    		 coordinateList.toArray(points); //transfer coordinates toArray and transfer to string array points
    		 for(int i = 0; i < points.length; i++)
    			 System.out.println(points[i]);

    My code output everything correctly I just need to know how to get each point individually and calculate the distance. Thanks :D

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Please help reading input from text file...

    I'm not entirely sure what you want. You should show an example of what you have, and what you want. That being said, perhaps you need String.split().

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default Re: Please help reading input from text file...

    Java Code:
    String[] values = points[i].split(",");
    String name = values[0];
    int x = Integer.valueOf(values[1]);
    int y = Integer.valueOf(values[2]);
    int z = Integer.valueOf(values[3]);

  4. #4
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Please help reading input from text file...

    Quote Originally Posted by Reeling View Post
    I'm not entirely sure what you want. You should show an example of what you have, and what you want. That being said, perhaps you need String.split().
    well since the whole line is stored into and array i cant really calculate the distance with the name in front, I guess i would need to parse the data individually? I think that is the right word to use...However i am having trouble parsing each coordinate, does this make sense?

  5. #5
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Please help reading input from text file...

    thanks a lot Addez, however would i need to use the Integer.parseInt() as well?

  6. #6
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Please help reading input from text file...

    Okay so it's complaining about
    Exception in thread "main" java.lang.NumberFormatException: For input string: " -99"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.valueOf(Unknown Source)
    at hw2.main(hw2.java:56)


    here is my code:
    Java Code:
    		 for(int i = 1; i < points.length; i++) {
                 System.out.println(points[i]);
                 String[] values = points[i].split(",");
                 for (int k = 0; k < values.length; k++) {
                     System.out.println("values[" + k + "] = <" + values[k]+ ">");
                 }
    
                 String name = values[0];
                 System.out.println("name = " + name);
    
                 int x = Integer.valueOf(values[1]);
                 System.out.println("x = " + x);
                 
                 int y = Integer.valueOf(values[2]);
                 System.out.println("y = " + y);
                 
                 int z = Integer.valueOf(values[3]);
                 System.out.println("z = " + z); 
    		 }
    I have it specified as int but when i debug it, it somehow treats it like a string?

  7. #7
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Please help reading input from text file...

    never mind i figured out the problem i needed the String[] values = points[i].split(","); to have an extra space after the comma so it looks like this
    String[] values = points[i].split(", ");

  8. #8
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Please help reading input from text file...

    Java Code:
    String[] values = points[i].split(",");
    ...
    Integer.valueOf(values[1].trim());
    should also get you the correct results. This method would also allow for input without space before/after a comma.

  9. #9
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Please help reading input from text file...

    Thanks guys for answering all my questions! However I encountered another problem...

    So i have all my points and distances printed out, but how would I use my sorting algorithm to sort the distances in order. I know right now I need to somehow store the distances into an array, however I am not sure how would i proceed to do this... I already created my quick sort algorithm. Basically after the distances are sorted in order it should print out the points in order ex) Yoda, -99, 99, -99
    Here is the output of all the points:

    Yoda, -99, 99, -99
    Name = Yoda
    x = -99
    y = 99
    z = -99
    Distance = 180.80653

    Geordi, 165, -179, -195
    Name = Geordi
    x = 165
    y = -179
    z = -195
    Distance = 308.1477

    C-3PO, 47, 39, 59
    Name = C-3PO
    x = 47
    y = 39
    z = 59
    Distance = 77.94228

    Troi, 245, 345, 555
    Name = Troi
    x = 245
    y = 345
    z = 555
    Distance = 692.268

    Q, -1010101, 0, 0
    Name = Q
    x = -1010101
    y = 0
    z = 0
    Distance = 1010111.0

    Tarkin, -98, 3, 17
    Name = Tarkin
    x = -98
    y = 3
    z = 17
    Distance = 108.89445

    Jean-Luc, 80, 160, 240
    Name = Jean-Luc
    x = 80
    y = 160
    z = 240
    Distance = 294.68628

    Han, 40, 2, -4
    Name = Han
    x = 40
    y = 2
    z = -4
    Distance = 31.304953

    Jabba, 100, -100, 50
    Name = Jabba
    x = 100
    y = -100
    z = 50
    Distance = 140.78352

    Worf, 0, 300, -300
    Name = Worf
    x = 0
    y = 300
    z = -300
    Distance = 428.6257

    Leia, -12, -2, -2
    Name = Leia
    x = -12
    y = -2
    z = -2
    Distance = 22.803509

    Vader, -1, 0, 3
    Name = Vader
    x = -1
    y = 0
    z = 3
    Distance = 11.224972

    Chewbacca, -70, -70, -70
    Name = Chewbacca
    x = -70
    y = -70
    z = -70
    Distance = 128.45233

    Spock, 909, 89, 438
    Name = Spock
    x = 909
    y = 89
    z = 438
    Distance = 1002.4161

    Luke, 9, 81, -2
    Name = Luke
    x = 9
    y = 81
    z = -2
    Distance = 83.222595

    Lando, 45, 100, 88
    Name = Lando
    x = 45
    y = 100
    z = 88
    Distance = 136.69308

    Greedo, -90, 98, 2
    Name = Greedo
    x = -90
    y = 98
    z = 2
    Distance = 141.4355

    Obi-Wan, 0, 101, 0
    Name = Obi-Wan
    x = 0
    y = 101
    z = 0
    Distance = 103.56158

    Fett, 25, 100, 75
    Name = Fett
    x = 25
    y = 100
    z = 75
    Distance = 125.17987

    Data, 501, -420, 325
    Name = Data
    x = 501
    y = -420
    z = 325
    Distance = 720.30963

    Guinan, 502, -611, 894
    Name = Guinan
    x = 502
    y = -611
    z = 894
    Distance = 1185.346

    Wesley, -220, -220, 885
    Name = Wesley
    x = -220
    y = -220
    z = 885
    Distance = 936.26117

    R2-D2, -80, 53, 61
    Name = R2-D2
    x = -80
    y = 53
    z = 61
    Distance = 119.89162

    Riker, -200, 177, 141
    Name = Riker
    x = -200
    y = 177
    z = 141
    Distance = 308.07468

    Ackbar, -50, 150, -2
    Name = Ackbar
    x = -50
    y = 150
    z = -2
    Distance = 163.5237

    Beverly, 440, 440, 440
    Name = Beverly
    x = 440
    y = 440
    z = 440
    Distance = 755.2218

    Yar, -499, -761, 314
    Name = Yar
    x = -499
    y = -761
    z = 314
    Distance = 965.01917

    Here is my main:

    Java Code:
    public static void main(String[] args) throws IOException{
    		 List<String> coordinateList = new ArrayList<String>(); // creating new array to store words into
    		 BufferedReader br = null;
    		 try{
    			 br = new BufferedReader(new FileReader("points.txt")); //open the file
    			 String point;
    			 while((point = br.readLine())!= null){ // read lines until we get null
    				 coordinateList.add(point); // add the points to the coordinateList array
    			 }
    		 }catch(IOException e){
    			 e.printStackTrace();
    		 }finally{
    			 try{
    				 br.close(); // close file
    			 
    			 }catch(IOException ex){
    				 ex.printStackTrace();
    			 }
    		 }
    		 String[] points = new String[coordinateList.size()]; // initialize new string array equal to the size of coordinateList 
    		 coordinateList.toArray(points); //transfer coordinates toArray and transfer to string array points
    		 
    		 for(int i = 1; i < points.length; i++) {
                 System.out.println(points[i]);
                 String[] values = points[i].split(", ");
    //             for (int k = 0; k < values.length; k++) {
    //                 System.out.println("values[" + k + "] = <" + values[k]+ ">");
    //             }
    
                 String name = values[0];
                 System.out.println("Name = " + name);
    
                 int x = Integer.valueOf(values[1]);
                 System.out.println("x = " + x);
                 
                 int y = Integer.valueOf(values[2]);
                 System.out.println("y = " + y);
                 
                 int z = Integer.valueOf(values[3]);
                 System.out.println("z = " + z); 
                 
                 float distance = (float) Math.sqrt(Math.pow((x - 10), 2) + Math.pow((y - (-2)), 2) + Math.pow((z - 4), 2));
                 System.out.println("Distance = " + distance + "\n");
    		 }
    	 }
    And here is my quick sort:

    Java Code:
    public class QuickSort {
    	int[] a;
    	
    	public QuickSort(int[] inputArray) // ask for the array
    	{
    		a = inputArray;
    	}
    	
    	public int partition(int front, int back){
    		int i = front; //set i as the first element
    		int j = back; //set j as the last element
    		int pivot = a[(front + back)/2]; // use the middle as pivot
    		int tmp;
    		while(a[i] < pivot){ //check the front element of the array and see if it is smaller than the pivot point
    			i++; // if it is smaller then increment the front and check the next element
    		}
    		while(a[j] > pivot){ //check the back element of the array and see if it is greater than the pivot point
    			j--; // if it is larger then decrement the back and check the next element
    		}
    		if(i < j){ //if front is less than the back then swap them and increment them
    			tmp = a[i]; 
    			a[i] = a[j];
    			a[j] = tmp;
    			i++;
    			j--;
    		}
    		return i;
    	}
    	
    	public void sort(int front, int back){
    		if (front >= back) // if index front is greater than back jump out of the loop
    			return;
    		int p = partition(front, back); //
    		sort(front, p);
    		sort(p+1, back);
    	}
    }

  10. #10
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Please help reading input from text file...

    Well, I haven't gone over the quicksort, I'll assume it's correct. What, though, is the quicksort sorting? An Array of int. So... if you fed in an array containing the distances, you'd end up with a sorted array containing distances... how are you going to reference this back to the original data? Once you solve that, you're done.

  11. #11
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Please help reading input from text file...

    yeah my biggest problem right now is to somehow associate the distances to the original data... Can you please help me with this. As for the distances storing into an array can you also give me some hints as well thanks!

  12. #12
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Please help reading input from text file...

    Well, there are a number of ways to do this... I don't know how much you've gotten into data structures, so I'll give a simple [sic] solution.

    In your quicksort have two arrays. One for distances, and one for file location (i.e. 1 for Yoda, 2 for Geordi, etc.). Perform your quick sort.

    ex. a[] = {5, 6, 4, 2, 3}, b[] = {1, 2, 3, 4, 5} going in
    out -> a[] = {2, 3, 4, 5, 6}, b[] = {4, 5, 3, 1, 2}

    Then read through the input file (ugh, several times) to print out your solution. I say several times, because you'd have to save all of the
    data in order to minimize file reads... but first pass you'd print out the 4th entry, then 5th entry, etc...

    Optimal solution would involve reading the file once, and saving all of the data in a List (Set, Map, w/e) of objects, then not worrying about file contents (or IOExceptions!)

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  2. Help with reading input file
    By mgeno216 in forum New To Java
    Replies: 17
    Last Post: 02-11-2011, 11:04 PM
  3. Need help wit reading txt file into input.
    By ShinTec in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:40 PM
  4. reading from input file and then write on it
    By sara12345 in forum New To Java
    Replies: 9
    Last Post: 01-19-2010, 12:41 PM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •