Whats wrong with my Code?
hi there can any one help me figure out how to convert numbers into string without using an array and a method please thank you
example of my arrayed code i dont like this
the code here is working but i want to use the other way for not using array
just like switches and if and loops only
im sorry for not using the proper tags to this code im a new comer thank you for your time to reply on this thread
i made a code here but i did'nt run what i want to output sir
Code Java:
package UnderPackage;
import java.util.Scanner;
public class NumberToWords {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int number;
int b;
String thousands ="";
String hundred ="";
String tens ="";
String firstTen ="";
String ones ="";
System.out.println("Enter a number: ");
number = input.nextInt();
int th = number/1000;
switch (th){
case 1:
thousands ="one thousand";
break;
case 2:
thousands ="two thousand";
break;
case 3:
thousands ="three thousand";
break;
case 4:
thousands ="four thousand";
break;
case 5:
thousands ="five thousand";
break;
case 6:
thousands ="six thousand";
break;
case 7:
thousands ="seven thousand";
break;
case 8:
thousands ="eight thousand";
break;
case 9:
thousands ="nine thousand";
break;
case 10:
thousands ="ten thousand";
break;
}
switch(number/100){
case 1:
hundred = "one hundred";
break;
case 2:
hundred = "two hundred";
break;
case 3:
hundred = "three hundred";
break;
case 4:
hundred = "four hundred";
break;
case 5:
hundred = "five hundred";
break;
case 6:
hundred = "six hundred";
break;
case 7:
hundred = "seven hundred";
break;
case 8:
hundred = "eight hundred";
break;
case 9:
hundred = "nine hundred";
break;
}
if(number<= 19 && number >=11){
int special = number%100;
switch (special){
case 11:
firstTen = "eleven";
break;
case 12:
firstTen = "twelve";
break;
case 13:
firstTen = "thirteen";
break;
case 14:
firstTen = "fourteen";
break;
case 15:
firstTen = "fifthteen";
break;
case 16:
firstTen = "sixteen";
break;
case 17:
firstTen = "seventeen";
break;
case 18:
firstTen = "eightteen";
break;
case 19:
firstTen = "nineteen";
break;
}
System.out.println(th + special);
}
switch (number/10){
case 1:
tens ="ten";
break;
case 2:
tens ="twenty";
break;
case 3:
tens ="thirty";
break;
case 4:
tens ="fourty";
break;
case 5:
tens ="fifty";
break;
case 6:
tens ="sixty";
break;
case 7:
tens ="seventy";
break;
case 8:
tens ="eighty";
break;
case 9:
tens ="ninety";
break;
}
if(number>0){
switch (number){
case 1:
ones ="one";
break;
case 2:
ones ="two";
break;
case 3:
ones ="three";
break;
case 4:
ones ="four";
break;
case 5:
ones ="five";
break;
case 6:
ones ="six";
break;
case 7:
ones ="seven";
break;
case 8:
ones ="eight";
break;
case 9:
ones ="nine";
break;
}
System.out.print(thousands + hundred +firstTen+ tens + ones);
}
}
}
Re: Whats wrong with my Code?
When posting code, please use the highlight tags to preserve formatting.
What does this code do? What are you confused about? Where are you stuck?
Re: Whats wrong with my Code?
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.
Re: Whats wrong with my Code?
sorry sir i got sleep . . same concept but i dont want to use array here i dont know how to convert it not using arrays and methods
Re: Whats wrong with my Code?
Quote:
convert it not using arrays and methods
Writing code that doesn't use arrays or methods is a step backwards. Is that a requirement from your instructor to show you how hard it is and how ugly a program would be without using those two techniques?
It could be done with a long chain of if/else if statements
or with a switch statement.
Re: Whats wrong with my Code?
yes sir it is. . im confusing when starting to think the logic after declaring variables and accept an input type