Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Feedback on Program  RSS feed

 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm taking an Intro to Computer Science class and was looking for some feedback on code I've written.

This code reads from a csv file, and prints three arrays. One is a null array. One is the info from the file. One is the info from the file but each element is modified in some way (i.e. first column contains names, in the modified array you'd change it to be all in caps).
My csv file is called Trees and lists the name, height, and diameter of appx. 10 trees.

There is one problem that I can't seem to find the source of;
all elements from the csv file are read as Strings, I convert the height and diameter's to int and double numbers respectively.
When converting the heights however it throws my exception saying that the string is not an int, even though they are in fact int's (i.e. 100, 10, 80, etc.)

Also in the class Trees I was wondering if the get/set methods require JavaDocs

I don't know why and was hoping a fresh pair of eyes could help me out.
I've pasted the code and csv file below.
Thank You!

Name, Height, Diameter
Koa, 100, 5
Wiliwili, 20, 3
Ohia, 80, 3
Halapepe, 25, 3
Loulu, 75, 1
Naio, 60, 3
Naupaka, 16, 3
Sandalwood, 33, 5
Milo, 50, 7
Eucalyptus, 100, 5
Noni, 20, 0.5
Iliahi, 13, 0.5



Output:
Display Trees array[] without initializing elements:
Index Element
0 null
1 null
2 null
3 null
4 null
5 null
6 null
7 null
8 null
9 null
10 null
11 null
ERROR: 100 is not a number!
ERROR: 20 is not a number!
ERROR: 80 is not a number!
ERROR: 25 is not a number!
ERROR: 75 is not a number!
ERROR: 60 is not a number!
ERROR: 16 is not a number!
ERROR: 33 is not a number!
ERROR: 50 is not a number!
ERROR: 100 is not a number!
ERROR: 20 is not a number!
ERROR: 13 is not a number!

Display Trees array[] after initializing elements:
index name height diameter
0 Koa 0 5.00
1 Wiliwili 0 3.00
2 Ohia 0 3.00
3 Halapepe 0 3.00
4 Loulu 0 1.00
5 Naio 0 3.00
6 Naupaka 0 3.00
7 Sandalwood 0 5.00
8 Milo 0 7.00
9 Eucalyptus 0 5.00
10 Noni 0 0.50
11 Iliahi 0 0.50

Display Trees array[] after initializing elements:
index name height diameter
0 KOA -10 15.00
1 WILIWILI -10 13.00
2 OHIA -10 13.00
3 HALAPEPE -10 13.00
4 LOULU -10 11.00
5 NAIO -10 13.00
6 NAUPAKA -10 13.00
7 SANDALWOOD -10 15.00
8 MILO -10 17.00
9 EUCALYPTUS -10 15.00
10 NONI -10 10.50
11 ILIAHI -10 10.50

 
Junilu Lacar
Sheriff
Posts: 11152
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

There are many things I could comment on but let me start with this: There's way too much code jammed into main. It's what's called a Big Ball of Mud. I like to say that you've lost yourself in the woods, you're so deep into the details that you've lost sight of the forest for the trees. (Pun intended)

To make your code more comprehensible, coherent, and maintainable, you should break out each different task, e.g. parsing strings to get some ints and doubles, into its own separate method. Strive to write small methods. That way, your main method will consist of calls to these tasks and will read more like a story that explains what your program is doing rather than being an inscrutable jumble of code that you need to hack your way through to figure out.

Sorry I started off with non-specific advice but I really wish schools would concentrate on teaching these kinds of things first so that students don't start off with poor code hygiene habits right from day one which they'll have to unlearn later on. They teach us good hygiene in kindergarten, to wash our hands before eating and after going to the bathroom. They should also teach computer programming kiddies how to be clean about their code writing first and foremost.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your heightToVal has a leading space.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could fix it with this

Allows optional spaces on either side of the comma.
 
Junilu Lacar
Sheriff
Posts: 11152
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use String.split() and String.trim() methods
 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Could fix it with this

Allows optional spaces on either side of the comma.


Hello,

Just wanted to ask in regards to "\\s*,\\s*"
What does it break down to mean/ translate to?

And why, with just

did the diameter convert but the height did not?
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiera Hakikawa wrote:
Carey Brown wrote:Could fix it with this

Allows optional spaces on either side of the comma.


Hello,

Just wanted to ask in regards to "\\s*,\\s*"
What does it break down to mean/ translate to?

And why, with just

did the diameter convert but the height did not?

\s means space or tab
\\s allows this to be a part of a Java String which uses backslash as an escape character.
\\s* means zero or more spaces

Integer.parseInt() is pickier than Double.parseDouble(). Don't know why.
 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . \s means space or tab . . .
Or some other forms of whitespace, including line ends (though you can alter some regex engines' confugurations to include or exclude line ends).

There is another way to turn Strings into ints:Beware: there are some oddities about that technique.
  • 1: You are using the old‑fashioned technique of returning a sentinel value from failure. That needs to be tested for; an alternative would be to throw an Exception (see below).
  • 2: That technique will happily accept "   123 Campbell" as a valid int=123.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!