• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

What is the Java Implementation for This?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have a Windows text file with the following info inside it:

25 05 38 26 53 04
07 45 50 33 19 34
55 25 21 30 09 39
26 11 30 12 13 41
32 23 44 11 50 39
45 30 07 44 55 54
21 10 35 46 48 27
52 41 05 53 11 50
40 38 17 43 10 54
45 27 29 12 39 31
24 42 38 02 18 09
13 43 28 06 53 30
45 47 29 30 53 13
38 45 28 48 47 36
25 34 18 06 07 55

How can I code them to break that info apart into six columns and put each column into their own array? (Like in the image below...)


{If you can't still see the image, kindly click on this link.. - webpage )
 
Saloon Keeper
Posts: 12728
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep track of a list of columns, and repeatedly iterate over the columns while you have input:

Now that you have a list of columns (with each column being a list of integers), you can convert them all to arrays if you really wanted to, but I would prefer keeping them as lists.
 
Fudgie Braun
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, where does the text file reading part come in?

Please bear with me - I don't seem to see it in the code...
So sorry :-(
 
Stephan van Hulst
Saloon Keeper
Posts: 12728
277
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the scanner variable. You create it like this:

By default, a Scanner will split the input into tokens that are delimited by whitespace, no matter whether the whitespace is a space or a new line. This will cause scanner.nextInt() to return the next token as an int, no matter how your file is formatted, as long as each individual token is a valid integer and they are separated by whitespace and nothing else.

Be sure to declare the scanner and the reader inside the parentheses of the try-statement. It will ensure that the file is closed after you're done reading it.
 
Marshal
Posts: 72066
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice bit of code, but I think I would change this:-toIf you get any situation where you don't have a next int, that means the file has a line with 5 or 7 numbers in, or a non‑number, so the file is corrupt and you should stop reading. Should I throw a different sort of exception?
 
Stephan van Hulst
Saloon Keeper
Posts: 12728
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you get any situation where you don't have a next int, that means the file has a line with 5 or 7 numbers in, or a non‑number, so the file is corrupt and you should stop reading.


I don't agree. The formatting of the file itself is inconsequential. It's just a bunch of integers that get distributed over different lists.

If you want to deduce the number of columns from the formatting, you should probably perform some special processing on the first line first.

If you don't care that the number of integers is a multiple of the number of columns, but you still want to check that the input doesn't contain invalid tokens, you should probably do it like this:

Should I throw a different sort of exception?


InputMismatchException or an appropriate custom exception.
 
rubbery bacon. crispy tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic