Hey guys,
So I'm on the second last question of my latest homework assignment and I can not figure out what is wrong. Basically it asks you to input a date, name, amount and then prints out a check, with a word form of the name on the bottom. Well, I'm just trying to get the word form working and im trying to compare characters and its not working, I'm not sure how else to do this! I tried .equals but that failed miserably, with errors.

import java.util.*;
public class CH9Q7
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int month, year, day;
        String name;
        double amount;
        
        /*
        System.out.println("Please enter the month\n1.January\n2.February\n3.March\n4.April\n5.May\n6.June\n7.July\n8.August\n9.September\n10.October\n11.November\n12.December\n Enter the number of the month(1-12)");
        month=keyboard.nextInt();
        System.out.println("Please enter the day:");
        day=keyboard.nextInt();
        System.out.println("Please enter the year:");
        year=keyboard.nextInt();
        keyboard.nextLine();
        System.out.println("Enter the name of the person:");
        name=keyboard.nextLine();
        System.out.println("Please enter the amount the cheque is for: $");
        amount = keyboard.nextDouble();
        */
        

        month=5;
        day=25;
        year=2008;
        name="Joe Smith";
        amount = 1920.85;
        
        System.out.println("\t\t\t\t\t\t"+month+"/"+day+"/"+year);
        System.out.println("Pay to the Order of:\t"+name+"\t\t$"+amount);
        System.out.println(amountString(amount));
    }
    public static String amountString(double amount)
    {
        int count=0;
        String str = Double.toString(amount);
        String wordNums="!Unkown!";
        if(str.length()>6)
        {
            
            wordNums=singularName(str.charAt(0))+"thousand";
            System.out.println(str.charAt(0));
        }
        return wordNums;
    }
    public static String pluralName(char num)
    {
        String pluralName;
        if(num=='1')
            pluralName="Ten";
        if(num=='2')
            pluralName="Twenty";
        if(num=='3')
            pluralName="Thirty";
        if(num=='4')
            pluralName="Forty";
        if(num=='5')
            pluralName="Fifty";
        if(num=='6')
            pluralName="Sixty";
        if(num=='7')
            pluralName="Seventy";
        if(num=='8')
            pluralName="Eighty";
        if(num=='9')
            pluralName="Ninety";
        else
            pluralName="Error /w Plural Method";
        return pluralName;
    }
    public static String singularName(char num)
    {
        String singleName;
        if(num=='1');
            singleName="One";
        if(num=='2')
            singleName="Two";
        if(num=='3')
            singleName="Three";
        if(num=='4')
            singleName="Four";
        if(num=='5')
            singleName="Five";
        if(num=='6')
            singleName="Six";
        if(num=='7')
            singleName="Seven";
        if(num=='8')
            singleName="Eight";
        if(num=='9')
            singleName="Nine";
        else
            singleName="Error /w Singular Method";
        return singleName;
    }
}

All ideas are appreciated! I have been working on this question for a couple of hours now and I can't get it!

Recommended Answers

All 11 Replies

if(num=='8')
    pluralName="Eighty";
if(num=='9')
    pluralName="Ninety";
else
    pluralName="Error /w Plural Method";

this will always return "Error /w Plural Method", unless the value for num == '9'
what you want to do (and not only for the '8' check) is using nested if's

if(num=='8'){
    pluralName="Eighty";
}
else{
    if(num=='9'){
        pluralName="Ninety";
    }
    else{
        pluralName="Error /w Plural Method";
    }
}

Well I guess your actual problem has been solved by stultuske, But still I would like to mention a few things by looking at your code :-

Firstly Java is an object oriented language and its true power is manifested in that only, but you have basically pulled it down to the level of a Structured Programming Language.

Secondly instead of those big line of "if" statements have you ever considered using "switch case", It would solve the problem which stultuske is mentioning about, if you need help in that too then just go here.

And finally I also remember you had asked for a better IDE for Java as compared to BlueJ. Honestly judging from your code here, you are just starting out with Java so stick with either BlueJ or JCreator Lite do not try to start using Netbeans or Eclipse, you will simply be lost there.

if(num=='8')
    pluralName="Eighty";
if(num=='9')
    pluralName="Ninety";
else
    pluralName="Error /w Plural Method";

this will always return "Error /w Plural Method", unless the value for num == '9'
what you want to do (and not only for the '8' check) is using nested if's

if(num=='8'){
    pluralName="Eighty";
}
else{
    if(num=='9'){
        pluralName="Ninety";
    }
    else{
        pluralName="Error /w Plural Method";
    }
}

This is a weak example of actually implementing it the right way, it should have been done using switch-case statement and using break as correctly pointed out by the above post. Also this method is far unoptimized since it would check for every 'if' till the num=='8' even if the very first 'if' statement is true. I don't think you want your program to check every condition on every occassion even if the very first condition is true in many of those occassions. ;)

This is a weak example of actually implementing it the right way, it should have been done using switch-case statement and using break as correctly pointed out by the above post !!!

I never claimed to have delivered the most perfect code. I just based myself on his code and explained where he went wrong.
It's not my job to re-write his code to the most efficiënt form, it's his own task to find out whether or not there is a better way.

I never claimed to have delivered the most perfect code. I just based myself on his code and explained where he went wrong.
It's not my job to re-write his code to the most efficiënt form, it's his own task to find out whether or not there is a better way.

But atleast you can guide someone correctly if you are guiding someone at all. Do not guide someone wrongly.

And I hope now you get the answer for the extra '{' that you were commenting upon in the other thread. :D

Also this method is far unoptimized since it would check for every 'if' till the num=='8' even if the very first 'if' statement is true.

what you want to do (and not only for the '8' check) is using nested if's

ehm... no, no it wouldn't, unless off course, you'd just copy paste my code without reading the rest of the post.

And I hope now you get the answer for the extra '{' that you were commenting upon in the other thread.

no, I didn't, but you just answered it yourself:

But atleast you can guide someone correctly

no, I didn't, but you just answered it yourself:

I'had thought you'd give some intelligent answer just as I gave to yours, but instead you are just copy-pasting my answers after I have put them in your face. You know it's so easy to appear intelligent when parroting intelligent people. ;)

commented: This is not constructive here. -3

And I'm not going to post any more on this because of some other things that I believe.
No matter you win or lose a rat race you 'd still be a rat.
You can continue as long as you want, I'm here to do other more meaningful things.

Tsk tsk, let's not get into unproductive discussion here. I am pretty sure you both understand that there is no point in nitpicking each others' post to the point that it turns into name calling.

If you have something to say on topic as per PhiberOptik request please do so, however personal attacks will not be tolerated.

I got it! Thanks guys, and I switched over to a switch statement, and it works beautifully.

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.