Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2009, 6:34 AM   #1
The_Duck
Code retard.
 
The_Duck's Avatar
 
Join Date: Mar 2009
Posts: 6
Rep Power: 0 The_Duck is on a distinguished road
Send a message via MSN to The_Duck
Noob question

Hi people. I am trying to learn Java and I tried to make a simple program. Unfortunately I have been unsuccessful in doing this and I am constantly receiving errors. I have corrected it to the best of my ability.
If you have any advice on how to correct this, please post, it would be greatly appreciated.


   import javax.swing.*;
    public class NewAttempt {
   
       public static void main(String[] args) {
      
         int balance;
         int withdraw;
      
         String other;
         String answer;
      	
         balance =  1000;
         answer = JOptionPane.showInputDialog ("Please enter the number linked to the prefered transaction.  1 : balance.  2 : withdraw.");
         

         if (answer.equals("1"))

         {JOptionPane.showMessageDialog (null, "Your current balance is "+(balance)+" USD");}
         else if (answer.equals("2"))
      	{withdraw = JoptionPane.showInoutDialog  ("Please enter the amount you wish to withdraw");}
			{if (withdraw > balance)
      	{JOptionPane.showMessage ("The amount you requested exceeds your balance.  Please try again.");}
      	else (withdraw < balance);
      	{JOptionPane.showMessage ("You have withdrawn: "+(withdraw)+" USD.  Your current balance is: "+(balance - withdraw)+" USD");}}
      		
      
       
      }
      }
The_Duck is offline   Reply With Quote
Old Mar 14th, 2009, 10:20 AM   #2
Weird Fishes
Expert Programmer
 
Weird Fishes's Avatar
 
Join Date: Aug 2008
Location: Waterloo
Posts: 509
Rep Power: 10 Weird Fishes will become famous soon enough
Re: Noob question

Well I copied it into my IDE(netbeans) and it highlighted all the syntax-type errors. After fixing those, it ran, but it didn't run properly do to a logic issue, I think you can fix that.
Here is the change code:
java Syntax (Toggle Plain Text)
  1. public class Main
  2. {
  3.  
  4. public static void main(String[] args)
  5. {
  6.  
  7. int balance;
  8. int withdraw = 0;
  9.  
  10. String other;
  11. String answer;
  12.  
  13. balance = 1000;
  14. answer = JOptionPane.showInputDialog("Please enter the number linked to the prefered transaction. 1 : balance. 2 : withdraw.");
  15.  
  16.  
  17. if (answer.equals("1"))
  18. {
  19. JOptionPane.showMessageDialog(null, "Your current balance is " + (balance) + " USD");
  20. } else if (answer.equals("2"))
  21. {
  22. withdraw = Integer.parseInt(JOptionPane.showInputDialog("Please enter the amount you wish to withdraw"));
  23. }
  24. if (withdraw > balance)
  25. {
  26. JOptionPane.showMessageDialog(null, "The amount you requested exceeds your balance. Please try again.");
  27. } else if (withdraw < balance)
  28. {
  29. JOptionPane.showMessageDialog(null, "You have withdrawn: " + (withdraw) + " USD. Your current balance is: " + (balance - withdraw) + " USD");
  30. }
  31.  
  32.  
  33.  
  34. }
  35. }

Some general notes:
Watch your capitalization. Do you use an IDE? most will catch those errors and point them out for you.
if/else if/else blocks:
if (something)
{
}
else if (something else)
{
}
else if (something else)
{
}
else //anything else
{
}
showMessage isn't a method. you wanted showMessageDialog.

That should get you started enough to finnish this.

Last edited by Weird Fishes; Mar 14th, 2009 at 10:43 AM.
Weird Fishes is offline   Reply With Quote
Old Mar 14th, 2009, 10:53 AM   #3
The_Duck
Code retard.
 
The_Duck's Avatar
 
Join Date: Mar 2009
Posts: 6
Rep Power: 0 The_Duck is on a distinguished road
Send a message via MSN to The_Duck
Re: Noob question

Done.
Was not that difficult. I think that I cannot use my IDE. It is very unhelpful.

Thanks man.
The_Duck is offline   Reply With Quote
Old Mar 14th, 2009, 3:41 PM   #4
exception
Software & Web Developer
 
exception's Avatar
 
Join Date: Oct 2008
Location: Philippines
Posts: 173
Rep Power: 10 exception is on a distinguished road
Send a message via Yahoo to exception
Re: Noob question

