Click here to Skip to main content
14,828,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public class Rep
{
      public static void main(String args[])
     {
          Scanner s=new Scanner(System.in);
          int flag=0,p=0;
          String sen="java program";
          System.out.println("Enter the character to be searched");
          char search=s.next().charAt(0);
          System.out.println("Enter the replacement char");
          char replace=s.next().charAt(0);
          char ch[]=sen.toCharArray();
          for(int i=0;i<ch.length;i++)
{
if(search==ch[i])
p=i;
flag=1;
break;
}
if(flag==1)
ch[p]=replace;
system.out.println(ch.tostring()); it's="" showing="" some="" garbage="" value
}
else
system.out.println("character="" not="" found");
}
}


What I have tried:

System.out.println(ch.toString());

Instead of printing the result it's showing some garbage value. How to fix it.
Posted
Updated 3-Jul-19 10:58am
v3

How about this?

Java
if(sen.indexOf(search) != -1){
    string result = sen.substring(0, sen.indexOf(search) + 1).replace(search, replace);
    System.out.println(result);
}
else{
    System.out.println("character not found");
}


I assume you are on a real project, not a homework project to learn how to program.
   
It's not that code: that doesn't compile because the else clause is outside the body of the main method.
And
Java
system.out.println("character="" not="" found");
Isn't valid either.
Plus, "System" is not the same as "system".

What you are running is not that code: it's the last version that compiled, and what that looked like we have no idea, and probably neither do you...

Try this:
Java
public static void main (String args[]){
    Scanner s = new Scanner (System.in);
    int flag = 0, p = 0;
    String sen = "java program";
    System.out.println ("Enter the character to be searched");
    char search = s.next().charAt(0);
    System.out.println ("Enter the replacement char");
    char replace = s.next().charAt (0);
    char ch[] = sen.toCharArray();
    for (int i = 0; i < ch.length; i++){
	if (search == ch[i]){
	    p = i;
	    flag = 1;
	    break;
        }
    }
    if (flag == 1){
        ch[p] = replace;
        System.out.println (new String (ch));
    }
    else {
        System.out.println ("character " + search + " not found");
    }
}
   
Comments
Member 13954890 3-Jul-19 12:56pm
   
yes it worked :D Thanks a lot but why it's throwing garbage value when converting character array(i.e ch) to String using toString()?
First of all, when you add a comment, make it a comment
Java
system.out.println(ch.tostring()); // it's="" showing="" some="" garbage="" value

-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Your if at the end is a mess:
Java
public class Rep
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int flag=0,p=0;
        String sen="java program";
        System.out.println("Enter the character to be searched");
        char search=s.next().charAt(0);
        System.out.println("Enter the replacement char");
        char replace=s.next().charAt(0);
        char ch[]=sen.toCharArray();
        for(int i=0;i<ch.length;i++)
        {
            if(search==ch[i])
            p=i;
            flag=1;
            break;
        }
        if(flag==1)
            ch[p]=replace;
        system.out.println(ch.tostring()); // it's="" showing="" some="" garbage="" value
    }
    else
        system.out.println("character="" not="" found");
}
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Quote:
Instead of printing the result it's showing some garbage value. How to fix it.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900