Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 28 Apr 2008 09:21:07 pm    Post subject:

I have a question about Scanners in java.

I am attempting to read data from a .csv file. To do this, I am using a FileInputStream and then using a Scanner object to access the data. I have done the command useDelimeter(","); this is so that I can get each item from my database.

Unfortunately I have run into some problems while trying to take the data from the file and get it into my program. Sometimes in the .csv file there is no data between commas. So it goes like this, "Who,an,3,3,S,S,,TRUE,National,12/5/98" (Notice the ,, ).

This has made my code to generate an exception.

while (in.hasNext()) {
temp = in.next();
if (temp.charAt(0) == '"') {
do {
temp += in.next();
} while (temp.charAt(temp.length() - 1) != '"');
}

seperate.add(temp);
}


Seperate is an ArrayList<String>, in is the Scanner object, temp is a String object

When it reaches the ,, I need it to return an empty String. Why is it producing this exception and what do I need to do to get a null String instead?


Last edited by Guest on 02 Aug 2010 02:27:31 am; edited 1 time in total
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 28 Apr 2008 10:20:21 pm    Post subject:

I've never used scanners, but I think the String trim() method will do the job here. I forget the syntax, but you specify a delimiter, and it returns an array of Strings containing the data fields. Check the java api for the syntax, I suppose...
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 29 Apr 2008 08:40:03 am    Post subject:

The command String trim() will omit the leading and trailing whitespaces from a String. I think you were referring to the command String split(regex). This splits a string around the delimeter regex.

The problem with using that command is that some of my data fields contain commas in them. That is why I need to check each item as I remove it to see if it starts with a quotation mark. And as I am typing this I just realized my error. The problem is when I check the empty String. It throws an error when I try "temp.charAt(0)" because I'm checking an empty String. I will replace this with (temp.length() > 0 && temp.charAt(0)). I have to run to work right now, I will post my results this afternoon.
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 29 Apr 2008 02:11:27 pm    Post subject:

Yeah, when I posted that I had been awake for 29 hours straight, so I guess I wasn't thinking at my best Razz But yes, I did mean the split command, and your realization sounds like it could fix your problem. Good luck! Smile
Back to top
sgm


Calc Guru


Joined: 04 Sep 2003
Posts: 1265

Posted: 29 Apr 2008 02:26:24 pm    Post subject:

Surely you can just use an already-written Java CSV parser?

I guarantee that there will be thousands of examples.

(CSV is a lousy format incidentally, despite its widespread usage.)


Last edited by Guest on 29 Apr 2008 02:27:30 pm; edited 1 time in total
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 29 Apr 2008 02:34:25 pm    Post subject:

I hadn't thought about using an already-written one. It is more fun to write your own anyways.

I know that CSV is not the best format, but it is the best fit for the use that I need. The normal database is stored in Access, which is much worse than CSV.
Back to top
sgm


Calc Guru


Joined: 04 Sep 2003
Posts: 1265

Posted: 29 Apr 2008 02:44:12 pm    Post subject:

bananaman wrote:
It is more fun to write your own anyways.

Oh probably, but more error-prone. Just look at what happens with something as simple as binary search.

Quote:
I know that CSV is not the best format, but it is the best fit for the use that I need.  The normal database is stored in Access, which is much worse than CSV.

Well, ya gotta do what ya gotta do.
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 29 Apr 2008 04:38:02 pm    Post subject:

I adjusted the code as I stated early, and now everything works fine.
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 30 Apr 2008 10:19:57 am    Post subject:

Maybe it is best to use a premade CSV Java utility.

While doing further coding, I kept running into unexpected behavior, so I did some testing to see what values something was holding.

Examine these two outputs and see if you can figure out the unexpected behavior.


Code:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99


Code:
0 : QUESTION_NBR
1 : PREFIX
2 : QUESTION
3 : ANSWER
4 : CONFLICT
5 : REFERENCE
6 : TYPE
7 : KEY
8 : SYLLABLE_NV
9 : SYLLABLE_SR
10 : SYLLABLE_CLASS_NV
11 : SYLLABLE_CLASS_SR
12 : NV_SR
13 : SCREENED
14 : TRANS_BY
15 : TRANS_DATE
16 : "In Galatians 1,"
17 : Who is an apostle by Jesus Christ and God the Father?
18 : Paul
19 : "Phil 3:5, Gal 4:7"
20 : Gal 1:1
21 : Who
22 : an
23 : 3
24 : 3
25 : S
26 : S
27 :
28 : TRUE
29 : National
30 : 12/5/98
31 :
32 : Who raised Jesus Christ from the dead?
33 : Who [God (the Father) ]
34 :
35 : Gal 1:1
36 : Who
37 : raised
38 : 2
39 : 2
40 : S
41 : S
42 :
43 : TRUE
44 : National
10 : 12/5/98
46 : "In a verse 3,"
47 : From whom is grace and peace?
48 : God the Father and (our [believers'] Lord) Jesus Christ
49 : Phil 1:2
50 : Gal 1:3
51 : Who
52 : grace
53 : 4
54 : 4
55 : M
56 : M
57 :
58 : TRUE
59 : National
11 : 12/5/98
61 : "In a chapter 1,"
62 : Who gave Himself for our sins?
63 : Who [Jesus Christ]
64 : "Gal 2:20, 3:18"
65 : Gal 1:4
66 : Who
67 : gave
68 : 2
69 : 2
70 : S
71 : S
72 :
73 : TRUE
74 : National
13 : 12/5/98
76 :
77 : Whom might Jesus (Christ) deliver from this present evil world?
78 : us [believers]
79 :
80 : Gal 1:4
81 : Who
82 : Jesus
83 : 4
84 : 4
85 : M
86 : M
87 :
88 : TRUE
89 : National
14 : 12/5/98
91 :
92 : According to whose will might Jesus Christ deliver us from this present evil world?
93 : God's (and our [believers'] Father)
94 :
95 : Gal 1:4
96 : Who
97 : will
98 : 6
99 : 6

