So what I'm aiming for here is to take the user's input as float and write code to split up that input into individual digits and use an array.

How would I go about doing this?

Here is what my code looks like currently:

import java.util.Scanner;
public class Asn8
{
	public static void main(String args[])
	{
		Scanner kb = new Scanner(System.in);
		String s1;
		System.out.println("Please enter the name on the check (First Last) : ");
		s1 = kb.nextLine();
		System.out.println("Please enter the amount for the check (as a float) : ");
		float f1 = kb.nextFloat();
		if(f1 < 0)
			System.out.println("Invalid amount : " + f1);
		System.out.println("\n------------------------------------------------------");
		System.out.println("\nPay to : " + s1 + "\t\t\t\t$" + f1);
	}
}

Here is the final output that was provided:

Please enter the name on the check (First Last): Maya Tolappa
Please enter the amount for the check (as a float): 123.45
 ---------------------------
Pay to : Maya Tolappa           $123.45 
one hundred twenty three dollars and  45 cents

The 123.45 which is input as a float is taken and displayed as "one hundred twenty three dollars and 45 cents"

It isn't supposed to just work for 123.45 but all floats.


All feedback is much appreciated,

- Aepex

Implement a new function that takes teh float and gives you back a string!
If you're unable to post the function's code with questions..

See the below code ..it should solve your purpose

import java.text.DecimalFormat;
import java.util.Scanner;
public class Asn8
{
	
	  private static final String[] tensNames = {
		    "",
		    " ten",
		    " twenty",
		    " thirty",
		    " forty",
		    " fifty",
		    " sixty",
		    " seventy",
		    " eighty",
		    " ninety"
		  };
	  
	  private static final String[] numNames = {
		    "",
		    " one",
		    " two",
		    " three",
		    " four",
		    " five",
		    " six",
		    " seven",
		    " eight",
		    " nine",
		    " ten",
		    " eleven",
		    " twelve",
		    " thirteen",
		    " fourteen",
		    " fifteen",
		    " sixteen",
		    " seventeen",
		    " eighteen",
		    " nineteen"
		  };
	  
	  private static String convertLessThanOneThousand(int number) {
		    String soFar;

		    if (number % 100 < 20){
		      soFar = numNames[number % 100];
		      number /= 100;
		    }
		    else {
		      soFar = numNames[number % 10];
		      number /= 10;

		      soFar = tensNames[number % 10] + soFar;
		      number /= 10;
		    }
		    if (number == 0) return soFar;
		    return numNames[number] + " hundred" + soFar;
		  }
	  
	  
	  public static String convert(long number) {
		    // 0 to 999 999 999 999
		    if (number == 0) { return "zero"; }

		    String snumber = Long.toString(number);

		    // pad with "0"
		    String mask = "000000000000";
		    DecimalFormat df = new DecimalFormat(mask);
		    snumber = df.format(number);
		

		    // XXXnnnnnnnnn 
		    int billions = Integer.parseInt(snumber.substring(0,3));
		  
		    // nnnXXXnnnnnn
		    int millions  = Integer.parseInt(snumber.substring(3,6)); 
		    
		    // nnnnnnXXXnnn
		    int hundredThousands = Integer.parseInt(snumber.substring(6,9)); 
		  
		    // nnnnnnnnnXXX
		    int thousands = Integer.parseInt(snumber.substring(9,12));   
		   

		    String tradBillions;
		    switch (billions) {
		    case 0:
		      tradBillions = "";
		      break;
		    case 1 :
		      tradBillions = convertLessThanOneThousand(billions) 
		      + " billion ";
		      break;
		    default :
		      tradBillions = convertLessThanOneThousand(billions) 
		      + " billion ";
		    }
		    String result =  tradBillions;

	
		    
		    String tradMillions;
		    switch (millions) {
		    case 0:
		      tradMillions = "";
		      break;
		    case 1 :
		      tradMillions = convertLessThanOneThousand(millions) 
		      + " million ";
		      break;
		    default :
		      tradMillions = convertLessThanOneThousand(millions) 
		      + " million ";
		    }
		    result =  result + tradMillions;


		    String tradHundredThousands;
		    switch (hundredThousands) {
		    case 0:
		      tradHundredThousands = "";
		      break;
		    case 1 :
		      tradHundredThousands = "one thousand ";
		      break;
		    default :
		      tradHundredThousands = convertLessThanOneThousand(hundredThousands) 
		      + " thousand ";
		    }
		    result =  result + tradHundredThousands;

		
		    String tradThousand;
		    tradThousand = convertLessThanOneThousand(thousands);
		    result =  result + tradThousand;
		    

		    // remove extra spaces!
		    return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
		  }

	public static void main(String args[])
	{
		Scanner kb = new Scanner(System.in);
		String s1;
		System.out.println("Please enter the name on the check (First Last) : ");
		s1 = kb.nextLine();
		System.out.println("Please enter the amount for the check (as a float) : ");
		float f1 = kb.nextFloat();
		if(f1 < 0)
			System.out.println("Invalid amount : " + f1);
		System.out.println("\n------------------------------------------------------");
		System.out.println("\nPay to : " + s1 + "\t\t\t\t$" + f1);
		long value = (long)f1;
		  
	           String cents = new Float(f1).toString(); 
	           String [] cents1=cents.split("[.]");
	         
	    
		 System.out.println(Asn8.convert(value)+"  "+"dollars"+"  "+"and" +" "+cents1[1]+" "+ "cetns");
	}
	
}
commented: Don't just hand students completed code for their assignment. That's not how things are done here. -3

Yes good answer....Cnt do anther easy way with short......

Thank you so much for the help. Everything makes a lot more sense now. I really appreciate it. :]

Thats gr8 :)

Sir..
May you please help me how to create a syntax for triful looping using java language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.