Java: prevent auto-decoding of encoded String - java

I have an encoded String like this:
17298457,abcdef/17298529,ghijklm/17298562,opq%2Frstu
and want to split it on the "/".
In the last part, there is a encoded "/" as "%2F".
The result is
[17298457,abcdef , 17298529,ghijklm , 17298562,opq , rstu]
The problem is, that Java decodes the string on the fly as soon as i pass it to another method (split method e.c.)
Do someone have a good idea how to work around that?
thanks a lot!
monk

Not for me....
import java.util.Arrays;
public class Test {
public static void main(String[] args) throws Exception {
String s = "17298457,abcdef/17298529,ghijklm/17298562,opq%2Frstu";
System.out.println(Arrays.toString(s.split("/")));
}
}
gives
[17298457,abcdef, 17298529,ghijklm, 17298562,opq%2Frstu]

public static void main(String[] args) {
String test = "17298457,abcdef/17298529,ghijklm/17298562,opq%2Frstu";
String[] args2 = test.split("/");
for (int i = 0; i < args2.length; i++) {
String[] args3 = args2[i].split("%2F");
for (int j = 0; j < args3.length; j++) {
if(!args3[j].trim().startsWith(",") && j != 0)
System.out.print(" ,");
System.out.print(args3[j]);
}
}
OUT PUT - AS U WRITTEN -
17298457,abcdef17298529,ghijklm17298562,opq ,rstu

Related

Java program to reverse a string not working?

I have written a piece of code to reverse a string in Java. However, its showing multiple errors and I wish to understand where it is that I am going wrong. I know that there are alternative methods of reversing a string. However, I want to know where I am going wrong with my code.
public class RevString {
public static void main(String[] args)
{
public Reverse (String str)
{
int len = str.length();
String rev;
for (int i = 0; i <= len; i++)
{
rev = str[i] + rev;
}
System.out.println(rev);
}
Reverse("Canyon");
}
}
Errors:
Multiple markers at this line
- Syntax error on token ")", ; expected
- Syntax error on token "(", . expected
- Reverse cannot be resolved to a type
- Illegal modifier for parameter str; only final is
The method Reverse(String) is undefined for the type
RevString
Could someone provide me with a resolution?
There are many errors in your code :
For loop condition should be i < len
String rev should be initialized to "" (empty string), else it will throw error when you try to append another string to it.
You can't access characters in a string using str[i], use str.charAt(i) instead.
You are trying to initialize a function (Reverse) inside another function (main), you must initialize it outside the main function.
Also, here is a simple one liner for string reversal:
new StringBuilder(str).reverse().toString()
A good tip might be to use the StringBuilder class whenever you want to do any kind of string manipulation in Java.
Your code has many issues:
You are declaring the method Reverse() inside the main method.
You also need to initialize rev to an empty string.
You can use str.charAt(i) to access each character of the string.
Your for loop goes beyond the string if you use i <= len; so it
should be i < len;.
Your Reverse() method should be static since you are calling it in main
method (which is static)
Here is working code.
public class RevString {
public static void main(String[] args) {
Reverse("Canyon");
}
public static void Reverse (String str) {
int len = str.length();
String rev="";
for (int i = 0; i < len; i++) {
rev = str.charAt(i) + rev;
}
System.out.println(rev);
}
}
Please see the below code:
public class Hello {
public static String reverse (String str){
int len = str.length();
String rev="";
char[] strArray = str.toCharArray();
for (int i = 0; i < len; i++)
{
rev = strArray[i] + rev;
}
return rev;
}
public static void main(String[] args) {
String result = reverse("Canyon");
System.out.println("Reversed String: " + result);
}
}
public class reverseString
{
public static void main(String[] args)
{
System.out.println("Welcome to the the string reverser.");
System.out.println("Here is where a person may put is a sentence and the orintation" +
" of the words is reversed.");
Scanner keyboard = new Scanner(System.in);
String word = keyboard.nextLine();
int lengthOf = word.length();
int j = 0;
char loopStr;
String LoopStr;
String Nstr = "";
for(int n = lengthOf; n >0 ;n--)
{
j = n;
LoopStr = word.substring(j-1,j);
Nstr = Nstr + LoopStr;
}
System.out.println(Nstr);
}
}

PalindromeChecker comparTo

I am trying to make a Palindrome (the inverse of word is the word self e.g. tacocat) checker in java.
I use this code:
private static void PalindroomChecker(String sWord){
char[] arrcWord=sWord.toCharArray();
char[] arrcDrow=new char[arrcWord.length];
for(int i=0;i<arrcWord.length;i++)
for(int j=arrcDrow.length-1;j>=0;j--)
arrcDrow[j]=arrcWord[i];
String sDrow=new String(arrcDrow);
if(sWord.compareTo(sDrow)==0)
System.out.println(sWord);
else
System.out.println("false");
}
for some reason I keep printing false. So for some reason there are no palindrome's not even tacocat.
you only need the index. This should work.
private static void PalindroomChecker(String sWord) {
char[] arrcWord = sWord.toCharArray();
char[] arrcDrow = new char[arrcWord.length];
int i = 0;
for (int j = arrcDrow.length - 1; j >= 0; j--)
arrcDrow[j] = arrcWord[i++];
String sDrow = new String(arrcDrow);
if (sWord.compareTo(sDrow) == 0)
System.out.println(sWord);
else
System.out.println("false");
}
you might have got why your code doesn't work from other answers... here is a no loop approach it's much simpler::
public static void main(String[] args) {
String yourString = "tacocat";
StringBuilder a = new StringBuilder(yourString);
System.out.println(a.reverse().toString().equals(yourString) ? true : false);
}

replacing elements of an array change by change

I'm trying to do some basics concerning an array of elements, specifically characters. My question is, how do I get the program to print my changes one by one? for example (I do not want my output going from "moon" to "mOOn" in one instance, but from "moon" to "mOon" to "mOOn", like that. Here is my code.
import java.util.*;
public class Practice
{
public static void main(String[] args)
{
String array[] = {"uuuuuuuupppppppssssssssss"};
System.out.println(Arrays.toString(array));
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i] = array[i].replace('p', 'P'));
//trying to print each change here
}
}
}
Thanks again!
EDIT/Update: I got the output to get the loop right, but the output is still not what I want (basically output: uPPPPPPS, uPPPPPPs, uPPPPPs, etc until the length of p ends). Any hints on what I could do? Thanks!
public static void main(String[] args){
String array = "uuuuuuuupppppppssssssssss";
System.out.println(array);
char[] chars = array.toCharArray(): //converted
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'p') {
System.out.println(array.replace('p', 'P'));
}
}
}
public static void main(String[] args)
{
String array = "uuuuuuuupppppppssssssssss";
System.out.println(array);
while ( array.contains("p") )
{
array = array.replaceFirst("p", "P");
System.out.println(array);
}
}

