Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
A lil assistance please? Reading from a file to an array Posted by iceq321 on 4 Mar 2005 at 12:15 AM
Here is my code.....I dont know quite what's wrong with it....i cant seem to get it to read the numbers into the array. Any help/tips would be great!. Thanks!

import java.io.*;
public class fileprocessing {

public static void main(String[] args) throws IOException {
int size=10;
int number;
int target=0;
int[] array = new int[size];
File inputFile = new File("numbers.rtf");
FileReader text = new FileReader(inputFile);
File outputFile = new File("cis326.rtf");
FileWriter out = new FileWriter(outputFile);
for (int i=0; i<size; i++)
{
text.read();
number = array[i];
}
System.out.println(array[5]);
search(array, target);

}

static public int search(int [] array, int target)
{
int high = array.length, low = -1, probe;
while (high - low > 1)
{
probe = (high + low) / 2;
if (array[probe] < target)
low = probe;
else
high = probe;
}
if (high == array.length || array[high] != target)
return -1;
else
return high;
}

}
Report
Re: A lil assistance please? Reading from a file to an array Posted by Vilanye on 4 Mar 2005 at 1:40 AM
for (int i=0; i<size; i++)
{
text.read();
number = array[i];
}

Is this supposed to read from a file and put it into an array?

.read() is inherited from input stream reader, it reads a single character and returns it as an int, -1 if there is nothing to read. Did you mean to use it like this:

number = text.read();

The next line you are trying to take what is in the array and pass it to number. The problem is that you haven't declared the size of the array, nor have you put anything into it.

What does the data in the file look like? Is it always a single digit integer on a single line? If not, you might want to wrap BufferedReader around that FileReader so you can easily get at each line.
Report
Re: A lil assistance please? Reading from a file to an array Posted by iceq321 on 4 Mar 2005 at 9:08 AM
This message was edited by iceq321 at 2005-3-4 9:9:41

: for (int i=0; i<size; i++)
: {
: text.read();
: number = array[i];
: }
:
: Is this supposed to read from a file and put it into an array?
:
: .read() is inherited from input stream reader, it reads a single character and returns it as an int, -1 if there is nothing to read. Did you mean to use it like this:
:
: number = text.read();
:
: The next line you are trying to take what is in the array and pass it to number. The problem is that you haven't declared the size of the array, nor have you put anything into it.
:
: What does the data in the file look like? Is it always a single digit integer on a single line? If not, you might want to wrap BufferedReader around that FileReader so you can easily get at each line.
:
>>Thank you very much for your help! I figured it out based on what you said. Do you know how to take in an integer from the user from the screen? How do you do that, not using a GUI, jsut basic input?

Report
Re: A lil assistance please? Reading from a file to an array Posted by Vilanye on 4 Mar 2005 at 1:01 PM
: This message was edited by iceq321 at 2005-3-4 9:9:41

: : for (int i=0; i<size; i++)
: : {
: : text.read();
: : number = array[i];
: : }
: :
: : Is this supposed to read from a file and put it into an array?
: :
: : .read() is inherited from input stream reader, it reads a single character and returns it as an int, -1 if there is nothing to read. Did you mean to use it like this:
: :
: : number = text.read();
: :
: : The next line you are trying to take what is in the array and pass it to number. The problem is that you haven't declared the size of the array, nor have you put anything into it.
: :
: : What does the data in the file look like? Is it always a single digit integer on a single line? If not, you might want to wrap BufferedReader around that FileReader so you can easily get at each line.
: :
: >>Thank you very much for your help! I figured it out based on what you said. Do you know how to take in an integer from the user from the screen? How do you do that, not using a GUI, jsut basic input?
:
:

This works:

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

Then nameof string = stdin.readLine(); to read the who line typed in from the command prompt. It does return a string, which is a good thing as it gives you a chance to validate input.

int val = Integer.parseInt(nameofstring);

If anything but an integer is typed in it will throw an exception which you can handle or ignore and let it crash the program.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.