Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in output format

 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
my application contains jsp,servlets....my problem is im unable to get the required format of my output code....i need my output as

1) I need to get as 'A,12:' but im getting as 'A,12::::'..if i enter one value in my jsp page
2) I need to get as 'A,12:R,5:' but im getting as 'F,5:R,:5:::'-----if i enter 2 values in my jsp page...

here is my code...



thanks,
madhu.
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhuri.

You have complicated the code. Sorry. Why you are getting those extra colons at the end of your output is because that the getParameterValues method will return all the values of the webpage, even if they are empty. So inorder to eliminate those that are empty, modify the condition in the for loop and check for an empty string after trimming it using trim method.

Also instead of the if condition inside the for loop, you can increment the loop variable 'i' by 2 instead of just 1. Then you can directly concatenate what you need.

I need to know what value you are entering in your jsp page. Then only I can tell you more...

Hope this information is useful

Regards,
Hari
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
thanks for your reply..i tried what you suggested..but im getting the output as follows
output ---- 'DB1,5:DV1,8::::::' but i need as 'DB1,5:DV1,8:'
here is my modified code....im not able to trace why im getting ending so many 'colons' but i need is only one 'colon'

you told to use trim function may i know where to implement it...
in my jsp page....i had checkbox with corresponding textbox....here is my jsp page code...


i hope you got what im saying...can you help me out in this matter..waiting for the reply...

thanks,
madhu
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any help regarding my post
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
madhuri kunchala wrote:hi hari,
thanks for your reply..i tried what you suggested..but im getting the output as follows
output ---- 'DB1,5:DV1,8::::::' but i need as 'DB1,5:DV1,8:'
here is my modified code....im not able to trace why im getting ending so many 'colons' but i need is only one 'colon'

you told to use trim function may i know where to implement it...
in my jsp page....i had checkbox with corresponding textbox....here is my jsp page code...

i hope you got what im saying...can you help me out in this matter..waiting for the reply...

thanks,
madhu

Well - where in your modified code did you do what Hari suggested ??

- check for empty strings
- changed the for loop variable 'i' from 1 to 2
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PatienceIsAVirtue.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rene Larsen wrote:Well - where in your modified code did you do what Hari suggested ??

- check for empty strings
- changed the for loop variable 'i' from 1 to 2

and one more thing...

What are the values of the String[] you get from request.getParameterValues(...) ??
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhuri,

I don't think, you have done the changes in your code what I told. I will tell you again. The code should be as follows



I think that should work.

Reply as soon as possible

Hope this information is useful

Regards
Hari
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
His own code will also work - if he checks for empty Strings in the for loop...

Like this:


 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: you will get better performance looking for odd and even numbers by using i & 1. I shall let you work out whether it returns 1 for even numbers or 0 for even numbers.
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rene,

In your code, there are two conditions that are checked for every time the loop runs. In that two conditions, one of them is not at all needed because the number of times that the loop is going to run, is dependent on the second condition which you have written in the if block. So I made it the loop condition, which removes unnecessary branching statement "if". Comparing your code and mine [I am telling for only educational purposes, not to prove that i am better], my code is optimized and performs better because, unnecessary branching statements are not present, and unnecessary conditions are removed.

Regards
Hari
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hari haran the great wrote:Hi Rene,

In your code, there are two conditions that are checked for every time the loop runs. In that two conditions, one of them is not at all needed because the number of times that the loop is going to run, is dependent on the second condition which you have written in the if block. So I made it the loop condition, which removes unnecessary branching statement "if". Comparing your code and mine [I am telling for only educational purposes, not to prove that i am better], my code is optimized and performs better because, unnecessary branching statements are not present, and unnecessary conditions are removed.

Regards
Hari

Yeah, you are right - I wasn't trying to say that your code wasn't correct...

- BTW your code isn't correct, the result will always be an empty String (hint: !)
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rene,

Sorry, I forgot that ! symbol in the for loop condition

;)

Thanks
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thanks to Rene and Hari....i got what type of output i want.....i checked where i went wrong in my code.....and coming to Rene's question regarding
What are the values of the String[] you get from request.getParameterValues(...) ??


The values can me numbers or the string what ever entered in the form....

Thanks a lot for providing me the solution....i need to do some modifications for the output what i am getting i.e.,

im getting output as 'DB1,5:DV1,8:' and if i need to add at the end using any symbol that means either '$' or '*' or some specific symbols how to append that symbols..that means i need to get output as 'DB1,5:DV1,8:$'

i had done changes like this to the existing code


im getting output as 'DB1,5:$DB1,5:DV1,8:$$' but i need as 'DB1,5:DB1,5:DV1,8:$'....where i had to do changes for getting correct output....

thanks,
madhu.



 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhuri,

I can't understand your requirement. If you want to have any symbol at the end. Then simply append it at the end after the for loop.



Voila..

You got the output you needed.

If it is not what you have wanted, then tell me properly

Regards
Hari
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Hari,
according to your code i had checked..im getting only '$' symbol...no values.

1) i need the output as 'F,5:R,5:$'
2) i appended at the end and im getting output as 'F,5:$R,:$5'

here is my code..


i hope you got my point what i want

thanks,
madhu.

 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Madhu,

You have misunderstood what I told. I wanted you to append after the loop. But You have appended it inside the loop. So it will work wrongly. First of all, you code is not optimized as I told Rene. Try using the code, which I gave you. Check it out..
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
i had appended at the end what you suggested...im getting output as F,5:R,:5$ but i need as F,5:R,5:$...here is my modified code


thanks,
madhu.
 
subhash kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
madhuri kunchala

why are you not trying the hari's code
else
compare your code with the hari's code
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi subhash kumar,
i tried with hari's code...i m getting output as $..nothing im getting...here is my code which hari has suggested me...

thanks,
madhu.

 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhuri,

Even still, i cannot understand your requirement. Can you please tell me completely. Only then I can help you.

 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhuri, in my code, replace the variable names and parameter names with what your program has
 
subhash kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi subhash kumar,
i tried with hari's code...i m getting output as $..nothing im getting...here is my code which hari has suggested me...



madhuri you are not concentrating on the thread some body pointed out the hari's code lacks ! sign in the for loop
try last i have pasted

correct is


 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi subhash kumar,
i checked it working...i came to know where i went wrong...
thanks,
madhu.
 
subhash kumar
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hari : sorry I got in between you was talking to madhuri in this thread
good work by you
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem Subhash

Madhuri: Got what you needed

 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
sorry i wrote wrongly what you had suggested is working fine...i m getting what i required.....even my problem was not closed.....in my jsp page...i had checkboxes and textboxes..if i select sidebyside checkboxes and textboxes im getting correct output...i.e., F,5:A,8:R,7:$

1)suppose if left alternate checkboxes and textboxes...im getting the output as F,5:$ or F,5:R,:8
here is my code..


my jsp code is


can you help me out...
thanks,
madhu.
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey madhu. Is your JSP contains only those 4 checkboxes and textboxes. If it is so, what you are doing, is again a complicated way.
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
i had nine check boxes corresponding text boxes....i had given first 4 check boxes and text boxes......i can select continuously or any checkboxes out of nine or sometimes only one....
1)if i select only one...im getting correct output example:'F,5:$'
2)if i select 1st and 2nd that means continuous checkboxes im getting correct output example:'F,5:A,7:$'
3)if suppose i select 1st and 3rd checkboxes im getting the output as 'F,5:$' but i need to get 'F,5:R,7:$'

im unable to trace where im going wrong....

thanks,
madhu.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hari haran the great wrote:Reply as soon as possible

Even though you're not the one asking the problem, you should still EaseUp.
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi madhu.

your code needs modifications... Alter the for loop like shown here




Check it and tell me as soon as possible. I am leaving out now. Sorry..
But I am sure, that the above will satisfy your needs


 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
thanks a lot..its working fine....thanks a lot....once again...have a nice weekend...bye..
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

I am sorry. Actually I am going out now. That is why I stressed upon getting answers fast. I thought if i didn't reply, the questioner may mistake me. That is why. Anyway, I will correct it.

Thanks
 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhuri,

Feel free to ask me, any doubts that you may have.

Thanks.

bye
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
if i had doubts i'll surely ask you...enjoy the weekend...bye...
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
im getting error as java.lang.ArrayIndexOutOfBoundsException: 11...im selecting 1st,4th and 6th checkboxes..im getting that error...may i know why..

 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhuri ,change the loop's condition to length -1
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
i changed according what you had suggested...im getting the wrong output format..i.e.,F,5:M,4:R_1_L,:7:$ but i need as F,5:M,4:R_1_L,7:$
here i the code i modified


thanks,
madhu.

 
Hari haran Ravi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi madhu..

The code you have given, is not my code. My Code contains || symbol in the if inside for loop Check it and try out
 
madhuri kunchala
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hari,
according to your code...i had done changes...but im not getting correct output..im getting as F,5:M,4:$ i had selected 1st,4th and 6th checkboxes....i need to get as F,5:M,4,R_1_L,8:$ ...here is the code

thanks,
madhu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic