Hi I have a program that I am to write that is suppose to read from a text file. I am suppose to assume no more than 100 item and terminate after by -1. have to read and store the data. I have to write a method to print the array without showing the null elements, and I need to add a method that will search the string in the array if it can't be find i need to write an error message if the search occurred more than one i am to return an array to show all the corresponding array indexes of the string location. I am also suppose to invoke the print array method. then i have to search the array for a item in it.

here is the txt file here
Able
Baker
Charlie
Dog
Easy
Fox
George
How
Int
Jig
King
Love
Mike
Negative
Option
Peter
Jig
Queen
Roger
Sugar
Tare
Uncle
Victor
William
X-ray
Jig
Yolk
Zebra
<<EOD>>
There are 28 data items

This is my code so far

public static void main(String[] args) {


        Scanner inFile = new Scanner(new File (""));
        //String array  declared created and 
         String[] sArray;
         sArray = new String[100];

         String searchValue = "", index;
         Scanner inFile = new Scanner (new File(""));
         for (int i = 0; i < sArray.length; i++){
             sArray[i] = inFile.next();
         }
         System.out.println("Enter a)
         index = binarySearch(sArray, searchValue);
         if(index != -1) {
            System.out.println("Found at index: " + index);
         }
         else {
             System.out.println("Not Found")
         }

Recommended Answers

All 11 Replies

You seem to be getting some reasonable code written there, so what's your question?

Which part of the project are you having problems with? Pick one part and ask questions about your problems. Then work on it, get it to compile, execute and test OK and move to the next part.

First thing I see is that the code is not in a class and won't compile without errors. Put the code into a class with the necessary import statements and compile it. Copy and paste the full text of the error messages here that you are having problems with.

I am not having problems with the class I know how to write a class. I am having trouble with adding a method to search the array and searching the array to see how many time the word occurs at what index. so far I have this for it.

public class Array_2{

    /**
     * @param args
     */
    public static void main(String[] args) {


        Scanner inFile = new Scanner(new File (""));
        //String array  declared created and 
         String[] sArray;
         sArray = new String[100];

         String searchValue = "", index;
         Scanner inFile = new Scanner (new File(""));

         //print method to print the string
         public static void printArray(String[] sArray) {
             for(int i = 0; i < sArray.length; i++) {
                 System.out.print(sArray[i] + "");
             }

         //Count the occurrence
        String[] counts = countWords(String)
         index = binarySearch(sArray, searchValue);
         if(index != -1) {
             System.out.println("Found at index:" + " ");
         }
         else {
             System.out.println("Not Found")
         }

         //Invoking the print method
         printArray(new String[], "");

You really should be compiling this code as you go. You may need to add a couple of "stub" methods - ie with the right signature and if necessary returning a dummy value.
If you keep writing code without fixing the errors then when you finally do try to compile you will get an overwhelming cascade of messages and it will be very difficult to see where to start fixing them. Frequesnt compiling keeps you grounded. At your stage of learning I wouldn't write more than a dozen lines without running them through the compiler and fixing them as I go.
I could point out a couple of errors in your code, but there's no point when the compiler will do that anyway.

could you point in the right direction to fixing the errors i have.

can you also tell me how to input a txt file I am unsure of how to do it. I am using eclipse on mac

The code you had in your first post lines 10-13 is a good start. You just need to put a real file name in the new File instead of "". OR use a JFileChooser to allow the user to chose one in the usual way.

ps Eclipse will be flagging errors with a red jagged underline as you go, so fix them straight away, don't ignore them.

i have the "" because I don t know how to get the file to read the data.

You need the full name of the file, and if you don't know what the default directory is (who does?) the full path to the file. That's why using JFileChooser is usually the easier option.

For this exercise, just use the default path (filename in the space between the quotes). Then for Eclipse, look in your workspace. On windows 7, the default is {C:\Users\Star\workspace\ThisProject}

I would just play with the file I/O for now and just see if you can read the data. Maybe just use a simple loop to read it until there is nothing left.

Your IDE should prompt you to import some essential libraries as well for File etc.

Don't forget to close the file at the end of your main method

    // go through infile until no more data
    while (inFile.hasNext()) {
        String itemName = inFile.next();
        System.out.println(itemName);
    }
    inFile.close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.