i think it would be an exception.. NumberFormatException or TypeMistmatchException if you dont parse it.. string cannot be compared to int (vice-versa), so you must parse it first to its same data type..

goodluck!
__________________
|Knowledge alone is not POWER, Applied knowledge is POWER.
exception is offline   Reply With Quote
Old Mar 16th, 2009, 1:52 PM   #5
The_Duck
Code retard.
 
The_Duck's Avatar
 
Join Date: Mar 2009
Posts: 6
Rep Power: 0 The_Duck is on a distinguished road
Send a message via MSN to The_Duck
Re: Noob question

Well I decided to step it up a level and here is my code:

   import javax.swing.*;  
   
    public class YearstoDays {  
   
       public static void main(String[] args) {  
       
         int grr;
         int mine;
         int hello;
         int fivenine;
         int fiveninetwo;
         int seventy;
         String hours;
      	
      
         fivenine = Integer.parseInt (JOptionPane.showInputDialog ("Please enter your age."));
         seventy = (fivenine + 1);
         fiveninetwo = (fivenine * 365 + 365);
         grr = (seventy * 8766);
         hello = (seventy * 525948);
         hours = JOptionPane.showInputDialog ("Would you like your answer in minutes, hours or days?   1: minutes.   2: hours.   3: days");
         
      	if (hours.equals ("1")); {
      	 JOptionPane.showMessageDialog (null, "On your "+(seventy)+"th birthday,  you will have lived for "+(hello)+" minutes");

      	}
         
      	
         else if (hours.equals("2")) {
         JOptionPane.showMessageDialog (null, "On your "+(seventy)+"th birthday,  you will have lived for "+(grr)+" hours");

         }
         
         else if (hours.equals ("3"))  {
         
            JOptionPane.showMessageDialog (null, "On your "+(seventy)+"th birthday,  you will have lived for "+(fiveninetwo)+" days");
         }
      }
   }

Here is my error message:

YearstoDays.java:30: 'else' without 'if'
         else if (hours.equals("2")) {
         ^
1 error

Please advise me. Any help would be appreciated.
I would like to know what I have done wrong so that I can avoid making this mistake in the future.

Toggle to normal view. The code option scrambles it.
The_Duck is offline   Reply With Quote
Old Mar 16th, 2009, 1:54 PM   #6
Weird Fishes
Expert Programmer
 
Weird Fishes's Avatar
 
Join Date: Aug 2008
Location: Waterloo
Posts: 509
Rep Power: 10 Weird Fishes will become famous soon enough
Re: Noob question

if (hours.equals ("1"));

Take out the semicolon. You want the whole block in brackets to execute an the if statement, but as written that statement doesn't do anything.
Weird Fishes is offline   Reply With Quote
Old Mar 16th, 2009, 2:09 PM   #7
The_Duck
Code retard.
 
The_Duck's Avatar
 
Join Date: Mar 2009
Posts: 6
Rep Power: 0 The_Duck is on a distinguished road
Send a message via MSN to The_Duck
Re: Noob question

I forgot about that. Thank you.
The_Duck is offline   Reply With Quote
Old Mar 17th, 2009, 12:40 AM   #8
zhitz18
Programmer
 
Join Date: Sep 2008
Posts: 73
Rep Power: 0 zhitz18 is on a distinguished road
Re: Noob question

Quote:
Originally Posted by exception View Post
i think it would be an exception.. NumberFormatException or TypeMistmatchException if you dont parse it.. string cannot be compared to int (vice-versa), so you must parse it first to its same data type..

goodluck!
That's not entirely true. Number values represented by strings aren't stored in memory as their respective number data type, but rather a string (array of chars). So, like The_Duck did in his code, you can use the string method .equals to compare "numbers", which, essentially is just looking for the same sequence of characters between the two strings.

It's definitely not a bad idea to parse a string into its number data type though. From my experience, I've sometimes forgot that I was still working with the string of a number and not the actual number data type, and vice versa, sometimes causing some head scratchers that are actually really obvious fixes
zhitz18 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob question. Why isnt my "Hello World" program working? Chel C++ 24 Oct 24th, 2010 9:42 PM
sort of a noob question... Varrickmana C++ 5 May 26th, 2006 11:00 AM
A noob question on editing variables MR.T Python 13 Feb 28th, 2006 5:13 PM
How to post a question nnxion C 0 Jun 3rd, 2005 9:55 AM
noob question crazykid48x Other Programming Languages 8 May 23rd, 2005 3:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC