1

I'm reading from a file and using a scanner to read the inputs but am having difficulty understanding how to apply next() and nextLine() to assign data from the text to variables to be sent to my rectangle class constructor for printing these objects to the console. I get parsing the input (nextInt...etc.), but when strings and primitive data mix on the same line on multiple lines of a scanner read text file how do you handle this? Do I create multiple scanner loops for ints and String(s) to loop through it all to ensure the read data gets assigned to the array elements correctly?

TLDR: How do I read multiple strings and primitives from one .txt file, populate that data into my class fields to then create an array of objects and print *'s to the console to symbolize my created "rectangular" objects from said .txt file?

  • It depends how your txt file looks like and what separator you use. Could you post the content of your file? – F. Fritz Jul 06 '17 at 23:32
  • 6 6 3 filled 3 6 unfilled 4 4 filled 6 6 unfilled 9 4 filled 4 8 unfilled – intheyear3000 Jul 06 '17 at 23:35
  • The first "6" are six objects to be created from the .txt file. After that, it's rows/columns/filled (a boolean). – intheyear3000 Jul 06 '17 at 23:36
  • If the boolean (filled=true) means I'm just printing *'s in an amount of rows/columns specified (unfilled=false) means its a hollow rectangle. 3 3 filled = 3 rows and colums of *'s printed to the console. Unfilled means the second row only has *'s to create a basic rectangle in lieu of a 3x3 * "block." – intheyear3000 Jul 06 '17 at 23:43
  • So, you'd like us to complete your homework? – Abhijit Sarkar Jul 07 '17 at 00:16
  • 1
    Please edit your question so that we can see nicely formatted code and input – Scary Wombat Jul 07 '17 at 00:42
  • Apologies if I'm breaking unwritten rules of engagement, I'm as green as they get. I even have problems uploading code because it doesn't seem to format it correctly. Basically, it looks like a wall of text that would upset anyone. I'm in a summer class and it moves very fast and outside of basic HTML this is all new to me. I'm trying my best. Any links to on how to better interact with the community here are appreciated. – intheyear3000 Jul 29 '17 at 00:32

2 Answers2

0

Check out this related question: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

I honestly didn't understand your explanation of the text input, but it might be because the Scanner#nextInt method does not consume the last newline ('\n') character. If that's the case, a workaround is to call another nextLine() to consume it.

Also, Scanner#next do ignore whitespaces, but it doesn't ignore primitives, such as booleans or integers, as they are valid tokens.

-1

When you get your string with nextLine() you have to split it with:

  • something like String#substing(int begin, int end) more information
  • or you use String#split(" ") and get a string array, which contains in index i the ith word. more information
  • I think it is also possible to get your Data with nextInt() and your Reader will only give you the first piece of data. With another nextInt() the second number, and so on. But I'm not sure about this solution (But you can try it, through)

After that you can convert it to a datatyp you want to have. For example if you want a double you can use Double.parseDouble(stringVar).

At Last you create your objects and you only have to find a way to print it.

F. Fritz
  • 69
  • 5
  • From my class, the professor said next() will read the first string it comes across, ignoring primitive data types and white spaces " " but the first " " it comes across after the read string will halt the read. nextInt() & more nextInt() ignore " " and strings and look for primitive data types and will run until it doesn't read anymore primitive data types, so long as you want it to? He made it sound like I can just "loop" through the file and blammo, populate my fields, create my objects, and print to the console. Methinks it's more than this... – intheyear3000 Jul 07 '17 at 00:06