Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a really quick question about replacing a character in a string at a specific index WITHOUT using nay built-in methods of java.
The task is to be completed with using the for loop only

my input should be: "option number" then phrase "then specific index"
my expected output is " "*" replaced with the character at the specific index"

This is what I tried so far, but I don't know what to put inside the if statement or if my code is wrong


import java.util.Scanner;
public class Phrases{
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
int option = Integer.parseInt(keyboard.nextLine());
String phrase = keyboard.nextLine();
if (option == 1){
int x = keyboard.nextInt();
for (int y = 0; y < phrase.length(); y++){
char n = phrase.charAt(y);
if ()
System.out.print(n+"*");
else
System.out.print(n);

What I have tried:

The above code is what I tried
Posted
Updated 17-Apr-20 21:51pm

1 solution

You have the requested index value in the variable x. So when y is equal to x you are at the correct position: that is the character to replace. Alternatively you could just replace the character at offset x and then print the updated string.
 
Share this answer
 

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