Using a non-constant expression in a switch statement? - java

I'm quite new to java and have created a Java class that creates a deck of cards, and assigns them a suite, a name, a value and an ID. The problem is, there are 52 different cards that require an ID, and I've been using switch statements to assign the name, value and suite. However, if I were to do this for the card ID, I would need 52 lines of case statements, which is far too many.
public class Deck {
private Card[] cards;
public Deck() {
String suit = null;
String name = null;
int cardID=0;
int value = 0;
cards = new Card[52];
int arrayID=0;
for (int i=1; i<=4; i++){ //number of suites
for (int j=1; j <= 13; j++){ //number of card types
for (int k=1; k==52; k++){ //number of cards
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 11; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
switch (k){
case k: cardID=k; break; //"Case expressions must be constant expressions"
}
Card card = new Card (cardID, name, suit, value);
cards[arrayID] = card;
arrayID++;
}
}
}
}
public void printDeck(){
System.out.println(Arrays.toString(cards));
}
}
I may be doing this whole thing wrong, so are there any other ways I could assign a unique ID to the card without using a switch statement?

There's no point in this switch statement :
switch (k){
case k: cardID=k; break; //"Case expressions must be constant expressions"
}
Just write :
cardID = k;

You could use enum and you could use ordinal value to get each count automatically and you could have property like value in your case and you could do it like:
public enum Cards {
ACE(11), TWO(..), .. JACK(....;
int value;
public int getValue() {return value;}
}
System.out.println(CARDS.ACE.ordinal() + 1);
System.out.println(CARDS.ACE.getValue());
Output:
1
11

You don't need the loop or the switch statement for the cardID. You can synthesize it from i and j
for (int i=1; i<=4; i++){ //number of suites
for (int j=1; j <= 13; j++){ //number of card types
cardId = (i - 1) * 13 + j; // <<< synthesize cardID like this
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 11; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
Card card = new Card (cardID, name, suit, value);
cards[arrayID] = card;
arrayID++;
}
}

What I would do is instead:
Create a class card:
class Card {
private String suit;
private String name;
private int ID;
public Card(String suit, String name, int ID) {
this.suit = suit
(same for name & ID)}}
Then, in your Deck class, you can just have a ArrayList that contains all your card, with some method to add the cards in your deck:
s
class Deck {
private ArrayList<Cards> myDeck = new ArrayList<Cards>();
public void addCards(Card myCard) {
this.myDeck.add(myCard); } }
EDIT: Forgot the variable type in addCards

Related

Main method cannot refer to non-static variable [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 2 years ago.
in my public class card, if I do not declare this class as static the program will not run, and all my new card objects will return the following error:
non-static variable this cannot be referenced from a static context
card card1 = new card();
Firstly, my professor has told me that the class should not be static, but has not explained why.
Secondly, I do not understand why the main method (I know it's static) cannot reference a non-static variable.
I think I'm not understanding something fundamental here, but I need to understand what's going on here before moving on.
I've found this definition:
the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type.
I have really no clue what that means. A particular member, is that referring to an object? an instance of that type? also sounds like its referring to an object that belongs to a specific class. To me something that declared static is private, and only other classes that are declared static can operate with each other - is that right? If so, why not just use the word 'private'.
The code:
import java.util.Random;
public class playing_cards {
public class card // sets up class
{
int face = 0;
int suit = 0;
String faceText;
String faceValue;
String suitValue;
String suitText;
public card() // card constructor, initially generates card number and suit
{
int face = 0;
int suit = 0;
Random generator = new Random();
face = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}
public int getFace() // sets up card face value
{
Random generator = new Random();
face = generator.nextInt(13) + 1;
return face;
}
public int getSuit() // sets up card suit value
{
Random generator = new Random();
suit = generator.nextInt(4) + 1;
return suit;
}
public String getFacetext() // gets card numeric value textually
{
switch(face)
{
case 1:
faceText = "one";
break;
case 2:
faceText = "two";
break;
case 3:
faceText = "three";
break;
case 4:
faceText = "four";
break;
case 5:
faceText = "five";
break;
case 6:
faceText = "six";
break;
case 7:
faceText = "seven";
break;
case 8:
faceText = "eight";
break;
case 9:
faceText = "nine";
break;
case 10:
faceText = "ten";
break;
case 11:
faceText = "eleven";
break;
case 12:
faceText = "twelve";
break;
case 13:
faceText = "thirtee";
break;
}
return faceText;
}
public String getSuittext() // gets card's suit value
{
switch (suit)
{ case 1:
suitText = "hearts";
break;
case 2:
suitText = "spades";
break;
case 3:
suitText = "diamonds";
break;
case 4:
suitText = "clubs";
break;
}
return suitText;
}
public String setCard(int face, int suit) // sets the card to passed through toString
{
switch (face)
{
case 1:
faceValue = "ace";
break;
case 2:
faceValue = "two";
break;
case 3:
faceValue = "three";
break;
case 4:
faceValue = "four";
break;
case 5:
faceValue = "five";
break;
case 6:
faceValue = "six";
break;
case 7:
faceValue = "seven";
break;
case 8:
faceValue = "eight";
break;
case 9:
faceValue = "nine";
break;
case 10:
faceValue = "ten";
break;
case 11:
faceValue = "jack";
break;
case 12:
faceValue = "queen";
break;
case 13:
faceValue = "king";
break;
}
switch (suit)
{
case 1:
suitValue = "hearts";
break;
case 2:
suitValue = "spades";
break;
case 3:
suitValue = "diamonds";
break;
case 4:
suitValue = "clubs";
break;
}
return suitValue;
}
public String toString()
{
String result = faceValue + " of " + suitValue;
return result;
}
}
public static void main(String[] args) {
card card1 = new card();
card card2 = new card();
card card3 = new card();
card card4 = new card();
card card5 = new card();
card card6 = new card();
card card7 = new card();
card card8 = new card();
card card9 = new card();
card card0= new card();
card1.setCard(card1.getFace(), card1.getSuit()); // sets each cards numeric value and suit and passes through toString
System.out.println(card1.toString());
card2.setCard(card2.getFace(), card2.getSuit());
System.out.println(card2.toString());
card3.setCard(card3.getFace(), card3.getSuit());
System.out.println(card3.toString());
card4.setCard(card4.getFace(), card4.getSuit());
System.out.println(card4.toString());
card5.setCard(card5.getFace(), card5.getSuit());
System.out.println(card5.toString());
System.out.println("");
card6.setCard(1, 1);
System.out.println(card6.toString());
card7.setCard(13,4);
System.out.println(card7.toString());
card8.setCard(15,5);
System.out.println(card8.toString());
card9.setCard(2,2);
System.out.println(card9.toString());
card0.setCard(3,2);
System.out.println(card0.toString());
System.out.println("");
System.out.println("Card one's suit is " + card1.getSuittext());
System.out.println("Card one's textual face value is " + card1.getFacetext());
}
}
You have to separate Card in a new class.
Create a class named Card.java:
import java.util.Random;
public class Card {
int face = 0;
int suit = 0;
String faceText;
String faceValue;
String suitValue;
String suitText;
public Card() // card constructor, initially generates card number and suit
{
int face = 0;
int suit = 0;
Random generator = new Random();
face = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}
public int getFace() // sets up card face value
{
Random generator = new Random();
face = generator.nextInt(13) + 1;
return face;
}
public int getSuit() // sets up card suit value
{
Random generator = new Random();
suit = generator.nextInt(4) + 1;
return suit;
}
public String getFacetext() // gets card numeric value textually
{
switch(face)
{
case 1:
faceText = "one";
break;
case 2:
faceText = "two";
break;
case 3:
faceText = "three";
break;
case 4:
faceText = "four";
break;
case 5:
faceText = "five";
break;
case 6:
faceText = "six";
break;
case 7:
faceText = "seven";
break;
case 8:
faceText = "eight";
break;
case 9:
faceText = "nine";
break;
case 10:
faceText = "ten";
break;
case 11:
faceText = "eleven";
break;
case 12:
faceText = "twelve";
break;
case 13:
faceText = "thirtee";
break;
}
return faceText;
}
public String getSuittext() // gets card's suit value
{
switch (suit)
{ case 1:
suitText = "hearts";
break;
case 2:
suitText = "spades";
break;
case 3:
suitText = "diamonds";
break;
case 4:
suitText = "clubs";
break;
}
return suitText;
}
public String setCard(int face, int suit) // sets the card to passed through toString
{
switch (face)
{
case 1:
faceValue = "ace";
break;
case 2:
faceValue = "two";
break;
case 3:
faceValue = "three";
break;
case 4:
faceValue = "four";
break;
case 5:
faceValue = "five";
break;
case 6:
faceValue = "six";
break;
case 7:
faceValue = "seven";
break;
case 8:
faceValue = "eight";
break;
case 9:
faceValue = "nine";
break;
case 10:
faceValue = "ten";
break;
case 11:
faceValue = "jack";
break;
case 12:
faceValue = "queen";
break;
case 13:
faceValue = "king";
break;
}
switch (suit)
{
case 1:
suitValue = "hearts";
break;
case 2:
suitValue = "spades";
break;
case 3:
suitValue = "diamonds";
break;
case 4:
suitValue = "clubs";
break;
}
return suitValue;
}
public String toString()
{
String result = faceValue + " of " + suitValue;
return result;
}
}
Then,create (or use) the Main.java class and write:
public class Main {
public static void main(String[] args) {
Card card1 = new Card();
Card card2 = new Card();
Card card3 = new Card();
Card card4 = new Card();
Card card5 = new Card();
Card card6 = new Card();
Card card7 = new Card();
Card card8 = new Card();
Card card9 = new Card();
Card card0= new Card();
card1.setCard(card1.getFace(), card1.getSuit()); // sets each cards numeric value and suit and passes through toString
System.out.println(card1.toString());
card2.setCard(card2.getFace(), card2.getSuit());
System.out.println(card2.toString());
card3.setCard(card3.getFace(), card3.getSuit());
System.out.println(card3.toString());
card4.setCard(card4.getFace(), card4.getSuit());
System.out.println(card4.toString());
card5.setCard(card5.getFace(), card5.getSuit());
System.out.println(card5.toString());
System.out.println("");
card6.setCard(1, 1);
System.out.println(card6.toString());
card7.setCard(13,4);
System.out.println(card7.toString());
card8.setCard(15,5);
System.out.println(card8.toString());
card9.setCard(2,2);
System.out.println(card9.toString());
card0.setCard(3,2);
System.out.println(card0.toString());
System.out.println("");
System.out.println("Card one's suit is " + card1.getSuittext());
System.out.println("Card one's textual face value is " + card1.getFacetext());
}
}
Your Card class has some strange methods that I didn't understand why you did like this. In general, I corrected some syntax conventions that you didn't respect (like the Camel Case). So, I will not criticize because you are still a student. Good lucky!
As the error says: You cannot refer to non-static variable in static method.
The easiest solution for you would be making another class in different file to which you will move your main method. You can also extract the card class. For example:
PlayingCards class:
public class PlayingCards {
public static void main(String[] args) {
card card1 = new card();
card card2 = new card();
card card3 = new card();
card card4 = new card();
card card5 = new card();
card card6 = new card();
card card7 = new card();
card card8 = new card();
card card9 = new card();
card card0= new card();
card1.setCard(card1.getFace(), card1.getSuit()); // sets each cards numeric value and suit and passes through toString
System.out.println(card1.toString());
card2.setCard(card2.getFace(), card2.getSuit());
System.out.println(card2.toString());
card3.setCard(card3.getFace(), card3.getSuit());
System.out.println(card3.toString());
card4.setCard(card4.getFace(), card4.getSuit());
System.out.println(card4.toString());
card5.setCard(card5.getFace(), card5.getSuit());
System.out.println(card5.toString());
System.out.println("");
card6.setCard(1, 1);
System.out.println(card6.toString());
card7.setCard(13,4);
System.out.println(card7.toString());
card8.setCard(15,5);
System.out.println(card8.toString());
card9.setCard(2,2);
System.out.println(card9.toString());
card0.setCard(3,2);
System.out.println(card0.toString());
System.out.println("");
System.out.println("Card one's suit is " + card1.getSuittext());
System.out.println("Card one's textual face value is " + card1.getFacetext());
}
}
Card class:
public class card // sets up class
{
int face = 0;
int suit = 0;
String faceText;
String faceValue;
String suitValue;
String suitText;
public card() // card constructor, initially generates card number and suit
{
int face = 0;
int suit = 0;
Random generator = new Random();
face = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}
public int getFace() // sets up card face value
{
Random generator = new Random();
face = generator.nextInt(13) + 1;
return face;
}
public int getSuit() // sets up card suit value
{
Random generator = new Random();
suit = generator.nextInt(4) + 1;
return suit;
}
public String getFacetext() // gets card numeric value textually
{
switch(face)
{
case 1:
faceText = "one";
break;
case 2:
faceText = "two";
break;
case 3:
faceText = "three";
break;
case 4:
faceText = "four";
break;
case 5:
faceText = "five";
break;
case 6:
faceText = "six";
break;
case 7:
faceText = "seven";
break;
case 8:
faceText = "eight";
break;
case 9:
faceText = "nine";
break;
case 10:
faceText = "ten";
break;
case 11:
faceText = "eleven";
break;
case 12:
faceText = "twelve";
break;
case 13:
faceText = "thirtee";
break;
}
return faceText;
}
public String getSuittext() // gets card's suit value
{
switch (suit)
{ case 1:
suitText = "hearts";
break;
case 2:
suitText = "spades";
break;
case 3:
suitText = "diamonds";
break;
case 4:
suitText = "clubs";
break;
}
return suitText;
}
public String setCard(int face, int suit) // sets the card to passed through toString
{
switch (face)
{
case 1:
faceValue = "ace";
break;
case 2:
faceValue = "two";
break;
case 3:
faceValue = "three";
break;
case 4:
faceValue = "four";
break;
case 5:
faceValue = "five";
break;
case 6:
faceValue = "six";
break;
case 7:
faceValue = "seven";
break;
case 8:
faceValue = "eight";
break;
case 9:
faceValue = "nine";
break;
case 10:
faceValue = "ten";
break;
case 11:
faceValue = "jack";
break;
case 12:
faceValue = "queen";
break;
case 13:
faceValue = "king";
break;
}
switch (suit)
{
case 1:
suitValue = "hearts";
break;
case 2:
suitValue = "spades";
break;
case 3:
suitValue = "diamonds";
break;
case 4:
suitValue = "clubs";
break;
}
return suitValue;
}
public String toString()
{
String result = faceValue + " of " + suitValue;
return result;
}

Java - Sharing int values between cases in a switch statment

I'm new to java and not sure how do I share values between cases in a switch statement? When I try to use a variable which i created in the previous case it tells me "variable might not have been initialized"
Code:
case 6:
String stringCopy = stringInput;
String lowerCase = stringCopy.toLowerCase();
int vowelCount = 0;
int stringLength = lowerCase.length();
for (int i = 0; i <= stringLength - 1; ++i){
switch(stringInput.charAt(i)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelCount++;
}
System.out.println(vowelCount);
break;
}
case 7:
int noofConstants = 0;
noofConstants = (stringLength - vowelCount);
Declare and initialize value before the switch statement.
int value = 0;
switch (key) {
case 3:
value = 1 + 1;
break;
case 4:
value = 1;
break;
you cannot access a variable that you initialize in a separate code block which is case .
declare it outside/before the code block
int stringLength = 0;
switch(){
case 6:
stringLength = 1;
break;
case 7:
stringLength = 2;
break;
}

could use some help on this decode(char c) method

The program is to write a calss PhoneNumber.java
I understand that I am supposed to test if the string is a digit or a letter and then if it is a letter its supposed to be decoded by decode(char c);
However, I dont think char c should be in between the ( ) If any one has suggestions thatd be great thanks!! The toString is left unreturned intentionally because i have not gotten that far in the program yet. Also, have to keep it in the case 'A' format Thanks
public class PhoneNumber {
private int areacode;
private int number;
private int ext;
PhoneNumber() {
areacode = 0;
number = 0;
ext = 0;
}
PhoneNumber(int newnumber) {
areacode = 216;
number = newnumber;
ext = 0;
}
PhoneNumber(int newarea, int newnumber, int newext) {
areacode = newarea;
number = newnumber;
ext = newext;
}
PhoneNumber(String newnumber) {
String areacode = str[0];
String number = str[1];
String[] str = newnumber.split("-");
String[] number = newnumber;
boolean b1, b2;
int i = 0;
int place = 0;
for (int x: newnumber){
newnumber.charAt[i] = place;
b1 = Character.isDigit(place);
if (b1 == true){
number = place;
i++;
} else {
b2 = Character.isLetter(place);
} if (b2 == true) {
number = decode(place);
i++;
} else {
System.out.print("invalid phone number!");
}
}
System.out.print(areacode.concat(number));
return newnumber;
}
private String decode(place) {
switch (c) {
case 'A': case 'B': case 'C': return "2";
case 'D': case 'E': case 'F': return "3";
case 'G': case 'H': case 'I': return "4";
case 'J': case 'K': case 'L': return "5";
case 'M': case 'N': case 'O': return "6";
case 'P': case 'Q': case 'R': case 'S': return "7";
case 'T': case 'U': case 'V': return "8";
case 'W': case 'X': case 'Y': case 'z': return "9";
default: return "";
}
}
public boolean equals(PhoneNumber pn) {
}
public String toString() {
}
}
G:\CIS260\Assignments>javac PhoneNumber.java
PhoneNumber.java:53: error: <identifier> expected
private String decode(place) {
^
1 error
In the constructor, you need to declare the array before you put things in it. You also can't say String[] number = newnumber because number is a String[] and newnumber is a String. equals() and toString() need to return something. And, to answer your question, just say
private String decode(char c){

Switch ignore case in java 7

I am doing a POC on Java 7 new features. I have code to use String in switch statement and it works. I want to make it work in case insensitive also. Is there a way to check out with ignoreCase on String?
package com.java.j7;
public class Test {
final private String _NEW ="NEW";
final private String _PENDING = "PENDING";
final private String _CLOSED = "CLOSED";
final private String _REJECTED ="REJECTED";
public static void main(String... strings){
Test j = new Test();
j.processItem("new");
j.processItem("pending");
j.processItem("closed");
j.processItem("rejected");
}
void processItem(String s){
switch (s) {
case _NEW:
System.out.println("Matched to new");
break;
case _PENDING:
System.out.println("Matched to pending");
break;
case _CLOSED:
System.out.println("Matched to closed");
break;
case _REJECTED:
System.out.println("Matched to rejected");
break;
default:
System.out.println("Not matching any more");
break;
}
}
}
no, but you could switch on s.toUpperCase(). so:
switch (s.toUpperCase()) {
//same as before
}
and while we're nitpicking, you better upper-case things in the english locale to avoid issues with turkish
using String in switch Example from oracle docs Using Strings in switch Statements
public class StringSwitchDemo {
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber;
}
switch (month.toLowerCase()) {
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}
return monthNumber;
}
public static void main(String[] args) {
String month = "August";
int returnedMonthNumber =
StringSwitchDemo.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
}
}
}
From oracle docs switch with string
The String in the switch expression is compared with the expressions associated with each case label as if the String#equals method were being used.
You can use
switch(s.toUpperCase()){
...
.....
}
See also
String#toUpperCase

Filling in array from end

I have a double split up into an int array, however the problem I am facing, is that I need it to fill in from the back, like this:
int[] arrayA = new int[5];
int[] arrayB = new int[5];
x = 23456.08;
//code here
//what im left with:
arrayA [0,2,3,4,5,6];
arrayB [0,8];
Heres how im cutting up the double:
public static void main(System args[])
{
Scanner input = new Scanner(System.in);
String answer = "";
int count = 0;
int thatone = 0;
int[] splitD = new int[5]; //main num
int[] splitDec = new int[1]; //decimal
//Enter the Number
System.out.print("Enter a number to convert: ");
double num = input.nextDouble();
// convert number to String
String convert = num + "";
// split the number
String[] split = convert.split("\\.");
String firstPart = split[0];
char[] charArray1 = firstPart.toCharArray();
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
// the decimal part
if (split.length > 1)
{
String secondPart = split[1];
char[] charArray2 = secondPart.toCharArray();
splitDec = new int[charArray2.length];
for (int i = 0; i < charArray2.length; i++)
{
// convert char to int
splitDec[i] = Character.getNumericValue(charArray2[i]);
}
}
for(int i =0; i<count;i++)
{
if(i ==0) // x00000.00 or 000x00.00
{
if(splitD[0] != "0")
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else
{
answer+= "";
}
}
else if(i ==1)//this goes with i =2 //0x0000
{
if(splitD[i] == 1)
{
switch (splitD[i+1])
{
case 9: answer+="Nineteen"; break;
case 8: answer+="Eighteen"; break;
case 7: answer+="Seventeen"; break;
case 6: answer+="Sixteen"; break;
case 5: answer+="Fifteen"; break;
case 4: answer+="Fourteen"; break;
case 3: answer+="Thirteen"; break;
case 2: answer+="Twelve"; break;
case 1: answer+="ten"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
thatone = 1;
}
else
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirty"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
}
}
else if(i == 2) //00x000
{
if(thatone ==0)
{
switch (splitD[i])
{
case 9: answer+=" Nine"; break;
case 8: answer+=" Eight"; break;
case 7: answer+=" Seven"; break;
case 6: answer+=" Six"; break;
case 5: answer+=" Five"; break;
case 4: answer+=" Four"; break;
case 3: answer+=" Three"; break;
case 2: answer+=" Two"; break;
case 1: answer+=" One"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
}
else
{
}
}
else if(i ==3)
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else if(i ==4) //0000x0
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirdy"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
answer+= " ";
}
else if(i ==5) //00000x
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
}
}
if(splitDec[0] == 0)
{
answer += " and 00/100 Dollars";
}
else if(splitDec[1] == 0)
{
answer += " and " +splitDec[0] + "0/100 Dollars";
}
else
{
answer += " and " +splitDec[0] +splitDec[1] +" /100 Dollars";
}
System.out.println(answer);
}
}
What should I do to make the array add 0 in the appropriate places?
such as if I typed in 54.00 I would get:
int[] SplitD = {0,0,0,0,5,4};
Thanks!
Instead of re-initializing the array:
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
Use the existing (with length = 5), iterating from the back:
if(charArray1.length > 5) // heavy hard-code, would prefer something better
throw new IllegalArgumentException("Maximum 5 digits, on both decimal sides.");
// watch out of ArrayIndexOutOfBoundsException
for (int i = charArray1.length - 1, j = splitD.length -1 ;
i >=0 && j >=0; i--, j--)
{
// convert char to int
splitD[j] = Character.getNumericValue(charArray1[i]);
count++;
}
Recall, int arrays are filled with zeros, at initialization.
Think creating an array of String, one String for each character (including the decimal point), would be a better approach:
String[] chars = String.valueOf(num).split("(?<=.)");
Job done.
Edit:
To use a switch, don't even split:
for (byte b : String.valueOf(num).getBytes()) {
switch(b) {
case '.':
case '1':
}
}

Categories

Resources