Solved

substring and ascii values

Posted on 2004-04-13
14
1,891 Views
Last Modified: 2010-05-18
Hey guys,

I have a string that i need to get the last four characters of and then step the certain characters either four characters back or forth throught the alphabet.

for example of the letter were "G" i may have to  step "H -> I -> J -> K".

This solution will also need to account for looping if A needs to be stepped back to  Z -> Y -> x etc.

I would do this myself but do not know how to work with ASCII values in JSP.

Cheers

Simon
0
Comment
Question by:aimon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10819021
how do you determine when to go forward and when back?
0
 
LVL 92

Expert Comment

by:objects
ID: 10819032
the code would look something like:

char[] c = s.toCharArray();
for (int i=c.length-5; i<c.length; i++)
{
   c[i] += 4;
   if (c[i]>'Z') c[i] -= 26;
}
0
 

Author Comment

by:aimon
ID: 10819077
one character will go forward and one will go back.

I have the characters A and B now.

Can you ammend the code to deal with this?

Cheers
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 10819112
to go backwards use:

   c[i] -= 4;
   if (c[i]<'A') c[i] += 26;
0
 
LVL 92

Expert Comment

by:objects
ID: 10819116
assuming your string only contains A-Z
0
 

Author Comment

by:aimon
ID: 10819240
and forwards would be?

c[i] += 4;
   if (c[i]<'Z') c[i] -= 26;
0
 
LVL 92

Expert Comment

by:objects
ID: 10819248
y
0
 

Author Comment

by:aimon
ID: 10821026
i get the following error

testcode.java:59: operator > cannot be applied to java.lang.String,char
probably occurred due to an error in testcode.jsp line 19:
if (firstNameChar>'Z') firstNameChar -= 26;

is this correct?
0
 
LVL 7

Expert Comment

by:searlas
ID: 10821390
You've gone and declared firstNameChar as a String haven't you....

It should be a char.
0
 

Author Comment

by:aimon
ID: 10829023
Hey searlas,

The code compiles but does not step the characters down the alphabet

here is my code

<%!      
 String code = "aaaa5555bbbb";
 String lastFour = (code.toUpperCase()).substring((code.length() - 4),(code.length()));
 String firstName = "Simon";
 String surname = "Hilton";  
 boolean success = false;
 char[] fnCharArray = firstName.toCharArray();
 char[] snCharArray = surname.toCharArray();
 char fnChar = fnCharArray[0];
 char snChar = snCharArray[0];
 char fnCharStepped = fnChar;
 char snCharStepped = snChar;
 
 boolean isValidCode(){

              fnCharArray[0] += 4;
               if (fnCharArray[0]>'Z') fnCharArray[0] -= 26;
            
            snCharArray[0] += 4;
               if (snCharArray[0]>'Z') snCharArray[0] -= 26;
      
       return true;
 }        
%>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p>Code = <%=code.toUpperCase()%><br>
  lastfour = <%=lastFour%> <br>
  firstname = <%=firstName%><br>
  surname = <%=surname%><br>
  firstname character = <%=fnChar%><br>
  surname character = <%=snChar%><br>
  firstname character stepped = <%=fnCharArray[0]%><br>
  surname character stepped = <%=snCharArray[0]%><br>
  success = <%=success%></p>
<p><u><strong>Comments</strong></u></p>
</body>
</html>

And here is my output

Code = AAAA5555BBBB
lastfour = BBBB
firstname = Simon
surname = Hilton
firstname character = S
surname character = H
firstname character stepped = S
surname character stepped = H
success = false
0
 
LVL 92

Expert Comment

by:objects
ID: 10829043
you never actually step the characters.
0
 

Author Comment

by:aimon
ID: 10829108
isn't that this bit?

fnCharArray[0] += 4;

if not what is the command to do this in java?
0
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 10829247
yes but that method never gets called that I can see.
0
 
LVL 92

Expert Comment

by:objects
ID: 10830587
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String format issue 13 93
Retrieving file from bytes array in spring mvc 8 62
instanceof  operator in java 26 105
going to wrong jsp page 2 48
Building a cohesive image for your brand is vital to making an impression on consumers. When the economy is tough, brands do better than unbranded  products. This can have a huge impact on your long-term profits, as the economy goes up and down.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question