Skip to content
Advertisement

Way to scan contents of a singular array index?

There is an array in my code that includes the names to random items delimited by a /n (i think). the splitLines[] array is an organizational method that collects strings and integers separated by a delimiter in a file. The file is formatted as

<<Prize’s Name 0>>t<<Prize’s Price 0>>n <<Prize’s Name 1>>t<<Prize’s Price 1>>n

My goal is to assign each line in the contents of splitLines[0] and splitLines[1] to its own index in separate arrays. The splitLines[0] array is formatted as

<<Prize’s Name 0>>/n <<Prize’s Name 1>>/n

and the splitLines[1] array is formatted as

<<Prize’s Price 0>>/n <<Prize’s Price 1>>/n

The process here is messy and convoluted, but as I am still learning the inner workings of arrays (and java as a language), I have yet to find a way that successfully reads through the array index and picks out each and every word and assigns it to another array. So far I have tried setting up a Scanner that takes splitLines[] as a parameter, but I am unsure whether fileScanner.next{Int,Line,Double, etc.}() is capable of reading into the array index at all. I am unsure how to proceed from here. Here is the block that I have so far

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.Math;
public class DrewCarey {


    public static void main(String[] args) throws FileNotFoundException {
        {
            int min = 0;
            int max = 52;

            int randomIndex = (int)Math.floor(Math.random()*(max-min+1)+min);

            String[] aPrize = new String[53];
            int[] aPrice = new int[53];

            final String DELIM = "t";

            Scanner fileScanner = new Scanner(new File("PrizeFile.txt"));
            String fileLine = fileScanner.nextLine();
            String[] splitLines = fileLine.split(DELIM);

            String temp = "drew";
            while(fileScanner.hasNextLine())
            {
               for(int i=0;i<aPrize.length;i++)
                {
                    fileLine = fileScanner.nextLine();
                    splitLines = fileLine.split(DELIM);

                    if(fileLine.split(DELIM) != splitLines)
                    {
                        // String name = splitLines[0];
                    // int price = Integer.parseInt(splitLines[1]);

                     //splitLines[0] = aPrize[i];
                    // price = aPrice[i];

                     System.out.println(splitLines[0]);

                   //  splitLines[0] = temp;
                    // splitLines[1] = temp;


                 }



               }

            }
            fileScanner.close();


        } ```

Advertisement

Answer

Your file/data is formatted in a very strange way which will cause all manner of issues, also are you splitting with “n” or “/n” it is conflicting in your question. You should NOT use “n” for the split because it is confused with an actual JAVA newline chracter. So assuming the file data is a single line that looks like this with “/n” and “/t”:

<<Prize’s Name 0>>/t<<Prize’s Price 0>>/n <<Prize’s Name 1>>/t<<Prize’s Price 1>>/n <<Prize’s Name 2>>/t<<Prize’s Price 2>>/n <<Prize’s Name 3>>/t<<Prize’s Price 3>>/n <<Prize’s Name 4>>/t<<Prize’s Price 4>>

Then you can correctly split the first line of the file as shown below. The biggest problem in your code is that you only split with the “t” DELIM, never with the “n” delim. The below code solves this problem by splitting with “/n” first, then we split the resulting line with the “/t” and simply assign each part of the split to the aPrize and aPrice array.

//Add "n" delim
final String DELIM_N = "/n ";
final String DELIM_T = "/t";

//We will use two string arrays in this demo for simplicity
String[] aPrize = new String[53];
String[] aPrice = new String[53];

Scanner fileScanner = new Scanner(new File("PrizeFile.txt"));

//Get first line
String fileLine = fileScanner.nextLine();

//Split line with "/n"
String[] splitLines = fileLine.split(DELIM_N);

//loop through array of split lines and save them in the Prize and Price array
for (int i = 0; i < splitLines.length; i++)
{
    //Split each itom with "/t"
    String[] splitItems = splitLines[i].split(DELIM_T);
    //Check that each line does not have unexpected items
    if (splitItems.length > 2)
    {
        System.out.println("Unexpected items found");
    }
    else
    {
        //Insert your code here to clean the input and remove the << and >> around items and parse them as an int etc.

        //Assign the items to the array
        //index 0 is prize
        aPrize[i] = splitItems[0];
        //and index 1 is price
        aPrice[i] = splitItems[1];
    }
}

//Complete. Print out the result with a loop
System.out.println("File read complete, Data split into two arrays:");
for (int i = 0; i < 10; i++)
{
    System.out.println("Prize at index "+ i +" is: " + aPrize[i]);
    System.out.println("Price at index "+ i +" is: " + aPrice[i]);
}

The output is as follows:

File read complete, Data split into two arrays:
Prize at index 0 is: <<Prize’s Name 0>>
Price at index 0 is: <<Prize’s Price 0>>
Prize at index 1 is: <<Prize’s Name 1>>
Price at index 1 is: <<Prize’s Price 1>>
...
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement