Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007, 07:51 AM
Member
 
Join Date: Nov 2007
Posts: 2
ice22 is on a distinguished road
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];

}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-12-2007, 05:37 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
As far as I can tell, you aren't putting anything in the array.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 01:39 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
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 ints 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
Code:
25 13 77 19 42 16 12 11 9 15 42 88
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-13-2007, 04:06 AM
Member
 
Join Date: Nov 2007
Posts: 2
ice22 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling trouble capacitator CLDC and MIDP 4 06-10-2008 11:12 PM
[SOLVED] trouble learnig swing monir6464 AWT / Swing 5 05-08-2008 07:01 AM
Compile Trouble adelgado0723 New To Java 5 04-21-2008 03:02 AM
trouble with program jimJohnson New To Java 1 04-03-2008 10:29 AM
JTree trouble Alantie Vala AWT / Swing 3 08-01-2007 12:12 AM


All times are GMT +3. The time now is 09:34 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org