My World Rules My World Rules - 1 year ago 71
Java Question

String.replace isn't working

import java.util.Scanner;

public class CashSplitter {
public static void main(String[] args) {
Scanner S = new Scanner(;

System.out.println("Cash Values");
String i = S.nextLine();
for(int b = 0;b<i.length(); b ++){

i.replace(i.substring(0, i.indexOf('.') + 3), "");
System.out.println(i.substring(0, i.indexOf('.') + 3));

The code should be able to take a string with multiple cash values and split them up, into individual values. For example 7.32869.32 should split out 7.32, 869.32 etc

Answer Source

A string is immutable, therefore replace returns a new String for you to use


 i = i.replace(i.substring(0, i.indexOf('.') + 3), "");

Although try using

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download