Alternate display of 2 strings in Java

I have a java program where the following is what I wanted to achieve:
first input: ABC
second input: xyz
output: AxByCz
and my Java program is as follows:
import java.io.*;
class DisplayStringAlternately
{
public static void main(String[] arguments)
{
String firstC[], secondC[];
firstC = new String[] {"A","B","C"};
secondC = new String[] {"x","y","z"};
displayStringAlternately(firstC, secondC);
}
public static void displayStringAlternately (String[] firstString, String[] secondString)
{
int combinedLengthOfStrings = firstString.length + secondString.length;
for(int counter = 1, i = 0; i < combinedLengthOfStrings; counter++, i++)
{
if(counter % 2 == 0)
{
System.out.print(secondString[i]);
}
else
{
System.out.print(firstString[i]);
}
}
}
}
however I encounter the following runtime error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
AyC at DisplayStringAlternately.displayStringAlternately(DisplayStringAlternately.java:23)
at DisplayStringAlternately.main(DisplayStringAlternately.java:12)
Java Result: 1
What mistake is in my Java program?
If both arrays have same length for loop should continue while i < anyArray.length.
Also you don't need any counter to determine from which array you should print first. Just hardcode that first element will be printed from firstString and next one from secondString.
So your displayStringAlternately method can look like
public static void displayStringAlternately(String[] firstString,
String[] secondString) {
for (int i = 0; i < firstString.length; i++) {
System.out.print(firstString[i]);
System.out.print(secondString[i]);
}
}
Anyway your code throws ArrayIndexOutOfBoundsException because each time you decide from which array print element you are incrementing i, so effectively you are jumping through arrays this way
i=0 i=2
{"A","B","C"};
{"x","y","z"};
i=1 i=3
^^^-here is the problem
so as you see your code tries to access element from second array which is not inside of it (it is out of its bounds).
As you commented, If both arrays length is same, you can simply do
firstC = new String[] {"A","B","C"};
secondC = new String[] {"x","y","z"};
Then
for(int i = 0; i < firstC.length; i++) {
System.out.print(firstC[i]);
System.out.print(secondC[i]);
}
Using the combined length of the Strings is wrong, since, for example, secondString[i] would cause an exception when i >= secondString.length.
Try the below working code with high performance
public static void main(String[] arguments)
{
String firstC[], secondC[];
firstC = new String[] {"A","B","C"};
secondC = new String[] {"x","y","z"};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < firstC.length; i++) {
builder.append(firstC[i]);
builder.append(secondC[i]);
}
System.out.println(builder.toString());
}
public class concad {
public void main(String[] args) {
String s1 = "RAMESH";
String s2 = "SURESH";
int i;
int j;
for (i = 0; i < s1.length(); i++) {
System.out.print(s1.charAt(i));
for (j = i; j <= i; j++) {
if (j == i) {
System.out.print(s2.charAt(j));
}
}
}
}
}
I have taken two strings as mentioned.Then pass one counter variable in inner for-loop with second string,Then for every even position pass with code "counter%2".Check this out if any concern then comment below.
public class AlternatePosition {
public static void main(String[] arguments) {
String abc = "abcd";
String def = "efgh";
displayStringAlternately(abc, def);
}
public static void displayStringAlternately(String firstString, String secondString) {
for (int i = 0; i < firstString.length(); i++) {
for (int counter = 1, j = 0; j < secondString.length(); counter++, j++) {
if (counter % 2 == 0) {
System.out.print(secondString.charAt(i));
break;
} else {
System.out.print(firstString.charAt(i));
}
}
}
}
}

