-2

I want to create a program in Java that get number from user_input and make it currency format... Here is my code

package Seperator_checker;

import java.util.Scanner;

public class Seperator {

    public static void main(String[] args) {
        Scanner number=new Scanner(System.in);
        System.out.print("Please Enter Your Number: ");
        String user_number=number.next();
        if(user_number.length()> 3) {
            user_number=user_number.substring(0,user_number.length()-3) + "," + user_number.substring(0,1);
            System.out.println("________________________________________");
            System.out.println("Your Currency Number Is: "+ user_number);
        }

    }

}
Nisarg
  • 1,631
  • 6
  • 19
  • 31
meh rdad
  • 1
  • 3
  • 2
    Please, format your code properly and state what doesn't work for you. Also, what do you mean by "currency format"? What is expected input and output? – Sergei Sirik Sep 11 '18 at 19:43
  • Is there a reason you cannot use `NumberFormat`? I might not be reading your code correctly, but I don't see where, e.g., "1000000" will be formatted to "1,000,000" correctly. Plus as currency, there should be decimals, etc. And nothing here handles localization. See [Java Currency Number format](https://stackoverflow.com/questions/2379221/java-currency-number-format). – KevinO Sep 11 '18 at 19:47
  • please make your question clear and format it properly. – The Scientific Method Sep 11 '18 at 19:52
  • e.g., "1000000" will be formatted to "1,000,000" : yes that exactly what i want – meh rdad Sep 12 '18 at 15:54

2 Answers2

0

One spontaneous answer I could think of is splitting the String into a List of Chars, and go through it backwards and insert a , after every third step, until you reach the beginning.

Pseudocode:

Convert String to Char List
goto end of List
counter = 0
while havent reached beginning of list
    counter += 1
    if counter == 3
        counter = 0
        insert ',' into List
        //maybe go back one here too depending on implementation of 
        //insert and which is the current element after inserting
    end if
    go one back one element in List
end while

sorry I have no idea how to write pseudo code

CWorldEnder
  • 13
  • 1
  • 5
0

If you have to use substring:

I dont know how to put this into words, so here is the code :)

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        int rest = input.length() % 3;
        if(rest == 0){
            rest = 3;
        }
        //add the "irregular" comma
        input = input.substring(0,rest) + "," + input.substring(rest);

        //add the rest
        for(int i = rest+1;i < input.length()-3; i+= 4){//4 because of the comma
            input = input.substring(0, i+3) + "," + input.substring(i+3, input.length()); 
        }
        System.out.println(input);
    }
}

(Basically first take care of the irregular part in the beginning, and then go through in steps of 3 (4 because of the commas you insert) and insert the commas)

Hope this could help :)

CWorldEnder
  • 13
  • 1
  • 5