• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Issue with Scanner class

 
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading from a CSV file with lines like this:

2,2021-10-11,12,29.00,

When I read the first value using the Scanner class, the debugger says it's "2", but it's a byte array. Thus, when I try to Integer.parseInt(), it fails with a number format exception.

I also tried code like this:



but that also fails.

I've really not used the Scanner class that much so I would appreciate any suggestions.

Thanks,
- mike
bytes.png
[Thumbnail for bytes.png]
 
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike London wrote:I'm reading from a CSV file with lines like this:
2,2021-10-11,12,29.00,

A mindless CSV parser would split this on the comma (,) characters giving you a String array like:
I don't see anywhere that a byte array appears.
 
Saloon Keeper
Posts: 13396
296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's NOT a byte array. It's a string. It has a private "value" field that's a byte array, but you don't need to know that (in fact, it's apparently only serving to confuse you).

The reason your temp variable is a byte array is because you're converting the string "2" to a byte array using the getBytes() method. Why? What's the point of that?

If you want to read an integer using a scanner, just call nextInt().

I strongly advise against reading a CSV file using a Scanner though. CSV files can be more complex than you think. Use a CSV parsing library.
 
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and if you change the String to a byte[], you are not simply getting 2. Please print out that text because I think you are getting more than you expected, possibly quote marks.
 
Saloon Keeper
Posts: 24558
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
I strongly advise against reading a CSV file using a Scanner though. CSV files can be more complex than you think. Use a CSV parsing library.

 
Mike London
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem was the Excel export to CSV.

I took all the values from that CSV and copied them into a new file and it worked fine.

Strange, right?

Thanks to all!

- mike
 
Carey Brown
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how did the new file look any different?
 
Mike London
Bartender
Posts: 1832
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:And how did the new file look any different?



Hey Carey,

Nope, both were ostensibly just regular text files. I have no idea.

Appreciate your reply.

-- mike
 
Saloon Keeper
Posts: 1646
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Were they intended to contain the same data?

When that sort of annoying thing happens to me, I will use whatever diff tool I have handy.

It could be something stupid like line endings, or presence or absence of a newline (whatever that is) somewhere...

If they were slightly different data rather than the same ones, it could be some stupid case that could come back up again at the least convenient time...

It is probably good your error is behind you, but many problems that could cause such behavior wouldn't jump to most people's eyes without running a diff.

 
Carey Brown
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you still using getBytes() to read it?
 
Campbell Ritchie
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Earlier today, I wrote:...if you change the String to a byte[] . . .

I tried it on JShell:-

jshell> "2".getBytes()
$1 ==> byte[1] { 50 }

Adding the UTF_8 charset didn't make any difference, so please check whether your text is actually in UTF8.
 
Carey Brown
Saloon Keeper
Posts: 8750
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are we using getBytes() at all? They're Strings.
 
Campbell Ritchie
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike London wrote:. . . when I try to Integer.parseInt(), it fails with a number format exception. . . .

You can't have tried to pass a byte[] to that method because none of its overloadings takes byte[] as a parameter, so that would fail to compile.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic