problem with a java program

Reply

Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

problem with a java program

 
0
  #1
Mar 20th, 2008
I have never posted anything in here for java so if I don'tuse the code tags right I apologize...anyways I compiled an assignment in text pad and have a ton or errors and I was wanting to know if it is something minor or I am really that stupid...thanks

Here is what the assignment says:

Write an application (PetAdvice.java) that recommends a pet for a user based on the user’s lifestyle. Prompt the user to enter whether he or she lives in an apartment, house, or dormitory (1, 2, or 3… 4 to quit) and the average number or hours the user is home per week.

Additional hints/requirements:
1. Do not use the default values for the title bar caption – see example below.
2. For your icons, use the question for input, the information “I” for output, and the error for error message dialog boxes.
3. You are required to use IF statements and case statements at some point in the program. I combined some IF statements with my case statements to come up with the pet in the getPet() method.
4. You should allow decimals for the hours input.
5. Make sure you provide reasonableness checks when necessary.
6. If the user makes an invalid entry, allow them to re-enter their data (loop back). However, you don’t need to loop back to the beginning at the end of your program. Once your user clicks the last OK, you can terminate.
7. You will be required to use multiple methods for this application (like we were doing with our Chapter 4 in-class exercises). You don’t have to use my exact methods, but here is what I used: getHousing(), getHours(), getPet(), output(), and finish().
8. Termination: program your cancel buttons to terminate in addition to terminating when your user selects 4 on the first screen.


Print your recommendation based on the following table:
Residence Hours Home Recommendations
House 18 or more Pot-bellied pig
House 10 to <18 Dog
House Fewer than 10 Snake
Apartment 10 or more Cat
Apartment Fewer than 10 Hamster
Dormitory 6 or more Fish
Dormitory Fewer than 6 Ant farm




