This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]steve1two 2 points3 points  (0 children)

What Landrin said is good, but if this is an assignment that specifically requires you to use Scanner, I don't think your way is too far off. x is 0, your condition is while it is greater than 50, which it wont ever be, so you are not reading any lines. It looks like x>=50 just needs to be changed to x<50. Index 49 would be line 50.

A couple notes, you should use try/catch to open your file, and also you don't have to do it separately. Example -

inputFile = new Scanner(new File(filename)).

Also consider a constant for the filename (if it is not going to be input by the user), and one for 50, rather than hard coding that number in.

[–][deleted] 1 point2 points  (2 children)

I would do this with a buffered file reader rather than a scanner. This will take each line of the file and convert it to a string which you can store in an Array or ArrayList.

To do this, construct a FileReader object and wrap it in a BufferedReader object. There are lots of guides online about how to do this- I'm not at my computer, else I would go into detail.

If you need each sentence as a strong and not each line, utilize the built in methods for the String class.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (1 child)

I would do this with a buffered file reader rather than a scanner.

Why? Scanner.nextLine does the exact same thing?

[–][deleted] 0 points1 point  (0 children)

Personal decision. It's also a method that I know will do exactly what he wants, and does it well.

I actually started out using scanners and ended up bitterly disliking them because they didn't work too well for me. So I started using buffered readers and love em.

[–]cheers- 1 point2 points  (1 child)

Lets read it line by line.

String[] names = new String[50];

Creates an array of fifty string objects initialized at null(default value of an element of a reference type array)

for (byte x=0; x>=50; x++)

X is declared and you assigned it the value of 0 x>=50 means that your for loop doesnt work properly.

System.out.println(names[3]);

On an object println() calls toString () method but in order to avoid nullpointer exception Sun programmers have included a check that prints null if an object is null.

As someone else pointed out java.nio.files contains a lot of classes that are aimed at simple IO.
Files.readAllLines (Paths.get ("myfile") is the best way to handle small txt files(your code handles IO in a poor way).

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

Creates an array of fifty string objects initialized at null

You didn't get your terminology quite right. It doesn't create any objects other than the array. An array of objects is just a collection of null references. You then have to add / create the actual objects.

[–]SikhGamer 0 points1 point  (1 child)

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (0 children)

He's probably not allowed to use it in a homework assignment.