Solved

substring and ascii values

Posted on 2004-04-13
14
1,885 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
  • 8
  • 5
14 Comments
 
LVL 92

Expert Comment

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

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
to go backwards use:

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

Expert Comment

by:objects
Comment Utility
assuming your string only contains A-Z
0
 

Author Comment

by:aimon
Comment Utility
and forwards would be?

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

Expert Comment

by:objects
Comment Utility
y
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:aimon
Comment Utility
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
Comment Utility
You've gone and declared firstNameChar as a String haven't you....

It should be a char.
0
 

Author Comment

by:aimon
Comment Utility
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
Comment Utility
you never actually step the characters.
0
 

Author Comment

by:aimon
Comment Utility
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
Comment Utility
yes but that method never gets called that I can see.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
A procedure for exporting installed hotfix details of remote computers using powershell
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now