Friday, 15 January 2010

java - Adding data from .txt document to array -


What does the text document look like? The first line is the number of elements I want to include in the array. The ID for the second product, which is different from # and the third line is the total value of the product which is once again divided by #

  10 PA / 1234 # PV / 5732 #U / 9271 is # DT / 9489 # HI / 7195 # Jedar / 7413 # BT / 4674 # LR / 4992 # XK / 8536 # KD / 9767 # 153 # 25 # 172 # 95 # 235 # 15 9 # 725 # 629 # 112 # 559 #  

I want to use the following method to pass the inputfile to readProductDataFile method:

  public stable products [readProductDataFile ( I want to make an array of size 10, or maybe an array, preferably the combination and value of the customer ID, such as Array [1] = PA / 1234_153   

There you go to the whole class, which in fact you want: < Pre> import java.io.buffferedReader; Import java.io.fileReader; Import java.util.Arrays; Import java.io.phileNotFoundException; Import java.io.IOException; Class myRead {public static void main (String [] AGR) file Notfound Akspesheshn, Aioi Akspesheshn {Bfredderr input file = new Bfrated Reader (New File Reader ( "Test. Test")); String numberofflements = inputfile Readline (); // This is the first line in which the number "10" / system OutprintLine (numberoffinals) is included; String second = inputfile Readline (); // This is the second line in which your data is included, split this "#" into the delimiter string [strrarray = secondLine.split ("#"); //System.out.println(Arrays.toString (strArray)); //System.out.println(strArray[0]); String third line = inputfile Readline (); // This is the third line in which your data is included; divide it as "#" into a delimiter string [] data array = third line split ("#"); // Combine arrays string [] Combined = New string [strarray.length]; For (int i = 0; i & lt; strArray.length; i ++) {combinedArray [i] = strArray [i] + "_" + data extra [i]; Println (combinedArray [i]); }}}

Output:

  PA / L234_l53 PV / 5732_25 Au / 927l_l72 DT / 948 9_ 9 5 9 HY / 7l95_235 JR / 74l3_l59 BT / 4674_725 LR / 49 9 2 9 629 XK / 8536_ll2 KD / 9767_559  

I read the driver there who have been readLine Using BufferedReader using # to divide each token in the form of a delimiter, each of the three rows, partition ("#"); and create an array, and combined [i] = StrArray [i] + "_" + dataar [i]; to put elements in a composite array as you want ...!

  public static product [] readProductDataFile (file inputfile) {BufferedReader inputFile = new BufferedReader (new FileReader (inputFile)); // The rest of my previous code goes here  

Edit: With the file as an input method, everything from inside the main to a different method is called!

  Import java.io.buffferedReader; Import java.io.fileReader; Import java.util.Arrays; Import java.io.phileNotFoundException; Import java.io.IOException; Import java.io.file; Class myRead {Public static zero main (string [] args) FileNotFoundException throws, IOException {file myFile = new file ("test.txt"); ReadProductDataFile (myFile); } Public static string [] readProductDataFile (file input file) throws Failnotfound Akspshn, IOException {BufferedReader myReader = new BufferedReader (new FileReader ( "test.txt")); String numberofflements = myReader.readLine (); // This is the first line in which the number "10" / system OutprintLine (numberoffinals) is included; String second = myReader.readLine (); // This is the second line in which your data is included, split this "#" into the delimiter string [strrarray = secondLine.split ("#"); //System.out.println(Arrays.toString (strArray)); //System.out.println(strArray[0]); String third line = myReader.readLine (); // This is the third line in which your data is included; divide it as "#" into a delimiter string [] data array = third line split ("#"); // Combine arrays string [] Combined = New string [strarray.length]; For (int i = 0; i & lt; strArray.length; i ++) {combinedArray [i] = strArray [i] + "_" + data extra [i]; Println (combinedArray [i]); Return return; }}  

OUTPUT

  PA / L234_l53 PV / 5732_25 Au / 927l_l72 DT / 9489_95 HY / 7l95_235 Jedar / 74l3_l59 BT / 4674_725 LR / 4992_629 XK / 8536_112KD / 9767_559  

No comments:

Post a Comment