this is what I have so far and if the code tags dont come out right please let me know

  1.  
  2. /*
  3.  
  4. Programmer: Jim Johnson
  5. Date: 3/18/08
  6. Filename: PetAdvice.java
  7. Purpose:
  8.  
  9. */
  10.  
  11. //packages to import
  12.  
  13. import javax.swing.JOptionPane;
  14.  
  15.  
  16. public class PetAdvice
  17. {
  18. public static void main(String[] args)
  19. {
  20. //declare class variables
  21. int housing;
  22. double hours;
  23. String pet;
  24.  
  25. //call method
  26. housing = getHousing();
  27. hours = getHours();
  28. pet = getPet();
  29. output();
  30. finish();
  31. }//end main()
  32.  
  33. //the output() method displays the pet recommended
  34. public static void output(String pet)
  35. {
  36. ;
  37.  
  38. //display output
  39. JOptionPane.showMessageDialog(null, "According to your requirements, I would recommend a ", + pet, "Pet Advice: Recommendation", JOptionPane.INFORMATION_MESSAGE);
  40.  
  41. }// end output
  42.  
  43.  
  44.  
  45. //the getPet() method accepts the housing and hours and returns a pet
  46. public static String getPet(String pet)
  47. {
  48. //declare method variables
  49. String pet;
  50.  
  51. switch(housing)
  52. {
  53. case 1: //pot-bellied pig
  54. if ((housing = 1) && (hours>=18))
  55. pet = "pot-bellied pig";
  56. else
  57. break;
  58. case 2: //dog
  59. if ((housing = 1) && (hours>=10) && (hours < 18))
  60. pet = "dog";
  61. break;
  62. case 3: //snake
  63. if ((housing = 1) && (hours < 10))
  64. pet = "snake";
  65. break;
  66. case 4: //cat
  67. if ((housing = 2) && (hours >= 10))
  68. pet = "cat";
  69. break;
  70. case 5: //hamster
  71. if ((housing = 2) && (hours < 10))
  72. pet = "hamster";
  73. break;
  74. case 6: //fish
  75. if ((housing = 3) && (hours >= 6))
  76. pet = fish;
  77. break;
  78. case 7: //ant farm
  79. if ((housing = 3) && (hours < 6))
  80. pet = "ant farm";
  81. break;
  82. }//end switch
  83.  
  84.  
  85. //.return value to the calling method
  86. return commission;
  87.  
  88. }//end getComm
  89.  
  90.  
  91.  
  92.  
  93. //the getHours() method asks the user to input hours and validate it
  94.  
  95. public static double getHours()
  96. {
  97. //declare method variables
  98. double hours = 0.0;
  99. boolean done = false;
  100.  
  101. //loop while not done
  102. while(!done)
  103. {
  104. String answer = JOptionPane.showInputDialog(null, "Enter the average number of hours\nyou are home per week:");
  105.  
  106. //See if the cancel buttom was clicked....this will only work if the input is brought in as a a string
  107. if(answer == null) finish();
  108.  
  109. //validate the input
  110. try
  111. {
  112. //is answer a valid double
  113. hours = Double.parseDouble(answer);
  114.  
  115. //is sales reasonable?
  116. if(hours <= 0) throw new NumberFormatException();
  117. //if the value is valid and reasonable, get out of the while loop
  118. else done = true;
  119. } //end try
  120. catch(NumberFormatException e)
  121. {
  122. JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.\nPlease enter a positive number for hours", "Error", JOptionPane. ERROR_MESSAGE);
  123.  
  124. } //end catch
  125.  
  126. } //end while
  127.  
  128.  
  129. return hours;
  130. } //end getHours()
  131.  
  132. //the getHousing() method retries a housing option from the user and validates it.
  133. public static int getHousing()
  134. {
  135. //declare method variables
  136. int housing = 0;
  137. boolean done = false;
  138.  
  139. //loop while not done
  140. while(!done)
  141. {
  142. try
  143. {
  144. String message = "Enter your type of residence:" + "\n\n1) House\n2) Apartment\n3) Dormitory\n4) Quit the Application\n";
  145.  
  146. code = Integer.parseInt(JOptionPane.showInputDialog(null,message));
  147.  
  148. //test for valid codes 1, 2, 3, or 4
  149. if(code<1 || code>4) throw new NumberFormatException();
  150. else done = true;
  151. }
  152. catch(NumberFormatException e)
  153. {
  154. JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.", "Error", JOptionPane.INFORMATION_MESSAGE);
  155. }
  156. }
  157. return code;
  158. }
  159.  
  160.  
  161. //the finish() method successfully terminates the application
  162. public static void finish()
  163. {
  164. System.exit(0);
  165. } //end finish()
  166.  
  167.  
  168. } //end class
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: problem with a java program

 
0
  #2
Mar 20th, 2008
What errors are you getting and what don't you understand about them?
It's vitally important that you learn to understand and interpret error messages.
As long as you can't, you'll never be able to successfully complete a piece of software in any language or using any tool.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: problem with a java program

 
0
  #3
Mar 21st, 2008
error messages....and by the way this is my first post in a java forum....so it is not like I am trying to get answers....as you can see I have alot of stuff in here and I am just confused because it seems like whenever I have this many errors one thing fixes most of them so if anyone can actually help me with this please let me know



