954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help creating an array

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

Aepexx
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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..

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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");
	}
	
}
abhishek20
Newbie Poster
17 posts since Feb 2008
Reputation Points: 8
Solved Threads: 1
 

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

Sadun89
Junior Poster
166 posts since Jan 2011
Reputation Points: -6
Solved Threads: 5
 

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

Aepexx
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Thats gr8 :)

abhishek20
Newbie Poster
17 posts since Feb 2008
Reputation Points: 8
Solved Threads: 1
 

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

FRGT/10
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: