• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue in the csv file or in code while implementing composite pattern  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on composite pattern . I have a program  that accepts the data from the csv file and have to print it in the tree - like structure. It goes as .... the file has a president that is at the root. the president's employee id will be the  designation id of the Manager. And the manager's employee id will be the designation id  of the remaining employee's that includes Analyst , Clerk , Salesman. such is the tree. However , i am noticing a hindrance. The president's employee id comes with a the quotes on one side. like



How do i solve the issue.

program is

Main()



Interface


For printing President and Manager




For  printing the remaining  designations




 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the line in your composite.csv file where that data occurs? And also some of the lines surrounding it?
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
composite.csv

7698,'BLAKE','MANAGER',7839,'1-MAY-81',2850,NULL,30
7369,'SMITH','CLERK',7902,'17-DEC-80',800,NULL,20
7788,'SCOTT','ANALYST',7566,'09-DEC-82',3000,NULL,20
7934,'MILLER','CLERK',7782,'23-JAN-82',1300,NULL,10
7654,'MARTIN','SALESMAN',7698,'28-SEP-81',1250,1400,30
7499,'ALLEN','SALESMAN',7698,'20-FEB-81',1600,300,30
7839,'KING','PRESIDENT',NULL,'17-NOV-81',5000,NULL,10
7782,'CLARK','MANAGER',7839,'9-JUN-81',2450,NULL,10
7844,'TURNER','SALESMAN',7698,'8-SEP-81',1500,0,30
7900,'JAMES','CLERK',7698,'3-DEC-81',950,NULL,30
7521,'WARD','SALESMAN',7698,'22-FEB-81',1250,500,30
7902,'FORD','ANALYST',7566,'3-DEC-81',3000,NULL,20
7876,'ADAMS','CLERK',7788,'12-JAN-83',1100,NULL,20
7566,'JONES','MANAGER',7839,'2-APR-81',2975,NULL,20
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message indicates that your data has a double quote in it but you're not showing it in the data you posted. I don't see a problem with your code. How did you post the data? Were you viewing it and then cutting and pasting it into the post? Could the program you used possibly be hiding the quotes from you? Other than that I don't know.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are those the exact contents of the CSV file? Because what you posted shows single quotes whereas the message you are telling us you are getting shows double quotes. Plus, there are no quotes around the value in the CSV file contents you posted.

One glaring thing in the code you posted: The local variables that are declared inside the while-loop will only have scope for one loop iteration. The variables will get initialized on each iteration of the while loop. Is that really what you want to happen with the topHierarchy and bottomHierarchy variables? What's the point of using those names then, since that would make them misleading. If this is not your intention, you should declare these variables outside of the while loop.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

At line 15 you look to see if b[3] is NULL, then you procede in line 22 to try and convert NULL to an int. That won't work.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey

7839,'KING','PRESIDENT',NULL,'17-NOV-81',5000,NULL,10
b[0] is not NULL. b[3] is NULL
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And b[0] is showing quotes error. as in
Exception in thread "main" java.lang.NumberFormatException: For input string: ""7839"
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sucheta Shrivastava wrote:@Carey

7839,'KING','PRESIDENT',NULL,'17-NOV-81',5000,NULL,10
b[0] is not NULL. b[3] is NULL

I realize that b[3] is not the error you initially reported on, but the code involving b[3] is in error none the less.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly Carey's point. You're not getting it. On line 15, execution will only continue into the body of the if-statement if b[3] is "NULL" -- that means that if line 22 gets executed, then b[3] *will* be NULL. That means your call to Integer.valueOf(b[3]) on line 22 will fail with an exception.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu

I created like that hierarchy previously also in a program and it worked fine.

The code may be incorrect as i am a novice programmer . But the CSV file for the number is giving numberformatexception.

Exception in thread "main" java.lang.NumberFormatException: For input string: ""7839"
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sucheta Shrivastava wrote:And b[0] is showing quotes error. as in
Exception in thread "main" java.lang.NumberFormatException: For input string: ""7839"

This leads me to suspect your data. How are you viewing your csv file? Did you try opening it up in notepad?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, based on what you posted as the supposed contents of the CSV file, we don't see why you would get that exception. We are asking you to confirm that you have posted the EXACT contents of the CSV file, which we suspect you haven't because the error message shows a double quote preceding the numbers and what you posted contains no double quotes at all, only single quotes.

Please clarify our doubts.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu and carey.

I corrected the quotes error by changing the file extention from .csv to .txt file. Now i no more get that error. As Junilu said  mentioned, at line no. 22
if (valueOfPresident == Integer.valueOf(b[3]).intValue())

i am getting the error of the input string it encountered as "NULL" it is throwing the exception.




I tried to replace the place of NULL  with  "0".

But it is still showing the same error .How do i modify and rectify my code





 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sucheta Shrivastava wrote:
I tried to replace the place of NULL  with  "0".

But it is still showing the same error .How do i modify and rectify my code

Line 12 isn't doing what you think it's doing. Remember, a String is immutable and all its methods that seem to modify it actually return another String object. The replace() method is no exception. So, when you called b[3].replace(...), you didn't change the string "NULL" to the string "0", you just threw "0" away and left "NULL" where it was originally.  There really is no need to call the replace() method at all. What you're trying to do is assign a different value to the element b[3]. How do you assign a value to an array element?

Talking about the larger picture however, your approach to resolving the exception you are getting on line 22 is very kludgey, that is, it's very awkward and inelegant. The problem with the code goes deeper than just trying to parse an integer out of a non-integer string value. The structure of your logic is very awkward and convoluted. I suggest you go back up and try to describe very clearly, in plain English, what you would like to do. Also, try to simplify things a little. You are making things hard for yourself with the complicated gyrations you are doing with value conversions, just like you tried to do something by calling replace() when a simple assignment statement would have worked.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!