A lil assistance please? Reading from a file to an array - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

A lil assistance please? Reading from a file to an array

iceq321iceq321 Posts: 5Member
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 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;
}

}

Comments

  • VilanyeVilanye Posts: 684Member
    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.
  • iceq321iceq321 Posts: 5Member
    [b][red]This message was edited by iceq321 at 2005-3-4 9:9:41[/red][/b][hr]
    : for (int i=0; i>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?

  • VilanyeVilanye Posts: 684Member
    : [b][red]This message was edited by iceq321 at 2005-3-4 9:9:41[/red][/b][hr]
    : : for (int i=0; i>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.
Sign In or Register to comment.