Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 21st, 2013, 09:42 PM
vertroa vertroa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 vertroa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 46 sec
Reputation Power: 0
Help save a newb

Edit: So I realized I am retarded and fixed the problem in the function here.

java Code:
Original - java Code
  1.     public static boolean exit() {
  2.         boolean exitProgram = false;
  3.         Scanner input = new Scanner(System.in);
  4.         String close = "n";
  5.         System.out.println("Are you sure you want to exit? (y/n)");
  6.         close = input.nextLine();
  7.             if (close.equals("y")) {
  8.                 exitProgram = true;
  9.             } else {
  10.                 exitProgram = false;
  11.             }
  12.         // input.close();
  13.         return exitProgram;
  14.     }


However I had to comment out my close on the scanner because it gives an error on "n". So my next question is how would I close the scanner in this instance? Or better yet use the same scanner? Thanks!

java Code:
Original - java Code
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. abstract class Shape {
  5.     private static double scaleFactor;
  6.    
  7.     abstract void getArea();
  8.     abstract void getPerimeter();
  9.     abstract void scaleShape();
  10. }
  11. //
  12. //class Circle extends Shape {
  13. // 
  14. //}
  15. //
  16. //class Rectangle extends Shape {
  17. // 
  18. //}
  19.  
  20.  
  21. public class lab5 {
  22.    
  23.     public static boolean exit() {
  24.         boolean exitProgram = false;
  25.         Scanner input = new Scanner(System.in);
  26.         String close = "n";
  27.         System.out.println("Are you sure you want to exit? (y/n)");
  28.         close = input.nextLine();
  29.             if (close == "y") {
  30.                 exitProgram = true;
  31.             } else {
  32.                 exitProgram = false;
  33.             }
  34.         input.close();
  35.         return exitProgram;
  36.     }
  37.    
  38.     public static void main(String[] args) {
  39.        
  40.         boolean closeProgram = true;
  41.         int userOption= 0;
  42.            
  43.         // Create scanner
  44.         Scanner input = new Scanner(System.in);
  45.        
  46.         do {   
  47.             // User selects a menu option
  48.             System.out.println("Please select an option. \n" +
  49.                     "1. Add a new circle \n" +
  50.                     "2. Add a new rectangle \n" +
  51.                     "3. Delete all Shapes \n" +
  52.                     "4. Scale all shapes \n" +
  53.                     "5. Display the perimeters of all shapes \n" +
  54.                     "6. Display the areas of all shapes \n" +
  55.                     "7. Enter scale factor \n" +
  56.                     "8. Exit the program \n");
  57.             userOption = input.nextInt();
  58.        
  59.                 switch (userOption) {
  60.                     case 1:
  61.                        
  62.                         break;
  63.                     case 2:
  64.                        
  65.                         break;
  66.                     case 3:
  67.                        
  68.                         break;
  69.                     case 4:
  70.                        
  71.                         break;
  72.                     case 5:
  73.                        
  74.                         break;
  75.                     case 6:
  76.                        
  77.                         break;
  78.                     case 7:
  79.                        
  80.                         break;
  81.                     case 8:
  82.                         closeProgram = exit();
  83.                         break;
  84.                     }
  85.                
  86.                 // Reset vars
  87.                 userOption = 0;
  88.                
  89.         } while (closeProgram == false);
  90.         // Close scanner
  91.         input.close();
  92.     }
  93. }


I get this error at the end:

Code:
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at lab5.main(lab5.java:59)

Last edited by vertroa : February 21st, 2013 at 10:54 PM. Reason: Fixed it kinda

Reply With Quote
  #2  
Old February 22nd, 2013, 05:51 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,037 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 18 m 14 sec
Reputation Power: 346
Quote:
InputMismatchException

At line 59 the Scanner class's nextInt() method tried to read a numeric input but got non-numeric data and threw the exception.

The user should be careful to enter the right data for the program
or the program should be careful when reading data to make sure the right data is available. The Scanner class's hasNext... methods could be used to test the type of the input.

Reply With Quote
  #3  
Old February 22nd, 2013, 08:20 AM
vertroa vertroa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 vertroa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 45 m 46 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
At line 59 the Scanner class's nextInt() method tried to read a numeric input but got non-numeric data and threw the exception.

The user should be careful to enter the right data for the program
or the program should be careful when reading data to make sure the right data is available. The Scanner class's hasNext... methods could be used to test the type of the input.


Okay, I am going to take a look at the hasNext thing. I have been only inputting ints so I don't know how it is reading non ints.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Help save a newb

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap