Increase variable value through charAt() [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm new for Java,and I want to know can we increase the variable value through charAt() in java as below.
public class CheckPalindrome{
public static boolean isPalindrome(String text) {
int length = text.length();
int forward = 0;
int backward = length - 1;
while (backward > forward) {
char forwardChar = text.charAt(forward++);
char backwardChar = text.charAt(backward--);
if (forwardChar != backwardChar)
return false;
}
return true;
}
public static void main(String args[]){
System.out.println (isPalindrome("level"));
}
}
I want to know what is happening below code line..
char forwardChar = text.charAt(forward++);

String is immutable, so no you can't do that. You need to create a new string (e.g. with substring) and combine the results:
String text = "ABCCEFG";
char midCharacter = text.charAt(3);
midCharacter++;
String output = text.substring(0, 3) + midCharacter + text.substring(4);
Output:
ABCDEFG

Related

How to generate fixed length string from another string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
Is it possible from one string (date) generate another fixed length string (5-char code) by some encrypting algorithm for example? Also should be possible to confirm that a output string (5-char code) has been generated using the input string (date)
What I need:
generateCode("10-10-2010") -> "HG45Q"
isCodeValid("HG45Q", "10-10-2010") -> true
Slightly hacky and not tested fully, but seems to do the job. I shall leave you to code the inverse function for validation:
public static String generateCode(String s) {
String result = null;
s = s.replaceAll("\\D", "");
result = new BigInteger(s).toString(36).toUpperCase();
while (result.length() < 5) {
result = "0" + result;
}
return result;
}
class HelloWorld {
public static void main(String[] args) {
System.out.println(toHexString("10-10-2010".getBytes()));
System.out.println();
System.out.println(fromHexString("31302d31302d32303130"));
}
public static String toHexString(byte[] ba) {
StringBuilder str = new StringBuilder();
for(int i = 0; i < ba.length; i++)
str.append(String.format("%x", ba[i]));
return str.toString();
}
public static String fromHexString(String hex) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
}
return str.toString();
}
}

Change the case of every alternate char [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to write a code in Java which does make changes to case of a alphabet character in an alternating fashion(either make it lowercase or uppercase)
For Example:
changeCapitalization("hey 123 ABC idk"); // hEy 123 AbC iDk
changeCapitalization("abcdef ghijk 12 abc"); // aBcDeF gHiJk 1 AbC
You can use StringBuilder to build new string and boolean marker to change between lower/upper letter case.
public static String changeCapitalization(String str) {
StringBuilder buf = new StringBuilder(str.length());
boolean upperCase = false;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLetter(ch)) {
buf.append(upperCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
upperCase = !upperCase;
} else
buf.append(ch);
}
return buf.toString();
}
public static void main(String[] args) {
String str ="hello THERE";
String new_str="";
for(int i=0;i<str.length();i++){
String c =Character.toString(str.charAt(i));
if(Character.isUpperCase(str.charAt(i))){
c=c.toLowerCase();
new_str+=c;
}
else if(Character.isLowerCase(str.charAt(i))){
c=c.toUpperCase();
new_str+=c;
}
else if(c.equals(" ")){
new_str+=" ";
}
}
System.out.println(str);
System.out.println(new_str);
}

I want to make a program in which when I press the button the text changes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It is an android app.
Text is stored in an array. It should change serial wise
Here is what I had done before.
String name = "";
String names[] = {"A", "B", "C", "D"};
int counter = 0;
name = names[counter];
counter++;
if(counter >= 3)
{
counter = 0;
}
return name;
I was doing something like that before. I know it totally incorrect . But something like this I wanted to do.
This may help solve your problem.
import java.util.Scanner;
public class Main {
static int currentIndex = 0;
static String[] words = {"word1", "word2", "word3"};
public static void main(String[] args) {
while(true) {
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
if(input != null){
System.out.println(words[currentIndex++]);
if(currentIndex == words.length){
currentIndex = 0;
}
}
}
}
}

How to convert array of String in hex to array of int using Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a class like below
public class EXOR{
public static void conv(){
String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
"ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};
String binAddr[]=new String[18];
for (int i=0;i<18;i++)
{
int x[]=new int[18];
binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));
System.out.println("binernya : " +binAddr[i]);
}
}
public static void main(String[] args){
new EXOR().conv();
}
}
and I want to convert that array to binary array format.
I want to get output like below
for example
00100100001111110110101010001000
10000111101000110000100011010011
................................
How to fix this problem?
I suppose while executing your code you must've got a number point exception. This occurs when the Hexadecimal string is out of the range of Integer.
You can use:
binAddr[i]= (new BigInteger(Parray[i],16)).toString(2);
instead of
binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));
This will solve your problem for quick reference
Big Integer Documentation
Code:
public class EXOR {
public static void conv(){
String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
"ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};
String [] binAddr = new String[Parray.length];
for (int i = 0; i < binAddr.length; i++)
{
int strLen = Parray[i].length();
binAddr[i] = "";
for(int j = 0; j < strLen; j++) {
String temp = Integer.toBinaryString(
Integer.parseInt(String.valueOf(
Parray[i].charAt(j)), 16));
// Pad with leading zeroes
for(int k = 0; k < (4 - temp.length()); k++) {
binAddr[i] += "0";
}
binAddr[i] += temp;
}
System.out.println("Original: " + Parray[i]);
System.out.println("Binary: " + binAddr[i]);
}
}
public static void main(String[] args){
conv();
}
}
First few lines of Output:
Original: 243f6a88
Binary: 00100100001111110110101010001000
Original: 85a308d3
Binary: 10000101101000110000100011010011
We have Integer.MAX_VALUE = 2147483647
But, the 2nd item "85A308D3" = 2242054355. It exceed the capability of an Integer.
So, you use Integer.parseInt(85A308D3) will cause java.lang.NumberFormatException.
To fix it, change your code to use Long instead of Integer
binAddr[i] = Long.toBinaryString(Long.parseLong(Parray[i], 16));
Hope this help!

How to find substring without String methods using char arrays? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
You are not allowed to use any inbuilt functions like indexOf(), contains() or matches() of String class.
Find string apple in string webapple using given char arrays?
String webapple ="webapple";
String apple="apple";
char[] webappleArray=webapple.toCharArray();
char[] appleArray = apple.toCharArray();
write a function
public boolean isPresent(char[] apple ,char[] webapple ){
//your code here
}
I add it here in case someone really need it or want to study from it:
public static void main(String[] args) {
String webapple = "webapple";
String apple = "apple";
char[] webappleArray = webapple.toCharArray();
char[] appleArray = apple.toCharArray();
System.out.println(isPresent(appleArray, webappleArray));
}
public static boolean isPresent(char[] apple, char[] webapple) {
for (int i = 0; i < webapple.length - apple.length+1; i++) {
for (int j = 0; j < apple.length; j++) {
if (webapple[i + j] == apple[j]) {
if (j == apple.length - 1) {
return true;
}
} else {
break;
}
}
}
return false;
}

Categories

Resources