Need java help on reading intger elements from file to array list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • needhelp123
    New Member
    • May 2007
    • 21

    Need java help on reading intger elements from file to array list

    Hi all,

    any body could help in reading integer values from .txt to array list and after transferring the dates from array list to another .txt file

    there should be two files....
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    Originally posted by needhelp123
    Hi all,

    any body could help in reading integer values from .txt to array list and after transferring the dates from array list to another .txt file

    there should be two files....

    Sure, with what part of the assignment are you having trouble with?

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Originally posted by needhelp123
      Hi all,

      any body could help in reading integer values from .txt to array list and after transferring the dates from array list to another .txt file

      there should be two files....
      Question you need to ask yourself
      How do you want to retreive your text files. by a scanner or static.
      how do you want to output your t.txt file.
      How many data types do you need.
      Where do you want to store the output txt files
      nomad

      Comment

      • needhelp123
        New Member
        • May 2007
        • 21

        #4
        Originally posted by prometheuzz
        Sure, with what part of the assignment are you having trouble with?

        the following information is enought i hope...
        * Create a file called Unsorted.txt.
        * Store the Unsorted values in this file.
        * Read the values from the Unsorted.txt file.
        * Sort the values
        * Store the sorted values in new file. Name it as Sorted.txt

        Implement this only for the following algorithms.

        Bubble sort

        Comment

        • needhelp123
          New Member
          • May 2007
          • 21

          #5
          Originally posted by nomad
          Question you need to ask yourself
          How do you want to retreive your text files. by a scanner or static.
          how do you want to output your t.txt file.
          How many data types do you need.
          Where do you want to store the output txt files
          nomad
          I hope following information is enough....

          * Create a file called Unsorted.txt.
          * Store the Unsorted values in this file.
          * Read the values from the Unsorted.txt file.
          * Sort the values
          * Store the sorted values in new file. Name it as Sorted.txt

          Implement this only for the following algorithms.

          Bubble sort

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by needhelp123
            I hope following information is enough....

            * Create a file called Unsorted.txt.
            * Store the Unsorted values in this file.
            * Read the values from the Unsorted.txt file.
            * Sort the values
            * Store the sorted values in new file. Name it as Sorted.txt

            Implement this only for the following algorithms.

            Bubble sort
            Have you read how to I/O files yet and do you know how to create a Arraylist?

            nomad

            Comment

            • prometheuzz
              Recognized Expert New Member
              • Apr 2007
              • 197

              #7
              Originally posted by needhelp123
              the following information is enought i hope...
              * Create a file called Unsorted.txt.
              * Store the Unsorted values in this file.
              * Read the values from the Unsorted.txt file.
              * Sort the values
              * Store the sorted values in new file. Name it as Sorted.txt

              Implement this only for the following algorithms.

              Bubble sort
              What I meant to say with my post is this: if you don't have any idea where (or how) to start coding *something*, then I cannot help you and you should either hire a private tutor or have a talk with your teacher (I don't mean to put you down!).

              But, if you have coded something up and you're getting compiler/runtime errors you don't understand what they mean: post your code AND errors here and explain what it is you're having a hard time with: I'll try to help you.

              Good luck.

              Comment

              • nomad
                Recognized Expert Contributor
                • Mar 2007
                • 664

                #8
                Originally posted by needhelp123
                I hope following information is enough....

                * Create a file called Unsorted.txt.
                * Store the Unsorted values in this file.
                * Read the values from the Unsorted.txt file.
                * Sort the values
                * Store the sorted values in new file. Name it as Sorted.txt

                Implement this only for the following algorithms.

                Bubble sort
                like prometheuzz we can not help you without see some form of coding.
                I'm also new to Java and I willing to help you learn Java from an entry level view points

                good luck

                Comment

                • needhelp123
                  New Member
                  • May 2007
                  • 21

                  #9
                  Originally posted by nomad
                  Have you read how to I/O files yet and do you know how to create a Arraylist?

                  nomad
                  i know all the things....
                  to read and create array list
                  and even i have the program tooo

                  i need a code where i can transfer data from text to array list....
                  i am not aware of this little bit...

                  if i kno this so i can implement the rest...

                  Comment

                  • needhelp123
                    New Member
                    • May 2007
                    • 21

                    #10
                    Originally posted by prometheuzz
                    What I meant to say with my post is this: if you don't have any idea where (or how) to start coding *something*, then I cannot help you and you should either hire a private tutor or have a talk with your teacher (I don't mean to put you down!).

                    But, if you have coded something up and you're getting compiler/runtime errors you don't understand what they mean: post your code AND errors here and explain what it is you're having a hard time with: I'll try to help you.

                    Good luck.

                    hi prometheuzz
                    thanks for your concern... i have the program code just i need to know about how to transfer integer data from text file to array list...

                    Comment

                    • nomad
                      Recognized Expert Contributor
                      • Mar 2007
                      • 664

                      #11
                      Originally posted by needhelp123
                      i know all the things....
                      to read and create array list
                      and even i have the program tooo

                      i need a code where i can transfer data from text to array list....
                      i am not aware of this little bit...

                      if i kno this so i can implement the rest...
                      if that is the case then you need to use the get()method and set() methods for your data types.

                      nomad

                      Comment

                      • prometheuzz
                        Recognized Expert New Member
                        • Apr 2007
                        • 197

                        #12
                        Originally posted by needhelp123
                        hi prometheuzz
                        thanks for your concern... i have the program code just i need to know about how to transfer integer data from text file to array list...

                        Have a look at Sun's I/O tutorial:
                        This Java tutorial describes exceptions, basic input/output, concurrency, regular expressions, and the platform environment

                        Comment

                        • needhelp123
                          New Member
                          • May 2007
                          • 21

                          #13
                          Originally posted by nomad
                          if that is the case then you need to use the get()method and set() methods for your data types.

                          nomad
                          this is my java program


                          [CODE=java] public class BubbleSort {

                          int[] bsArrays = new int[5];
                          int i; //indexing for array(outer loop counter)
                          int j; //indexing for array*(inner loop counter)


                          /**
                          * Adds element into an array
                          * sorts the array and print final value...
                          */
                          private void setBubblesort() {

                          // Insert elements into an array
                          bsArrays[0] = 20;
                          bsArrays[1] = 10;
                          bsArrays[2] = 50;
                          bsArrays[3] = 100;
                          bsArrays[4] = 05;

                          int temp = 0; //variable to store value during manipulation

                          System.out.prin tln("Bubble Sort");
                          System.out.prin tln(" ");
                          System.out.prin tln(" ");
                          System.out.prin tln("Data before Soring ");
                          System.out.prin tln("********** ****** ");
                          for (int val = 0; val < 5; val ++) {
                          System.out.prin tln("Value at index" + val + " = " + bsArrays[val]);
                          }

                          //swaping an elemens
                          for (i = 0; i < 4; i++ ) {
                          for (j = i + 1; j < 5; j ++) {
                          if (bsArrays[i] > bsArrays[j]) {
                          temp = bsArrays [ i ];
                          bsArrays[i] = bsArrays[j];
                          bsArrays[j] = temp;
                          }
                          }
                          }

                          //output: final sorted elements in ana array
                          System.out.prin tln(" ");
                          System.out.prin tln(" ");
                          System.out.prin tln(" After Sorting " );
                          System.out.prin tln(" *************** * ");
                          for (int fval = 0; fval < 5; fval ++) {
                          System.out.prin tln("Value at index" + fval + " = " +
                          bsArrays[fval]);
                          }
                          }

                          /**
                          * The main method illustrates the use of a Bubble sort
                          * for small array
                          */
                          public static void main(String str[]) {
                          BubbleSort acc = new BubbleSort();
                          acc.setBubbleso rt();
                          }
                          }
                          [/CODE]

                          here i have inserted elements directly in the program.. in spite of that i need to get those elements from text file what i mentioned before...

                          Comment

                          • needhelp123
                            New Member
                            • May 2007
                            • 21

                            #14
                            Originally posted by prometheuzz

                            thank you...
                            it might help in implenting code if my TL... sujst to use scanner

                            Comment

                            • prometheuzz
                              Recognized Expert New Member
                              • Apr 2007
                              • 197

                              #15
                              Originally posted by needhelp123
                              thank you...
                              it might help in implenting code if my TL... sujst to use scanner
                              You can or can't use the Scanner class?

                              Anyway, Scanner examples can be found in the tutorial I posted, and here's an example of how to do it without the Scanner class:
                              [CODE=java]import java.io.*;

                              public class Main {
                              public static void main(String[] args) throws IOException {
                              BufferedReader buffer =
                              new BufferedReader( new FileReader("Mai n.java"));
                              String lineOfText;
                              while((lineOfTe xt = buffer.readLine ()) != null) {
                              System.out.prin tln("> "+lineOfTex t);
                              }
                              buffer.close();
                              }
                              }[/CODE]
                              Have a look at the java.lang.Integ er class how to parse a String to a numerical value.

                              Good luck.

                              Comment

                              Working...