Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: I wanted to output the string itself, but the output looks like it's been encrypted or something

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default I wanted to output the string itself, but the output looks like it's been encrypted or something

    Hi, I'm trying to fix a string from "hollo werld" to "hello world". To do this, I made another string of what we want, turned both of them into char arrays, and iterated through, changing elements as needed in a for loop. Here's what I have:

    Here's the method definition:
    package samsExperiments;
     
    public class StaticMethodExample {	
     
    	//fix "Hollo Werld" to "Hello World"
     
    	public static String fixBadStr(char[] badStrToChars, char[] goodStrToChars) {
    		for(int i = 0; i < badStrToChars.length; i++) {
    			if(badStrToChars[i] != goodStrToChars[i]) {
    				badStrToChars[i] = goodStrToChars[i];//badStrToChars[i].replace
    			}
    		}
    		String backToStr = badStrToChars.toString();
    		return backToStr;
    	}
    }
    Here's where the method is called:
    package samsExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.ListIterator;
    import java.util.Random;
     
    import static samsExperiments.StaticMethodExample.*;
     
    import java.lang.StringBuilder;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.InputMismatchException;
    import java.util.Map;
     
    import customExceptions.IntegerOutOfRangeException;
     
    public class SamsExperimentsMain {
     
    	public static void main(String[] args){
     
    		String badStr = "Hollo Werld";
    		String goodStr = "Hello World";
    		char[] badStrToChars = badStr.toCharArray();
    		char[] goodStrToChars = goodStr.toCharArray();
    		String backToStr = StaticMethodExample.fixBadStr(badStrToChars, goodStrToChars);
    		System.out.println(backToStr);
     
    	}//end of main method	
     
    }//end of class

    The output is "[C@55f96302". What happened? Why isn't the output "Hello World"?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    The output is "[C@55f96302".
    That is the String returned by the default toString method for a char array. If you want to see a String, use one of the String class's constructors that take a char array and create a String from it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    SamJava_the_Hut (April 12th, 2019)

  4. #3
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    It works! Thanks Norm:
    public static String fixBadStr(char[] badStrToChars, char[] goodStrToChars) {
    		for(int i = 0; i < badStrToChars.length; i++) {
    			if(badStrToChars[i] != goodStrToChars[i]) {
    				badStrToChars[i] = goodStrToChars[i];//badStrToChars[i].replace
    			}
    		}
    		String backToStr = String.valueOf(goodStrToChars);
    		return backToStr;
    	}

  5. #4
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    I'm now trying to fix my bad string with array lists instead of regular arrays. If I use regular arrays, the program crashes with an array out of bounds error if the badStr and goodStr strings are not the same length. To counter this problem, I assumed array lists wouldn't have the same limited capacity problem, especially after giving both Character array lists an initial capacity of 200 on lines 9 and 14 in the FixString class:

    FixString.java
    package samsExperiments;
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.lang.StringBuilder;
     
    public class FixString {		
     
    	public String fixBadStrWithArrayList(String badStr, String goodStr) {
    		ArrayList<Character> badStrToChars = new ArrayList<>(200);
    		for(int x = 0; x < badStr.length(); x++) {
    			badStrToChars.add(badStr.charAt(x));
    		}
    		System.out.println(badStrToChars);
    		ArrayList<Character> goodStrToChars = new ArrayList<>(200);
    		for(int y = 0; y < goodStr.length(); y++) {
    			goodStrToChars.add(goodStr.charAt(y));
    		}
    		System.out.println(goodStrToChars);
    		for(int i = 0; i < badStrToChars.size(); i++) {
    			if(badStrToChars.get(i) != goodStrToChars.get(i)) {
    				/*char x = badStrToChars.get(i);
    				char y = goodStrToChars.get(i);
    				x = y;*/
    				badStrToChars.set(i, goodStrToChars.get(i));
    			}			
    		}		
    		StringBuilder str = new StringBuilder();
    		for(char z : badStrToChars) {
    			str.append(z);
    		}
    		String backToStr = String.valueOf(str);
    		return backToStr;
    	}
    }

    SamsExperimentsMain.java
    package samsExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
     
    public class SamsExperimentsMain {
     
    	public static void main(String[] args){	
     
    		FixString z = new FixString();
     
    		System.out.println("What is the bad string?");
    		Scanner a = new Scanner(System.in);
    		String badStr = a.nextLine();
    		System.out.println("What do you want the string to be?");
    		Scanner b = new Scanner(System.in);
    		String goodStr = b.nextLine();
    		String backToStr = z.fixBadStrWithArrayList(badStr, goodStr);
    		System.out.println("Our bad string is now changed to: " + backToStr);		
     
    	}//end of main method	
     
    }//end of class

    However, I still get an out of bounds error if the badStr string is longer than the goodStr string:
    Output:
    What is the bad string?
    holllo world
    What do you want the string to be?
    hello world
    [h, o, l, l, l, o, , w, o, r, l, d]
    [h, e, l, l, o, , w, o, r, l, d]
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at samsExperiments.FixString.fixBadStrWithArrayList(F ixString.java:20)
    at samsExperiments.SamsExperimentsMain.main(SamsExper imentsMain.java:28)

    And the returned backToStr string (which is supposed to be the fixed version of the original badStr) is cut off when the badStr is shorter than the goodStr:
    Output:
    What is the bad string?
    hello world
    What do you want the string to be?
    holllo world
    [h, e, l, l, o, , w, o, r, l, d]
    [h, o, l, l, l, o, , w, o, r, l, d]
    Our bad string is now changed to: holllo worl

    Why is all of this happening? And why is the returned backToStr cut off?
    Last edited by SamJava_the_Hut; June 25th, 2019 at 04:22 AM.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
    The code uses an index 11 that is past the end of the list. The list has less that 12 elements.
    Check that the code does not use an index past the end of the list.

    The code needs to test the value of the index against the length of the String it is used with if the two Strings are not the same length. Now the code uses the same index against both Strings.

    What does the logic need to be for when the Strings are different lengths?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    Norm, I am getting way closer! The IndexOutOfBoundsException problem is gone. Below is my updated code:

    FixString.java
    package samsExperiments;
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.lang.StringBuilder;
     
    public class FixString {		
     
    	public String fixBadStrWithArrayList(String badStr, String goodStr) {
    		ArrayList<Character> badStrToChars = new ArrayList<>();
    		for(int x = 0; x < badStr.length(); x++) {
    			badStrToChars.add(badStr.charAt(x));
    		}
    		System.out.println(badStrToChars);
    		int badSize = badStrToChars.size();
    		ArrayList<Character> goodStrToChars = new ArrayList<>();
    		for(int y = 0; y < goodStr.length(); y++) {
    			goodStrToChars.add(goodStr.charAt(y));
    		}
    		System.out.println(goodStrToChars);
    		int goodSize = goodStrToChars.size();
    		if(badSize < goodSize) {
    			System.out.println("badStr size before increase = " + badSize);
    			badStrToChars.ensureCapacity(goodSize);
    			System.out.println("badStr size after increase = " + badStrToChars.size() + ", " + badSize);
    		}
    		else if(goodSize < badSize) {
    			System.out.println("goodStr size before increase = " + goodSize);
    			goodStrToChars.ensureCapacity(badSize);
    			System.out.println("goodStr size after increase = " + goodStrToChars.size() + ", " + goodSize);
    		}
    		for(int i = 0; i < badStrToChars.size()-1; i++) {
    			if(badStrToChars.get(i) != goodStrToChars.get(i)) {//if the current badStr char is not what it's 
    															   //supposed to be,
    				badStrToChars.set(i, goodStrToChars.get(i));//then replace it with the correct goodStr char.
    			}			
    		}
    		System.out.println("Bad char array size " + badSize);
    		System.out.println("Good char array size " + goodSize);
    		if(badStrToChars.size() > goodStrToChars.size()) {//if there are still extra chars that don't belong there,
    			badStrToChars.subList(goodSize, badStrToChars.size()).clear();//then trim them off the fixed badStr.
    		}
    		StringBuilder str = new StringBuilder();
    		for(char z : badStrToChars) {
    			str.append(z);
    		}
    		String backToStr = String.valueOf(str);
    		return backToStr;
    	}
    }

    SamsExperimentsMain.java
    package samsExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
     
    public class SamsExperimentsMain {
     
    	public static void main(String[] args){	
    		SamsDataStructures x = new SamsDataStructures();
     
    		FixString z = new FixString();
     
    		System.out.println("What is the bad string?");
    		Scanner a = new Scanner(System.in);
    		String badStr = a.nextLine();
    		System.out.println("What do you want the string to be?");
    		Scanner b = new Scanner(System.in);
    		String goodStr = b.nextLine();
    		String backToStr = z.fixBadStrWithArrayList(badStr, goodStr);
    		System.out.println("Our bad string is now changed to: " + backToStr);		
     
    	}//end of main method	
     
    }//end of class

    When the goodStr is longer than the badStr, the output is mostly as expected, except I don't understand why the goodStrToChars arraylist size stays at 11 instead of increasing to 12 like it's supposed to:
    Output:
    What is the bad string?
    holllo werld
    What do you want the string to be?
    hello world
    [h, o, l, l, l, o, , w, e, r, l, d]
    [h, e, l, l, o, , w, o, r, l, d]
    goodStr size before increase = 11
    goodStr size after increase = 11, 11
    Bad char array size 12
    Good char array size 11
    Our bad string is now changed to: hello world

    When the badStr is longer than the goodStr, notice that the returned backToStr prints out as "holllo werd" instead of "holllo werld":
    Output:
    What is the bad string?
    hello world
    What do you want the string to be?
    holllo werld
    [h, e, l, l, o, , w, o, r, l, d]
    [h, o, l, l, l, o, , w, e, r, l, d]
    badStr size before increase = 11
    badStr size after increase = 11, 11
    Bad char array size 11
    Good char array size 12
    Our bad string is now changed to: holllo werd

    What is up with that? Any idea as to why the l is the char that is left out?

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    like it's supposed to
    What is the code supposed to do?
    Can you add some comments to the code saying what it is trying to do?
    Describe what the program will do when the Strings are the same length and what it is supposed to do when one String is longer than the other.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    Quote Originally Posted by Norm View Post
    What is the code supposed to do?
    Can you add some comments to the code saying what it is trying to do?
    Describe what the program will do when the Strings are the same length and what it is supposed to do when one String is longer than the other.
    The overall goal of this entire program is to overwrite the old badStr with the goodStr, regardless of what length the strings are. The overwritten badStr is the fixed version that of the badStr, and it's only fixed if it's exactly the same as the goodStr. Here you go:

    FixString.java
    package samsExperiments;
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.lang.StringBuilder;
     
    public class FixString {		
     
    	public String fixBadStrWithArrayList(String badStr, String goodStr) {
    		ArrayList<Character> badStrToChars = new ArrayList<>();
    		for(int x = 0; x < badStr.length(); x++) {
    			badStrToChars.add(badStr.charAt(x));
    		}
    		System.out.println(badStrToChars);
    		int badSize = badStrToChars.size();
    		ArrayList<Character> goodStrToChars = new ArrayList<>();
    		for(int y = 0; y < goodStr.length(); y++) {
    			goodStrToChars.add(goodStr.charAt(y));
    		}
    		System.out.println(goodStrToChars);
    		int goodSize = goodStrToChars.size();
    		if(badSize < goodSize) {
    			System.out.println("badStr size before increase = " + badSize);
    			badStrToChars.ensureCapacity(goodSize);
    			System.out.println("badStr size after increase = " + badStrToChars.size() + ", " + badSize);
    		}
    		else if(goodSize < badSize) {
    			System.out.println("goodStr size before increase = " + goodSize);
    			goodStrToChars.ensureCapacity(badSize);
    			System.out.println("goodStr size after increase = " + goodStrToChars.size() + ", " + goodSize);
    		}
    		for(int i = 0; i < badStrToChars.size()-1; i++) {
    			if(badStrToChars.get(i) != goodStrToChars.get(i)) {//if the current badStr char is not what it's 
    															   //supposed to be (if the char in the current 
    															   //badStrToChars index position is not the same  
    															   //as the char in the same index position of the
    															   //goodStrToChars),
    				badStrToChars.set(i, goodStrToChars.get(i));//then replace the badStrToChars char with the
    															//goodStrToChars char.
    			}			
    		}
    		System.out.println("Bad char array size " + badSize);
    		System.out.println("Good char array size " + goodSize);
    		if(badStrToChars.size() > goodStrToChars.size()) {//if there are still extra chars that don't belong there
    														  //(which will happen if the badStr is longer than the 
    														  //goodStr),
    			badStrToChars.subList(goodSize, badStrToChars.size()).clear();//then trim them off the fixed badStr.
    		}
    		//Allow the now overwritten badStrToChars arraylist to become an official String,
    		//so that we can return as a String, as the user originally entered it in as:
    		StringBuilder str = new StringBuilder();
    		for(char z : badStrToChars) {
    			str.append(z);
    		}
    		String backToStr = String.valueOf(str);
    		return backToStr;//return the overwritten and converted version of the badStr
    	}
    }

    SamsExperimentsMain.java
    package samsExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
     
    public class SamsExperimentsMain {
     
    	public static void main(String[] args){	
    		SamsDataStructures x = new SamsDataStructures();
     
    		FixString z = new FixString();
     
    		System.out.println("What is the bad string?");
    		Scanner a = new Scanner(System.in);
    		String badStr = a.nextLine();
    		System.out.println("What do you want the string to be?");
    		Scanner b = new Scanner(System.in);
    		String goodStr = b.nextLine();
    		String backToStr = z.fixBadStrWithArrayList(badStr, goodStr);
    		System.out.println("Our bad string is now changed to: " + backToStr);		
     
    	}//end of main method	
     
    }//end of class


    The only thing that doesn't work yet is when the goodStr is longer than the badStr. The effect of that situation is as follows:
    Output:
    What is the bad string?
    hello world
    What do you want the string to be?
    holllo werld
    [h, e, l, l, o, , w, o, r, l, d]
    [h, o, l, l, l, o, , w, e, r, l, d]
    badStr size before increase = 11
    badStr size after increase = 11, 11
    Bad char array size 11
    Good char array size 12
    Our bad string is now changed to: holllo werd

    So any idea to what happened to the l in werld?

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: I wanted to output the string itself, but the output looks like it's been encrypted or something

    So any idea to what happened to the l in werld?
    Try debugging the code by printing out the characters that are being replaced as they are replaced inside the loop.

    Also work though the logic with paper and pencil to see what is happening and to find the problem.

    Can you post the expected results for these Strings:
    123
    with
    45
    456
    4567
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: September 7th, 2014, 08:25 AM
  2. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  3. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  4. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  5. Help with string output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 27th, 2011, 02:05 PM