Solved

substring and ascii values

Posted on 2004-04-13
14
1,887 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session bean client machine 9 57
Struts tiles for layout structure along with Angular JS. 4 152
Coldfusion 10 - JSP File Not Found - 404 Error 5 84
maven project jars 7 50
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

21 Experts available now in Live!

Get 1:1 Help Now