F:\JAVA\PetAdvice.java:27: getPet(java.lang.String) in PetAdvice cannot be applied to ()
pet = getPet();
^
F:\JAVA\PetAdvice.java:28: output(java.lang.String) in PetAdvice cannot be applied to ()
output();
^
F:\JAVA\PetAdvice.java:38: operator + cannot be applied to java.lang.String
JOptionPane.showMessageDialog(null, "According to your requirements, I would recommend a ", + pet, "Pet Advice: Recommendation", JOptionPane.INFORMATION_MESSAGE);
^
F:\JAVA\PetAdvice.java:48: pet is already defined in getPet(java.lang.String)
String pet;
^
F:\JAVA\PetAdvice.java:50: cannot find symbol
symbol : variable housing
location: class PetAdvice
switch(housing)
^
F:\JAVA\PetAdvice.java:53: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 1) && (hours>=18))
^
F:\JAVA\PetAdvice.java:53: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours>=18))
^
F:\JAVA\PetAdvice.java:58: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:58: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:58: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:62: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 1) && (hours < 10))
^
F:\JAVA\PetAdvice.java:62: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 1) && (hours < 10))
^
F:\JAVA\PetAdvice.java:66: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 2) && (hours >= 10))
^
F:\JAVA\PetAdvice.java:66: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 2) && (hours >= 10))
^
F:\JAVA\PetAdvice.java:70: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 2) && (hours < 10))
^
F:\JAVA\PetAdvice.java:70: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 2) && (hours < 10))
^
F:\JAVA\PetAdvice.java:74: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 3) && (hours >= 6))
^
F:\JAVA\PetAdvice.java:74: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 3) && (hours >= 6))
^
F:\JAVA\PetAdvice.java:75: cannot find symbol
symbol : variable fish
location: class PetAdvice
pet = fish;
^
F:\JAVA\PetAdvice.java:78: cannot find symbol
symbol : variable housing
location: class PetAdvice
if ((housing = 3) && (hours < 6))
^
F:\JAVA\PetAdvice.java:78: cannot find symbol
symbol : variable hours
location: class PetAdvice
if ((housing = 3) && (hours < 6))
^
F:\JAVA\PetAdvice.java:85: cannot find symbol
symbol : variable commission
location: class PetAdvice
return commission;
^
F:\JAVA\PetAdvice.java:145: cannot find symbol
symbol : variable code
location: class PetAdvice
code = Integer.parseInt(JOptionPane.showInputDialog(null,message));
^
F:\JAVA\PetAdvice.java:148: cannot find symbol
symbol : variable code
location: class PetAdvice
if(code<1 || code>4) throw new NumberFormatException();
^
F:\JAVA\PetAdvice.java:148: cannot find symbol
symbol : variable code
location: class PetAdvice
if(code<1 || code>4) throw new NumberFormatException();
^
F:\JAVA\PetAdvice.java:156: cannot find symbol
symbol : variable code
location: class PetAdvice
return code;
^
26 errors

Tool completed with exit code 1
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: problem with a java program

 
0
  #4
Mar 21st, 2008
actually stratch that...i pretty much have two errors problems...one is with the if statements and the other is in the getPet.....

F:\JAVA\PetAdvice.java:27: getPet(java.lang.String) in PetAdvice cannot be applied to ()
pet = getPet();
^
F:\JAVA\PetAdvice.java:28: output(java.lang.String) in PetAdvice cannot be applied to ()
output();
^
F:\JAVA\PetAdvice.java:55: operator && cannot be applied to int,boolean
if ((housing = 1) && (hours>=18))
^
F:\JAVA\PetAdvice.java:60: operator && cannot be applied to int,boolean
if ((housing = 1) && (hours>=10) && (hours < 18))
^
F:\JAVA\PetAdvice.java:64: operator && cannot be applied to int,boolean
if ((housing = 1) && (hours < 10))
^
F:\JAVA\PetAdvice.java:68: operator && cannot be applied to int,boolean
if ((housing = 2) && (hours >= 10))
^
F:\JAVA\PetAdvice.java:72: operator && cannot be applied to int,boolean
if ((housing = 2) && (hours < 10))
^
F:\JAVA\PetAdvice.java:76: operator && cannot be applied to int,boolean
if ((housing = 3) && (hours >= 6))
^
F:\JAVA\PetAdvice.java:80: operator && cannot be applied to int,boolean
if ((housing = 3) && (hours < 6))
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: problem with a java program

 
0
  #5
Mar 21st, 2008
ok updating again my only error is in the switch with the &&
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: problem with a java program

 
0
  #6
Mar 21st, 2008
I see some potential problems in the "if" statements where you were using = instead off == in places. You say there's only one error now and that you've made revisions, so it may be good to post your revised code along with the error and the line number.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: problem with a java program

 
0
  #7
