Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

substring and ascii values

Posted on 2004-04-13
14
Medium Priority
?
1,897 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Working from home is a dream for many people who aren’t happy about getting up early, going to the office, and spending long hours at work. There are lots of benefits of remote work for employees.
Let's take a look into the basics of ransomware—how it spreads, how it can hurt us, and why a disaster recovery plan is important.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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