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

    Default Problem - input in two-dimensional array

    Hi
    I need to make input from a file in two-dimensional array. The file is like this(Joe Lapland 9.8, 9.2, 9.3, 9.4, 9.7, 9.5, 9.1, 9.9)

    I know that a have to do two arrays for it.
    so far I did this:

    package chapter09;

    import java.io.*;
    import java.util.*;

    public class CH {


    Scanner myScanner = new Scanner (("src\\CHO9EX04data.txt"));

    String inp = myScanner.nextLine();

    Scanner a2 = new Scanner (inp);

    String names [][] = new String [25][2];
    int ncr;
    int lnr;{


    while (myScanner.hasNext());{


    names [0][lnr] = a2.next();
    names [1][lnr] = a2.next();
    names [2][lnr] = a2.next();
    names [3][lnr] = a2.next();
    names [4][lnr] = a2.next();
    names [5][lnr] = a2.next();
    names [6][lnr] = a2.next();
    names [7][lnr] = a2.next();
    names [8][lnr] = a2.next();
    names [9][lnr] = a2.next();
    names [10][lnr] = a2.next();
    names [11][lnr] = a2.next();
    names [12][lnr] = a2.next();
    names [13][lnr] = a2.next();
    names [14][lnr] = a2.next();
    names [15][lnr] = a2.next();
    names [16][lnr] = a2.next();
    names [17][lnr] = a2.next();
    names [18][lnr] = a2.next();
    names [19][lnr] = a2.next();
    names [20][lnr] = a2.next();
    names [21][lnr] = a2.next();
    names [22][lnr] = a2.next();
    names [23][lnr] = a2.next();
    names [24][lnr] = a2.next();


    names [ncr][0] = a2.next();
    names [ncr][1]= a2.next();
    names [ncr][2]= a2.next();
    names [ncr][3]= a2.next();
    names [ncr][4]= a2.next();
    names [ncr][5]= a2.next();
    names [ncr][6]= a2.next();
    names [ncr][7]= a2.next();
    names [ncr][8]= a2.next();
    names [ncr][9]= a2.next();
    names [ncr][10]= a2.next();
    names [ncr][11]= a2.next();
    names [ncr][12]= a2.next();
    names [ncr][13]= a2.next();
    names [ncr][14]= a2.next();
    names [ncr][15]= a2.next();
    names [ncr][16]= a2.next();
    names [ncr][17]= a2.next();
    names [ncr][18]= a2.next();
    names [ncr][19]= a2.next();
    names [ncr][20]= a2.next();
    names [ncr][21]= a2.next();
    names [ncr][22]= a2.next();
    names [ncr][23]= a2.next();
    names [ncr][24]= a2.next();


    }
    }



    public static void main (String[] args){

    printName(names);

    }

    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("%7d", names[row][col]);

    System.out.println();
    }
    }

    }


    Can you tell me where is my mistakes and what I'm doing wrong. I know that I have to make one more array for the numbers, but I'm trying to set up first this with the names. I need to read 25 persons.
    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    sorry, but I have to say that that is some incredibly ugly code. Let's see if we can improve on this some.

    Please give a little more information on what the text file looks like including a few lines of the file, what its data represents, and what you are supposed to do with it.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

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

    Default

    Sorry about the code but i'm new in the forum.

    The assignment is :

    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.
    Pall Paulin 9.8, 9.2, 9.3, 9.4, 9.7, 9.5, 9.1, 9.9 total scores 56.9

    The input file look like this:

    Larry Bard 9.1, 9.2, 9.6, 9.5, 9.8, 9.6, 9.2, 9.3
    Emil Goev 9.7, 9.2, 9.4, 9.3, 9.8, 9.8, 9.1, 9.1
    Vanko Vrah 9.8, 9.2, 9.4, 9.5, 9.7, 9.6, 9.1, 9.2
    Scot Turkov 9.9, 9.0, 9.1, 9.5, 9.8, 9.6, 9.1, 9.2
    Joe Lapland 9.8, 9.2, 9.3, 9.4, 9.7, 9.5, 9.1, 9.9

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, you could use a 2-dimensional array here, it could work. A better way would be to create a Contestant class to hold both first and last name Strings and an int array for the data, but you may not have the liberty to do this.

    I'm guessing here, but probably what the instructor wants you to do is to create a Vector<String[]> for the names and a Vector<int[]> for the scores.

    The key when using arrays and vectors is not to hard-code the data acquisition as you have done in your first post as it makes your code very inflexible. Better is to use your scanner object to read in 2 Strings into a String[2] array and place it into the first Vector, and then use the Scanner object to read in the ints, place them in an int array, and place that int array into the second Vector.

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

    Default

    Thanks for a tip. The thing is that I now what a have to do, but it's hard to me to express my self in Java.
    I'm trying but noting is going on.
    Thanks again Fubarable.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    well keep at it; don't give up! With perseverance, you will succeed.

Similar Threads

  1. assign a value to two dimensional array
    By makpandian in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 06:21 AM
  2. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 08:21 PM
  3. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 04:34 PM
  4. How to initialize a two dimensional Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:48 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:00 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
  •