Results 1 to 19 of 19
  1. #1
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default Input data from file to arrays problem

    Hi
    I have a problem I don't now where is my mistake. I have a file with First name , Last name and 8 scores like this:
    Pall Paulin 9.8, 9.2, 9.3, 9.4, 9.7, 9.5, 9.1, 9.9 for 25 peoples.

    I have to input the data from this file to a two two-dimensional arrays, one is from String type another is double type:
    Here is my code
    Java Code:
    package chapter09;
    
    import java.io.*;
    import java.util.*;
    
    public class CHO9EX042 {
    	int row,col;
    	double sc;
    	String fn, ln;
    	static Vector<String> ime = new Vector<String>();
    	static String names[][] = new String[25][2];
    
    	static double scores[][] = new double[25][8];
    
    	Scanner myScanner = new Scanner(("CHO9EX04data.txt"));
    
    	String inp = myScanner.nextLine();
    
    	Scanner a2 = new Scanner(inp);
    	{
    		while (myScanner.hasNext()) {
    
    			for (row = 0; row < names.length; row++) {
    				for(col=0; col<names[row].length; col++){
    				names[row][0] = a2.nextLine();
    				names[row][1] = a2.nextLine();
    
    				for (row = 0; row <scores.length; row++)
    					for (col = 0; col < scores[row].length; col++) 
    						scores[row][col] = a2.nextDouble();
    
    						myScanner.close();
    
    					
    				}
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    
    		printName(names);
    		printscore(scores);
    		System.out.println();
    	}
    
    	public static void printName(String[][] names) {
    		int row, col;
    
    		for (row = 0; row < names.length; row++) {
    			for (col = 0; col < names[row].length; col++)
    				System.out.printf("%7s", names[row][col]);
    
    			System.out.println();
    		}
    	}
    
    	public static void printscore(double[][] scores) {
    		int ro, co;
    
    		for (ro = 0; ro < scores.length; ro++) {
    			for (co = 0; co < scores[ro].length; co++)
    				System.out.printf("%7f", scores[ro][co]);
    
    			System.out.println();
    		}
    	}
    }

    Any suggestions in which row I make mistakes.
    When i print out the Array they are empty no data from the file :confused:

    Thank's

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    competition, each contestant's score is score by droping the lowest and
    highest scores and then adding the remaining scores.Write a program that allows the user
    to enter 8 judges' scores and then outputs the points received by the contestant.
    Name class CH09EX04 in package chapt09
    Use input data file CH09EX04data.txt in package chapt09
    Input file contains each contestant’s name (first & last) and 8 scores
    Display each contestant’s name and score in order, highest score to lowest – maximum 25 contestants per event
    Input file should be read only once.
    Use Arrays and vectors.

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i dont know why you code programs like that...
    but
    Java Code:
    {
    		while (myScanner.hasNext()) {
    
    			for (row = 0; row < names.length; row++) {
    				for(col=0; col<names[row].length; col++){
    				names[row][0] = a2.nextLine();
    				names[row][1] = a2.nextLine();
    
    				for (row = 0; row <scores.length; row++)
    					for (col = 0; col < scores[row].length; col++) 
    						scores[row][col] = a2.nextDouble();
    
    						myScanner.close();
    
    					
    				}
    			}
    		}
    	}
    never execute

    for creating a scanner, use File in your case
    Scanner sc = new Scanner(new File("myNumbers"));
    remember to catch exception
    Last edited by mtyoung; 03-12-2009 at 10:26 AM.

  5. #5
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Do you now what I'm missing??? Or what is wrong??

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    1
    Java Code:
    Scanner myScanner = new Scanner(("CHO9EX04data.txt"));
    myScanner get input String "CHO9EX04data.txt", but not the content in file CHO9EX04data.txt

    2
    Java Code:
    {
     while (myScanner.hasNext()) {
     ....
    }
    execute only if you construct a CHO9EX042 object

    3
    why do you use 2 scanner object?

    4
    i found that you misuse for loops
    Java Code:
    for (row = 0; row < names.length; row++) {
    				for(col=0; col<names[row].length; col++){
    				names[row][0] = a2.nextLine();
    				names[row][1] = a2.nextLine();
    	}
    }
    Java Code:
    for (row = 0; row <scores.length; row++)
    	for (col = 0; col < scores[row].length; col++) 
    			scores[row][col] = a2.nextDouble();
    have you found something wrong here?

    5
    Pall Paulin 9.8, 9.2, 9.3, 9.4, 9.7, 9.5, 9.1, 9.9

    you use nextLine method to fetch first and last name, which are in the same line?
    and nextDouble cannot fetch "9.8,"

    6
    when will you close myScanner?
    in the middle of task or some where else?
    Last edited by mtyoung; 03-13-2009 at 01:27 AM.

  7. #7
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Thank you for the tips.

  8. #8
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default Still have problem with the input in the Array

    Hi
    I finally did some of the input but now inputs data for 7 persons. Can you look at it see where is my mistake. Thanks
    P.S. it was a lot of work but I'm happy because I get some data into arrays.




    Java Code:
    package chapter09;
    
    import java.io.*;
    import java.util.*;
    
    public class CHO9 {
    
    	
    	public static void main(String[] args) throws FileNotFoundException {
    
    		String names[][] = new String [25][2];
    		double scores [][] = new double [25][8];
    		Scanner my = new Scanner (new FileReader("src//CHO9EX04data.txt"));
    		
    		int r = 0;
    		while (my.hasNext()){
    			
    		String lm = my.nextLine();
    	
    		Scanner as = new Scanner(lm);
    		
    		names[r][0] = as.next();
    		names[r][1] = as.next();
    			
    		//r++;
    		//int sc;
    		while(as.hasNextDouble()){
    			
    			//double score = as.nextDouble();
    			scores[r][0]= as.nextDouble();
    			scores[r][1] =as.nextDouble();
    			scores[r][2] =as.nextDouble();
    			scores[r][3] =as.nextDouble();
    			scores[r][4] =as.nextDouble();
    			scores[r][5] =as.nextDouble();
    			scores[r][6] =as.nextDouble();
    			scores[r][7] =as.nextDouble();
    			
    		//}
    		
    		
    		r++;
    		
    		
    		
    		
    			
    		}}
    		my.close();
    		printW(names);
    		printS(scores);
    	}
    		public static void printW(String[][] names) {
    			int row, col;
    
    			for (row = 0; row < names.length; row++) {
    				for (col = 0; col < names[row].length; col++)
    					System.out.printf("%7s", names[row][col]);
    
    				System.out.println();}
    		}
    
    		public static void printS(double[][] scores) {
    			int row, col;
    
    			for (row = 0; row < scores.length; row++) {
    				for (col = 0; col < scores[row].length; col++)
    					System.out.printf("%7s", scores[row][col]);
    
    				System.out.println();}
    		}
    
    
    
    }

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    can i ask what is the content in CHO9EX04data.txt

    if content like
    Pall Paulin 9.8 9.2 9.3 9.4 9.7 9.5 9.1 9.9

    will be fine

    if like
    Pall Paulin 9.8, 9.2, 9.3, 9.4, 9.7, 9.5, 9.1, 9.9
    program will not able to get nextDouble as "9.8," is not a double
    Last edited by mtyoung; 03-14-2009 at 04:28 AM.

  10. #10
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Yes, the commas in input file was the problems.

    I have one more question??

    How I can get the data sort like:

    First name , Last name scores(high to low) total

    after executing the program

  11. #11
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if the number of scores is fixed, i suggest to use next()
    and replace the "," to "" (read String (Java Platform SE 6))
    and parse string to double(read Double (Java Platform SE 6))

    please read
    Arrays (Java Platform SE 6)
    and try
    Array.sort(xxx)
    Last edited by mtyoung; 03-14-2009 at 06:44 AM.

  12. #12
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Hi
    I read it and try to do it, but I did'n get success. can you type my how should the code look like for parsing the String to Array and to set two arrays to one

    THANK'S

  13. #13
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default final result question???

    Hi I do have idea to get final result with using Vector?

    Is this is better idea then using parsing String to double and after that sort arrays.

    one more thing do you have same examples how to set up a Vector.

    Thank's

  14. #14
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    are you success to get String "9.8" from String "9.8,",
    if yes, which coding used to parse String "9.8" to double 9.8?
    can you show me the code?


    set two arrays to one???

    can you get more detail about using Vector in the program?

    Java Code:
    		Vector<String> tmp = new Vector<String>();
    		tmp.add("Tom");
    		tmp.add("Young");
    		
    		Vector<Double> tmp2 = new Vector<Double>();
    		tmp2.add(15D);
    		tmp2.add(16.1);
    		
    		for(int i = 0; i < tmp.size(); i++)
    		{
    			System.out.println(tmp.elementAt(i));
    			
    		}
    sort method in Collections Class
    Java Code:
    Collections.sort(tmp);
    Last edited by mtyoung; 03-16-2009 at 02:36 AM.

  15. #15
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    I'm trying to use - Double.parseDouble

  16. #16
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Java Code:
    String[] score = {s1,s2,s3,s4,s5,s6,s7,s8};
     Arrays.sort(score);
     
     
    
      double[] t = new double[score.length];
      for(int i=0; i<score.length; i++)
      {
         
    	t [] = Double.parseDouble(score);
      }
    I' trying to convert String [] score to double, but it' going mistake in the last row ?????

  17. #17
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Java Code:
    String[] score = { s1, s2, s3, s4, s5, s6, s7, s8 };
    			Arrays.sort(score);
    
    			double[] t = new double[score.length];
    			for (int i = 0; i < score.length; i++) {
    
    				t[i] = Double.parseDouble(score[i]);
    			}
    
    			double sum = t[6] + t[5] + t[4] + t[3] + t[2] + t[1];
    I did it!!

    Thanks for the HELP!!!

  18. #18
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    parseDouble intake String as input, not String[]

    something like that
    Java Code:
    someDoubleArray[0][0] = Double.parseDouble("9.8,".replace(",", ""));
    then
    Java Code:
     Arrays.sort(someDoubleArray[0]);

  19. #19
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Thank you mtyoung that's helps a lot. Thanks for your help!!!

Similar Threads

  1. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 09:07 PM
  2. Problem in reading HTML input field while uploading file
    By sudipanand in forum Java Servlet
    Replies: 1
    Last Post: 11-27-2008, 09:26 AM
  3. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 01:23 AM
  4. Algorithms and data from a File Problem
    By BHCluster in forum Advanced Java
    Replies: 0
    Last Post: 04-18-2008, 04:38 PM
  5. Problem with Sequential File and Arrays
    By rhivka in forum New To Java
    Replies: 8
    Last Post: 07-30-2007, 09:03 PM

Posting Permissions

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