Look at number 45, 60, 75, 90 in the second example. Or should I say, the lack thereof. Why is this second code skipping over those numbers?
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 30 Apr 2008 10:00:07 pm    Post subject:

What happens when you replace TRANS_DATE with something else?
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 01 May 2008 10:26:19 am    Post subject:

in the future, it is ALWAYS helpful to tell *what* exception is raised. they aren't arbitrarily thrown, so the name and stack trace are always helpful to figuring out the problem.
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 01 May 2008 01:54:10 pm    Post subject:

Weregoose wrote:
What happens when you replace TRANS_DATE with something else?
[post="123021"]<{POST_SNAPBACK}>[/post]


What exactly do you mean by doing this. Do you mean, the solitary element in position 15. Or do you mean each multiple of 15.

The date is the same for a lot of these questions, so I think Java is only using one String element to represent them all, and the other occurrences simply point back to that one string. What I am wondering is, does the ArrayList.get(i) method actually change the value of i?

elfprince13 wrote:
in the future, it is ALWAYS helpful to tell *what* exception is raised. they aren't arbitrarily thrown, so the name and stack trace are always helpful to figuring out the problem.
[post="123035"]<{POST_SNAPBACK}>[/post]


What exception are you talking about? I figured out my original problem was caused by trying to access a part of a string that didn't exist. My second question, the code has not thrown any exceptions. I am wondering why it displays 10, 11, 12, 13 when it should be displaying 45, 60, 75, 90.
Back to top
Weregoose
Authentic INTJ


Super Elite (Last Title)


Joined: 25 Nov 2004
Posts: 3976

Posted: 01 May 2008 03:21:38 pm    Post subject:

bananaman wrote:
Weregoose wrote:
What happens when you replace TRANS_DATE with something else?
[post="123021"]<{POST_SNAPBACK}>[/post]


What exactly do you mean by doing this. Do you mean, the solitary element in position 15. Or do you mean each multiple of 15.
Not knowing the language, I ventured to guess that each multiple of 15 somehow looked to TRANS_DATE for their outputs; this now doesn't seem right upon looking at the short bit of code, but I'm again led to wonder whether the forward slash might be the cause of the function going haywire. Those lines that have dates are the only ones involving the character, at least.

Last edited by Guest on 01 May 2008 03:23:56 pm; edited 1 time in total
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 01 May 2008 07:52:43 pm    Post subject:

That is definitely a possibility. Java typically uses a '/' as the escape character within a string. It very well could producing unexpected results.

Edit:
I tried removing the forward slashes from the .CSV file and nothing changed.

Then I tried removing the new lines from the file. That way all the data was on one line. This solved the problem entirely. I realized that the numbers 10, 11, 12, 13 were supposed to be the next entries, but since they were after a New Line instead of after a comma, they were included in the data entry before them. Because of the new line inside the String, it was overwriting number 30 on the same line. This proves why it is easier to use pre-made libraries.


Last edited by Guest on 01 May 2008 08:56:10 pm; edited 1 time in total
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 01 May 2008 09:32:01 pm    Post subject:

Quote:
This has made my code to generate an exception.

elfprince13 wrote:
in the future, it is ALWAYS helpful to tell *what* exception is raised. they aren't arbitrarily thrown, so the name and stack trace are always helpful to figuring out the problem.



its much easier to help someone debugging with the full stack trace, or at least the name of the exception that is thrown. obviously you figured that one out, hence my "in the future"


Last edited by Guest on 01 May 2008 11:46:53 pm; edited 1 time in total
Back to top
luby
I want to go back to Philmont!!


Calc Guru


Joined: 23 Apr 2006
Posts: 1477

Posted: 02 May 2008 05:37:01 pm    Post subject:

I don't see where he said he had an exception
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 02 May 2008 09:04:42 pm    Post subject:

luby wrote:
I don't see where he said he had an exception

bananaman wrote:
So it goes like this, "Who,an,3,3,S,S,,TRUE,National,12/5/98" (Notice the ,, ).

This has made my code to generate an exception.
[post="122942"]<{POST_SNAPBACK}>[/post]
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 

Advertisement