Mar 21st, 2008
I think Vernon's suggestion will fix your problems. This is a common mistake when first learning Java and other C-style programming languages. Saying '=' is an assignment to a value statement, saying '==' is a test for equality. In other words the statement
  1. housing = 2
means "set 'housing' equal to 2" while the statement
  1. housing == 2
means "is housing currently equal to the number 2?" which can only be a true or false (boolean) answer.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: problem with a java program

 
0
  #8
Mar 23rd, 2008
  1.  
  2. /*
  3.  
  4. Programmer: Jim Johnson
  5. Date: 3/18/08
  6. Filename: PetAdvice.java
  7. Purpose: This program determines the best choice for a pet using five methods: getHousing(), getHours(), getPet(), output(), and finish()
  8.  
  9. */
  10.  
  11. //packages to import
  12.  
  13. import javax.swing.JOptionPane;
  14.  
  15.  
  16. public class PetAdvice
  17. {
  18. public static void main(String[] args)
  19. {
  20. //declare class variables
  21. int housing;
  22. double hours;
  23. String pet;
  24.  
  25. //call method
  26. housing = getHousing();
  27. hours = getHours();
  28. pet = getPet(hours, housing);
  29. output(pet);
  30. finish();
  31. }//end main()
  32.  
  33. //the output() method displays the pet recommended
  34. public static void output(String pet)
  35. {
  36. ;
  37.  
  38. //display output
  39. JOptionPane.showMessageDialog(null, "According to your requirements, I would recommend a " + pet, "Pet Advice: Recommendation", JOptionPane.INFORMATION_MESSAGE);
  40.  
  41. }// end output
  42.  
  43.  
  44.  
  45. //the getPet() method accepts the housing and hours and returns a pet
  46. public static String getPet(double hours, int housing)
  47. {
  48. //declare method variables
  49.  
  50. String pet;
  51.  
  52.  
  53. switch(housing)
  54. {
  55. case 1: //pot-bellied pig
  56. if ((housing == 1) && (hours>=18))
  57. pet = "pot-bellied pig";
  58. break;
  59. case 2: //dog
  60. if ((housing == 1) && (hours>=10) && (hours < 18))
  61. pet = "dog";
  62. break;
  63. case 3: //snake
  64. if ((housing == 1) && (hours < 10))
  65. pet = "snake";
  66. break;
  67. case 4: //cat
  68. if ((housing == 2) && (hours >= 10))
  69. pet = "cat";
  70. break;
  71. case 5: //hamster
  72. if ((housing == 2) && (hours < 10))
  73. pet = "hamster";
  74. break;
  75. case 6: //fish
  76. if ((housing == 3) && (hours >= 6))
  77. pet = "fish";
  78. break;
  79. case 7: //ant farm
  80. if ((housing == 3) && (hours < 6))
  81. pet = "ant farm";
  82. break;
  83.  
  84. }//end switch
  85.  
  86.  
  87. //.return value to the calling method
  88.  
  89. return pet;
  90.  
  91. }//end getComm
  92.  
  93.  
  94.  
  95.  
  96. //the getHours() method asks the user to input hours and validate it
  97.  
  98. public static double getHours()
  99. {
  100. //declare method variables
  101. double hours = 0.0;
  102. boolean done = false;
  103.  
  104. //loop while not done
  105. while(!done)
  106. {
  107. String answer = JOptionPane.showInputDialog(null, "Enter the average number of hours\nyou are home per week:", "Pet Advice: Hours", JOptionPane.QUESTION_MESSAGE);
  108.  
  109. //See if the cancel buttom was clicked....this will only work if the input is brought in as a a string
  110. if(answer == null) finish();
  111.  
  112. //validate the input
  113. try
  114. {
  115. //is answer a valid double
  116. hours = Double.parseDouble(answer);
  117.  
  118. //is sales reasonable?
  119. if(hours <= 0) throw new NumberFormatException();
  120. //if the value is valid and reasonable, get out of the while loop
  121. else done = true;
  122. } //end try
  123. catch(NumberFormatException e)
  124. {
  125. JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.\nPlease enter a positive number for hours", "Error", JOptionPane. ERROR_MESSAGE);
  126.  
  127. } //end catch
  128.  
  129. } //end while
  130.  
  131.  
  132. return hours;
  133. } //end getHours()
  134.  
  135. //the getHousing() method retries a housing option from the user and validates it.
  136. public static int getHousing()
  137. {
  138. //declare method variables
  139. int housing = 0;
  140. boolean done = false;
  141.  
  142. //loop while not done
  143. while(!done)
  144. {
  145. try
  146. {
  147. String message = JOptionPane.showInputDialog(null, "Enter your type of residence:" + "\n\n1) House\n2) Apartment\n3) Dormitory\n4) Quit the Application\n", "Pet Advice: Housing", JOptionPane.QUESTION_MESSAGE);
  148.  
  149. housing = Integer.parseInt(JOptionPane.showInputDialog(null,message));
  150.  
  151. //test for valid housing 1, 2, 3, or 4
  152. if(housing<1 || housing>4) throw new NumberFormatException();
  153. else done = true;
  154. }
  155. catch(NumberFormatException e)
  156. {
  157. JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.", "Error", JOptionPane.INFORMATION_MESSAGE);
  158. }
  159. }
  160. return housing;
  161. }
  162.  
  163.  
  164. //the finish() method successfully terminates the application
  165. public static void finish()
  166. {
  167. System.exit(0);
  168. } //end finish()
  169.  
  170.  
  171. } //end class

