?
Solved

substring and ascii values

Posted on 2004-04-13
14
Medium Priority
?
1,894 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 600 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

Industry Leaders: 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

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
New style of hardware planning for Microsoft Exchange server.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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