Finding specific argument then adding value of next one to variable

I'm new to java and i need some help. I got few things to do and i'm stuck
with this problem. I really have no idea how to do it...
So in CMD line if i enter banana banana apple apple -name Carlos banana Mike -c 8
it will print "Hello Carlos!" eight times.
public class cheese {
public static void main(String args[]) {
for(String s: args){
if(s.equals("-name")){
String p = (GIVE VALUE OF FIRST ARGUMENT AFTER "-name");
if (s.equals("-c")){
int i = Integer.parseInt(THE FIRST ARGUMENTS AFTER "-c");
for(int j=0; j >= i ; j++)
System.out.println("Hello "+p+"!");
}
}
}
}
}
Parsing command-line arguments properly is surprisingly hard, and there are lots of libraries that can help. Your example code can be rearranged as follows to make it work (but it has no real error handling, so there are lots of ways to make it go wrong, such as passing "-name" twice, or not supplying enough arguments).
public class CmdLine {
public static void main(String args[]) {
String p = "";
int i = 0;
for (int k = 0; k < args.length; k++) {
if (args[k].equals("-name")) {
p = args[k + 1];
} else if (args[k].equals("-c")) {
i = Integer.parseInt(args[k + 1]);
}
}
for (int j = 0; j < i; j++) {
System.out.println("Hello " + p + "!");
}
}
}

Categories