Results 1 to 4 of 4
  1. #1
    ice22 is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default Having trouble with array

    Hi im new to java and I am having a hard time trying to figure out why the input file is initialized into the array. When the array prints I simply get 26zeroes. Any help would be greatly appreciated.

    Thank you.

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

    public class me
    {
    static final int ARRAY_SIZE=26;
    static Scanner console=new Scanner(System.in);

    public static void main(String[]args)
    throws FileNotFoundException, IOException

    {

    int []list=new int[26];
    int counter=0;
    String s="ABCD";
    char A=s.charAt(0);
    int i=(int)A;


    Scanner inputStream=new Scanner(new FileReader("MessageIn.txt"));

    for(i=0;i<26;i++)
    System.out.print(list[i]+"");
    int maxIndex;
    int largestList;


    maxIndex=0;
    for(i=1;i<26;i++)
    if(list[maxIndex]<list[i])
    maxIndex=i;

    largestList=list[maxIndex];

    }

    }

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    As far as I can tell, you aren't putting anything in the array.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class FillArray
    {
        public static void main(String[] args) throws FileNotFoundException,
                                                      IOException
        {
            String filePath = "fillArray.txt";
            // Count lines in file.
            Scanner scanner = new Scanner(new File(filePath));
            int lineCount = 0;
            while(scanner.hasNext()) {
                scanner.nextLine();
                lineCount++;
            }
            scanner.close();
            System.out.println(filePath + " has " + lineCount + " lines");
    
            // Initialize and fill an array with file data.
            int[] list = new int[lineCount];
            Scanner reader = new Scanner(new FileReader(filePath));
            for(int i = 0; i < list.length; i++) {
                String line = reader.nextLine();
                list[i] = Integer.parseInt(line);
            }
            reader.close();
            System.out.println("original list = " + Arrays.toString(list));
    
            // Sort the array of [i]int[/i]s from high to low value.
            int maxIndex;
            int largestList = -Integer.MAX_VALUE;
            for(int i = 0; i < list.length; i++) {
                largestList = list[i];
                maxIndex = i;
                for(int j = i+1; j < list.length; j++) {
                    if(list[j] > largestList) {
                        maxIndex = j;
                        largestList = list[j];
                    }
                }
                if(maxIndex != i) {
                    int temp = list[i];
                    list[i] = largestList;
                    list[maxIndex] = temp;
                }
            }
            System.out.println("sorted list   = " + Arrays.toString(list));
        }
    }
    fillArray.txt
    Java Code:
    25
    13
    77
    19
    42
    16
    12
    11
    9
    15
    42
    88

  4. #4
    ice22 is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    Thank you so much. You have no idea how helpful you have been. the book I am using in class is nowhere near as good as this in showing how to do this. I doesnt even explain how to initialize an array from a file.

    Thank you again
    Robert

Similar Threads

  1. compiling trouble
    By capacitator in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-10-2008, 10:12 PM
  2. [SOLVED] trouble learnig swing
    By monir6464 in forum AWT / Swing
    Replies: 5
    Last Post: 05-08-2008, 06:01 AM
  3. Compile Trouble
    By adelgado0723 in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 02:02 AM
  4. trouble with program
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-03-2008, 09:29 AM
  5. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 07-31-2007, 11:12 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
  •