Question: netbeans, java programming The input file contain several lines. The first line is an integer (size of array) that tells how many other lines(array elements)
netbeans, java programming
The input file contain several lines. The first line is an integer (size of array) that tells how many other lines(array elements) there are in the file. On the other lines are Strings representing animals.
Example input file:
16
a cow
a dog
a cat
a mouse
a duck
a pig
a donkey
a pony
a sheep
an owl
a hen
a bee
a lion
a crow
a parrot
a turkey
This example file starts with the integer 16, which tells us that there are 16 more lines in the file. The other four lines contain Strings.
without editing the file can you use scanner to print only the strings lines? like for example,
this is my code :
File animal = new File("animals.txt");
Scanner b = new Scanner(animal);
int size = b.nextInt();
String [] array = new String [size];
for(int i = 0; i < array.length;i++){
array[i] = d.nextLine();
}
for(int i = 0; i < size;i++){ System.out.println(array[i]); }
and i get this when i run it :
a cow a dog a cat a mouse a duck a pig a donkey a pony a sheep an owl a hen a bee a lion a crow a parrot
but i should get this:
a cow
a dog
a cat
a mouse
a duck
a pig
a donkey
a pony
a sheep
an owl
a hen
a bee
a lion
a crow
a parrot
a turkey
please help
thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
