Please Please help on easy program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sexygal
    New Member
    • Nov 2006
    • 3

    #1

    Please Please help on easy program

    I need a program to do the following:

    1. Access a file where I would have a list of people each with a different number assigned to them.

    2. Ask the user to input one of the numbers

    3. return the name which is associated with that number

    I have eclipse, and know I need to do something with scanner and sort the array. But I have no idea what.

    Any help is appreciated.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sexygal
    I need a program to do the following:

    1. Access a file where I would have a list of people each with a different number assigned to them.

    2. Ask the user to input one of the numbers

    3. return the name which is associated with that number

    I have eclipse, and know I need to do something with scanner and sort the array. But I have no idea what.

    Any help is appreciated.
    What have you done so far?

    Comment

    • sexygal
      New Member
      • Nov 2006
      • 3

      #3
      Here is what I have so far. I am trying to get the file I have saved as a .txt file, but I seem lost and can't get it to see the file.
      import java.io.*;
      import javax.swing.*;

      public class FileManager {
      private static final String EMPTY_STRING="" ;
      private static String lineTerminator = System.getPrope rty("line.separ ator");

      public FileManager() {

      }

      public String openFile() throws FileNotFoundExc eption, IOException {
      String doc=EMPTY_STRIN G;

      JFileChooser chooser = new JFileChooser();
      int reply = chooser.showOpe nDialog(null);

      if (reply == JFileChooser.AP PROVE_OPTION){
      doc = openFile(choose r.getSelectedFi le().getAbsolut ePath());
      }
      return doc;
      }
      public String openFile(String fileName) throws FileNotFoundExc eption, IOException {
      String line;
      StringBuffer document = new StringBuffer(EM PTY_STRING);

      File inFile = new File(fileName);
      FileReader fileReader = new FileReader(inFi le);
      BufferedReader bufReader = new BufferedReader( fileReader);

      while (true){
      line = bufReader.readL ine();

      if (line == null)
      break;
      document.append (line + lineTerminator) ;
      }
      return document.toStri ng();
      }
      public void saveFile(String data) throws IOException {
      String fileName, doc=EMPTY_STRIN G;

      JFileChooser chooser = new JFileChooser();
      int reply = chooser.showSav eDialog(null);

      if (reply == JFileChooser.AP PROVE_OPTION){
      saveFile(choose r.getSelectedFi le().getAbsolut ePath(),data);
      }
      }
      public void saveFile(String fileName, String data) throws IOException {
      File outFile = new File(fileName);
      FileOutputStrea m outFileStream = new FileOutputStrea m(outFile);
      PrintWriter outStream = new PrintWriter(out FileStream);

      outStream.print (data);
      outStream.close ();
      }
      }


      import javax.swing.*;
      import java.io.*;
      public class UseFileManager {


      public static void main(String[] args)throws IOException {
      FileManager fm = new FileManager(); //associated file with fm

      String doc= "Hello World",doc2; //test string
      fm.saveFile(doc );
      //fm.saveFile("sa mple.txt",doc); //write to file
      doc2=fm.openFil e();
      doc2=fm.openFil e("Memberships. txt"); //read from file
      JOptionPane.sho wMessageDialog( null,doc2); //verify

      }

      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sexygal
        Here is what I have so far. I am trying to get the file I have saved as a .txt file, but I seem lost and can't get it to see the file.
        import java.io.*;
        import javax.swing.*;

        public class FileManager {
        private static final String EMPTY_STRING="" ;
        private static String lineTerminator = System.getPrope rty("line.separ ator");

        public FileManager() {

        }

        public String openFile() throws FileNotFoundExc eption, IOException {
        String doc=EMPTY_STRIN G;

        JFileChooser chooser = new JFileChooser();
        int reply = chooser.showOpe nDialog(null);

        if (reply == JFileChooser.AP PROVE_OPTION){
        doc = openFile(choose r.getSelectedFi le().getAbsolut ePath());
        }
        return doc;
        }
        public String openFile(String fileName) throws FileNotFoundExc eption, IOException {
        String line;
        StringBuffer document = new StringBuffer(EM PTY_STRING);

        File inFile = new File(fileName);
        FileReader fileReader = new FileReader(inFi le);
        BufferedReader bufReader = new BufferedReader( fileReader);

        while (true){
        line = bufReader.readL ine();

        if (line == null)
        break;
        document.append (line + lineTerminator) ;
        }
        return document.toStri ng();
        }
        public void saveFile(String data) throws IOException {
        String fileName, doc=EMPTY_STRIN G;

        JFileChooser chooser = new JFileChooser();
        int reply = chooser.showSav eDialog(null);

        if (reply == JFileChooser.AP PROVE_OPTION){
        saveFile(choose r.getSelectedFi le().getAbsolut ePath(),data);
        }
        }
        public void saveFile(String fileName, String data) throws IOException {
        File outFile = new File(fileName);
        FileOutputStrea m outFileStream = new FileOutputStrea m(outFile);
        PrintWriter outStream = new PrintWriter(out FileStream);

        outStream.print (data);
        outStream.close ();
        }
        }


        import javax.swing.*;
        import java.io.*;
        public class UseFileManager {


        public static void main(String[] args)throws IOException {
        FileManager fm = new FileManager(); //associated file with fm

        String doc= "Hello World",doc2; //test string
        fm.saveFile(doc );
        //fm.saveFile("sa mple.txt",doc); //write to file
        doc2=fm.openFil e();
        doc2=fm.openFil e("Memberships. txt"); //read from file
        JOptionPane.sho wMessageDialog( null,doc2); //verify

        }

        }
        I do not see where your requirements say you have to open the file. Is it really neccessary/required that you actually open the file?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          How to get the name associated with a number depends on the format of the file containing the names. How are the names and numbers arranged in the text file?

          Comment

          • sexygal
            New Member
            • Nov 2006
            • 3

            #6
            Originally posted by r035198x
            How to get the name associated with a number depends on the format of the file containing the names. How are the names and numbers arranged in the text file?
            I made the file in excel and saved it as a .txt document. I listed the numbers in the first column, and put the names in the second column. So the notepad has it saved with a number, a space, and then the name. Same thing on the next line, and so on. There are fifteen lines, so 15 numbers and 15 names.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by sexygal
              I made the file in excel and saved it as a .txt document. I listed the numbers in the first column, and put the names in the second column. So the notepad has it saved with a number, a space, and then the name. Same thing on the next line, and so on. There are fifteen lines, so 15 numbers and 15 names.
              Code:
               
              public String showName(int pos) throws FileNotFoundException, IOException {
              String line = "";
              
              File inFile = new File("Memberships.txt");
              FileReader fileReader = new FileReader(inFile);
              BufferedReader bufReader = new BufferedReader(fileReader);
              while ((line = bufReader.readLine()) != null){
              String[] text = line.split(" ");
              if(pos == Integer.parseInt(text[0])) {
               return text[i];
              }
              }
              return "Name not Found";
              }
              This is probably all you need to do.

              Comment

              Working...