We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,313 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

java stuff

this is the program i have to write
you have to write a program using java language which will realize the following scenario
project scenario:when you run your program it will display a welcome messege (you choose ur welcome messege). then program will ask to input a number beetween 1-9999, once u (user) insert a number the program will output the english words corresponding to the inserted number then ask the user (display the messege),"will u want another try?" if user inser yes then program will ask again to insert a number beetween 1-9999. once u (user) insert another number it will again output in words and this process continues until the user response "No" for the question."will u want another try?". once user inserts "no" program will display "goodbye" and ends the program.


i have tried writing it
but it comes out weird

import javax.swing.*;
public class blahblahblah 
	{
	public static void main (String []args){
	}
boolean cc=true;


String []name= {
		"Zero"*10,"One"*10,"Two"*10,"Three"*10,"Four"*10,"Five"*10, 
		"Six"*10, "Seven"*10, "Eight"*10, "Nine"*10, "Ten"*10,"Eleven"*10,
		"Twelve"*10,"Thirteen"*10,"Fourteen"*10,"Fifteen"*10,"Sixteen"*10,
		"Seventeen"*10,"Eighteen"*10,"Nineteen"*10,"Twenty"*10,"Thirty"*10,
		"Fourty"*10,"Sixty"*10,"Seventy"*10,"Eighty"*10,"Ninety"*10,"One hundred"*10,
		"Two Hundred"*10,"Three Hundred"*10,"Four Hundred"*10,"Five Hundred"*10,
        "Six Hundred"*10,"Seven Hundred"*10,"Eight Hundred"*10"Nine Hundred"*10,
        "One Thousand"*10,"Two Thousand"*10,"Three Thousand"*10,"Four Thousand"*10,
		"Five Thousaand"*10,"Six Thousand"*10,"Seven Thousand"*10,"Eight Thousand"*10,
		"Nine Thousand"*10,"Ten Thousand"*10};
		
int i=0;
name[i]=JOptionPane.showMessageDialog(null,"Welcome to Java Program!!")
String st1=JOptionPane.showInputDialog("Insert a Number between 0-9999");{
i=Integer.parseInt(st1);
System.out.println ("0-9999");
String st2=JOptionPane.showInputDialog ("Do you want another try?");
	
	
	if (st1.equals ("yes"))
	{
	}
else
{

cc=false;
{
}
}
6
Contributors
14
Replies
2 Weeks
Discussion Span
1 Year Ago
Last Updated
15
Views
Jenna1994
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

it comes out weird

Can you explain what happens and perhaps copy and paste here the programs output.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Also don't forget to wrap your codes in code tags and properly indent it cause it's an eyesore

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

Can you explain what happens and perhaps copy and paste here the programs output.

it doesnt print anything it prints errror

Jenna1994
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

it doesnt print anything it prints errror

post those errors here

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

Please copy and paste here what ever is printed. All of it.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

no the problem is that the program doesnt run when i print thats the error

Jenna1994
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

the program doesnt run

Can you explain what the program does when you try to execute it?
How are you trying to execute it?

Does the program compile? Do you get compiler errors?
Please copy and paste here the compiler errors.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

it does run but:

public static void main (String []args){
	}

your main method is empty.

stultuske
Industrious Poster
4,380 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

it does run but:

public static void main (String []args){
	}

your main method is empty.

i tried doing this it does not work it gives me a red x
i know how to start the program and do everythin i just want to know how i can put the number to 9000 could u explain that anyone of u

Jenna1994
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

i just want to know how i can put the number to 9000

Please explain what "put the number to 9000" means?
Do you mean to assign the value of 9000 to a variable named number?
int number = 9000;

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

1) Like Stultuske stated, your main method is empty

public static void main (String []args){
	}

2) Why would you use the * operator on a string?

String []name= {
		"Zero"*10,"One"*10,"Two"*10, ...

3) Why would you set the first String in your array to your welcoming message?

int i=0;
name[i]=JOptionPane.showMessageDialog(null,"Welcome to Java Program!!")

4) Why save the number he entered in the int "i" but never use it again?

String st1=JOptionPane.showInputDialog("Insert a Number between 0-9999");{
i=Integer.parseInt(st1);
System.out.println ("0-9999");

5) Why save the "continue?" answer in st2 then test if st1 is equal to "yes" ?

String st2=JOptionPane.showInputDialog ("Do you want another try?");
	if (st1.equals ("yes"))
	{
	}

6) There is no looping mechanism in your algorithm

7) There is no algorithm to decide which string to print according to the number saved in st1

Asking you to modify this code would be longer than re-writing it , the whole structure is wrong, heres a what it should look like :