I am getting an error that says...
H:\JAVA\PetAdvice.java:88: variable pet might not have been initialized
return pet;

i think it is in...

  1. //declare method variables
  2.  
  3. String pet;

Is there some kind of way to initialize a string if this is the problem
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: problem with a java program

 
0
  #9
Mar 24th, 2008
Java is a "safe" language and not a very trusting one. Even if you have coded it in such a way so as to make sure that "pet" is always initialized before being returned, Java still requires that there be pretty much no possible way that such a return situation can happen. C++ might issue you a warning, which you are free to ignore, but Java doesn't give you that option. So you must initialize it to SOMETHING in your program. You are going to make some assignment in the code that overwrites that initialization so it's irrelevant what value you initialize it to, but you still need that initialization to get it to compile. So just initialize it to whatever:
  1. String pet = "Hello world";
or simply the empty string:
  1. String pet = "";
and see if it compiles and does what you want.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 53
Reputation: whoost is an unknown quantity at this point 
Solved Threads: 1
whoost's Avatar
whoost whoost is offline Offline
Junior Poster in Training

Re: problem with a java program

 
0
  #10
Mar 24th, 2008
VernonDozier is correct as to why your program won't compile.

This however, is not why I am commenting, as it has already been stated. I am here to state that once you do said corrections, you will need modifications to your switch statement, which is what i thought was the original cause of your problem.

to state:

  1. switch(housing){
  2. case 1: //pot-bellied pig
  3. if ((housing == 1) && (hours>=18))
  4. pet = "pot-bellied pig";
  5. break;
  6. case 2: //dog
  7. if ((housing == 1) && (hours>=10) && (hours < 18))
  8. pet = "dog";
  9. break;

will not achieve the results you want (just try to get the result "dog"). since you are passing housing as the argument to the switch, saying case 1 is like saying "if housing equals 1," then do something. To do what you want, instead, you should say:

  1. switch(housing)
  2. {
  3. case 1:
  4. if (hours>=18){
  5. pet = "pot-bellied pig";
  6. }
  7. else if ((hours>=10) && (hours < 18)){
  8. pet = "dog";
  9. }
  10. else if (hours < 10){
  11. pet = "snake";
  12. }
  13. break;

Seeing as how this is an assignment, i would suggest that you study (perhaps even try) both ways, and see if you can get the rest of the switch statement. That'd be the best way to learn, but theres always people to help if you still get stuck. Hope this sets you on the path to get the results you want.

peace
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC