-1

These are the instructions for my program:

The first line on this file will hold a number indicating how many lines will follow. Every line after that will have a word and an integer on it. Fill up two arrays, one for the word and one for the integer. Once the arrays are filled, go to each element of the String array and print out that String the number of times the parallel number array says for it to print.

However, when I try to print out the answer, an error occurs, saying there's no line to pick up when reading from the file I'm supposed to.

    Scanner input = new Scanner(new File("parallel.txt"));
    int numLine = input.nextInt();

    int [] array = new int[numLine];
    String [] Stringarray = new String[numLine];

    for (int i = 0; i< numLine; i++)
        {   
            Stringarray[i] = input.next();
            array[i] = input.nextInt(); 
            input.nextLine();
        }   

    for (int i = 0; i<Stringarray.length; i++)
        {
            for (int a = 0; a<array[a]; a++)
                out.print(Stringarray[i] + " ");
            out.println();
        }

}

}

Paul Bae
  • 1
  • 1
  • 1
    A sample of the input would help. – Sid Dec 08 '17 at 02:12
  • 1
    I thought this might have been a duplicate of https://stackoverflow.com/q/23036062 but as I look more closely, I see it probably isn't. If you want help with this, you'd really better tell us exactly what the error says, as well as showing us the input. – Dawood ibn Kareem Dec 08 '17 at 02:31
  • Define 'an error occurs, saying there's no line to pick up'. – user207421 Dec 08 '17 at 05:03

2 Answers2

0

In the first part of your code, you read the first integer in the file, and but you fail to read the newline character. You then attempt to read the first token from the second line with Stringarray[i] = input.next() but your scanner is still waiting on the first line and there is no next. You can fix that like so:

int numLine = input.nextInt();
input.nextLine();

I prefer to read an entire line at once, and then parse it separately, it's much cleaner. For example, and without any error checking:

String line = input.nextLine();
int number = Integer.parseInt(line); // parse the entire line as an integer

String line = input.nextLine();
String[] parts = line.split(" "); // split the line on spaces
String word = parts[0]; // treat the first part as a string
int number = Integer.parseInt(parts[1]); // parse the second part as an integer

In the second part of your code, to print out the word X times you need to index the numbers array with the same index as the word array.

for (int i = 0; i < Stringarray.length; i++) // for each word in the Stringarray
{
    for (int a = 0; a < array[i]; a++) // index with i, not a
        System.out.print(Stringarray[i] + " "); // print the word
    System.out.println();
}
Matt
  • 3,677
  • 1
  • 14
  • 24
0

You want to print the String array element corresponding to the integer in the file

so you have an input file like below

3 //indicate the line number
string1 6
string2 7
string3 5

and you are expecting an output like below

string1 string1 string1 string1 string1 string1 
string2 string2 string2 string2 string2 string2 string2 
string3 string3 string3 string3 string3 

I don't want to change your logic, you can use this for getting the output

for (int a = 0; a<array.length; a++)
 for(int k=0;k<array[a];k++)
  System.out.print(Stringarray[a] + " ");
 System.out.println()

Stringarray[a]-> here we wont get any Arrayindexbound exception as the length of Stringarray and array are always same

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39