import java.awt.Dialog;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

public class Numbers {

	static public void main(String[] args) {
		boolean bKeepGoing = true;
		String sNumber;
		int iNumber;
		int iDialogResult;
		JOptionPane.showMessageDialog(null,"Hello!", "Welcoming title!" , JOptionPane.PLAIN_MESSAGE);
		while(bKeepGoing){
			sNumber = JOptionPane.showInputDialog(null, "Enter a number between 0 and 9999 :", "Input a number!",JOptionPane.QUESTION_MESSAGE);
			try{
				iNumber = Integer.parseInt(sNumber);
				JOptionPane.showMessageDialog(null, getString(iNumber) , "Here it is!" , JOptionPane.INFORMATION_MESSAGE);
				iDialogResult = JOptionPane.showConfirmDialog(null, "Would you like to continue?", "Keep going?", JOptionPane.YES_NO_OPTION);
				if(iDialogResult == JOptionPane.NO_OPTION){
					bKeepGoing=false;
				}
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(null, "Ooops we got an error!\n" + e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
			}
		}
		JOptionPane.showMessageDialog(null, "Good bye stranger!", "Cya", JOptionPane.INFORMATION_MESSAGE);
	}
	
	public static String getString(int n){
		String s = "";
		if(n == 0){
			s += "Zero";
			return s;
		}
		if(n < 0){
			s += "Minus ";
			n *= -1;
		}
		if(n/1000 > 0){
			s+= getNumber(n/1000) + "Thousand ";
		}
		n -= (n/1000)*1000;
		if(n/100 > 0){
			s+= getNumber(n/100) + "Hundred ";
		}
		n -= (n/100)*100;

		if(n/10 > 1){
			s+= getDecade(n/10);
			 n -= (n/10)*10;
			if(n/1 > 0){
				s+= getNumber(n/1);
			}
		}
		else if(n/10 == 1){
			if(n > 13){
				s += getNumber(n-10).trim() + "teen";
			}
			else{
				switch (n) {
				case 10:
					s += "Ten";
					break;
				case 11:
					s += "Eleven";
					break;
				case 12:
					s += "Twelve";
					break;
				case 13:
					s += "Thirteen";
					break;
				}
			}
		}
		else{
			n -= (n/10)*10;
			if(n > 0){
				s+= getNumber(n);
			}
		}
		return s;
	}
	
	public static String getNumber(int n){
		String s = "";
		switch (n) {
		case 9:
			s= "Nine ";
			break;
		case 8:
			s= "Eight ";
			break;
		case 7:
			s= "Seven ";
			break;
		case 6:
			s= "Six ";
			break;
		case 5:
			s= "Five ";
			break;
		case 4:
			s= "Four ";
			break;
		case 3:
			s= "Three ";
			break;
		case 2:
			s= "Two ";
			break;
		case 1:
			s= "One ";
			break;
		}
		return s;
	}

	public static String getDecade(int n){
		String s;
		switch (n) {
		case 9:
			s= "Ninety ";
			break;
		case 8:
			s= "Eightty ";
			break;
		case 7:
			s= "Seventy ";
			break;
		case 6:
			s= "Sixty ";
			break;
		case 5:
			s= "Fifty ";
			break;
		case 4:
			s= "Fourty ";
			break;
		case 3:
			s= "Thirty ";
			break;
		case 2:
			s= "Twenty ";
			break;
		default:
			s = "";
			break;
		}
		return s;
	}
}
Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

Giving an OP the code for their program doesn't help them learn how to solve problems on their own.
See: http://www.javaprogrammingforums.com/cafe/9544-problem-spoon-feeding.html

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

@Philippe.Lahaie
1) Forum rules clearly state

Do provide evidence of having done some work yourself if posting questions from school or work assignments

which original poster ignored therefore you should not provided the code
2) Your code? Ouch what a mess that if/else statements and switch statements is something to kill for. Half of that mess can be reduced just by simple use of array or any other collection

peter_budo
Code tags enforcer
Moderator
15,791 posts since Dec 2004
Reputation Points: 2,867
Solved Threads: 944
Skill Endorsements: 50

fair enough, my mistake.

I was trying to make my post as complete as possible but i definitly understand why i shouldn't have posted the complete code and won't do that mistake twice.

as for the if statement this was done quikly it is probably not the best possible solution nor did i say it was, but it works.

Phil~

PS:I can't edit the older post i made so if any moderator feels like deleting the spoon feeding part feel free.

Philippe.Lahaie
Posting Whiz
360 posts since Oct 2007
Reputation Points: 103
Solved Threads: 54
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1098 seconds using 2.85MB