473,421 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

display an error msg saying wrong format.

5
hi everybody..
i need help with my assignment.i want to display a error message saying that the user has inputed a string where an integer is needed.because when i enter "h" (for an example) in the x coordinate field the system crashes.i want to display an error message instead.

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * A program that simulates an ant taking a step on a sheet of paper. stage 2
  3.  * 
  4.  * @author (safra)
  5.  * @version (18/8/2010)
  6.  */
  7. import java.util.Scanner;
  8. import javax.swing.JOptionPane;
  9. public class Stage2a
  10. {
  11.     public static void main(String[] args)
  12.     {
  13.         final int WIDTH = 100; // width of the sheet in pixels
  14.         final int HEIGHT = 100; // height of the sheet in pixels
  15.  
  16.         String input;
  17.         String input1;
  18.         int x; // the x-coordinate
  19.         int y; // the y-coordinate
  20.         int steps;// the number of steps to be taken
  21.  
  22.  
  23.        input = JOptionPane.showInputDialog ("Enter your ant's x-coordinate: ");
  24.        x = Integer.parseInt (input);
  25.  
  26.        if(x < 0 || x > WIDTH-1) //checking if it's in the range
  27.         {
  28.             JOptionPane.showMessageDialog (null,"The x-coordinate must be between 0 and " + (WIDTH-1));
  29.             System.exit(-1);
  30.  
  31.         }
  32.        input = JOptionPane.showInputDialog("Enter your ant's y coordinate: ");
  33.        y = Integer.parseInt(input);
  34.  
  35.        if (y <0 || y > HEIGHT-1)
  36.        {
  37.            JOptionPane.showMessageDialog (null,"The y-coordinate must be between 0 and " + (HEIGHT-1));
  38.            System.exit(-1);
  39.  
  40.         }
  41.  
  42.           input = JOptionPane.showInputDialog ("Enter your ant's direction");
  43.  
  44.  
  45.       //the number of steps
  46.         input1 = JOptionPane.showInputDialog ("How many steps should the ant take?");
  47.         steps = Integer.parseInt(input1);
  48.  
  49.        for (int z=1;z<=steps;z++)
  50.        {
  51.  
  52.  
  53.        if(input.equalsIgnoreCase("U"))//increasing or decresing the coordinates according to direction
  54.         {
  55.             y =++y;
  56.         }
  57.        else if (input.equalsIgnoreCase("UR")) 
  58.         {
  59.             y =++y;
  60.             x =++x;
  61.         }
  62.         else if (input.equalsIgnoreCase("UL"))
  63.         {   
  64.             y =++y;
  65.             x =--x;
  66.         }
  67.         else if (input.equalsIgnoreCase("R"))
  68.         {
  69.             x = ++x;
  70.         }
  71.         else if (input.equalsIgnoreCase("DR"))
  72.         {
  73.             y =--y;
  74.             x =++x;
  75.         }
  76.  
  77.         else if (input.equalsIgnoreCase("D"))
  78.         {
  79.             y =--y;
  80.         }
  81.         else if (input.equalsIgnoreCase("DL"))
  82.         {
  83.             y =--y;
  84.             x =--x;
  85.         }
  86.         else if (input.equalsIgnoreCase("L"))
  87.         {
  88.             x =--x;
  89.         }
  90.         else 
  91.         {JOptionPane.showMessageDialog (null,"Please enter one of the following: \n U,UR,UL,D,DR,DL,L,R");
  92.         System.exit (-1);}
  93.         }
  94.  
  95.         // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
  96.         if(x < 0)
  97.         {
  98.             x += WIDTH;
  99.         }
  100.         else if (x > WIDTH-1)
  101.         {
  102.             x=x-WIDTH;
  103.         }
  104.  
  105.         if (y < 0)
  106.         {
  107.             y += HEIGHT;
  108.         }
  109.          else if (y > HEIGHT-1)
  110.         {
  111.             y=y-HEIGHT;
  112.         }
  113.  
  114.  
  115.        JOptionPane.showMessageDialog (null,"The new x coordinate is: " + x + "\n the new y coordinate is: " + y);
  116.  
  117.     }
  118. }
Aug 28 '10 #1

✓ answered by ahmee

Check this out.I used TRY CATCH statement.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import javax.swing.JOptionPane;
  3. class Stage2a
  4. {
  5. public static void main(String[] args)
  6. {
  7. String D_Play="the user has inputed a string where an integer is needed";
  8.     try{
  9.  
  10. final int WIDTH = 100; // width of the sheet in pixels
  11. final int HEIGHT = 100; // height of the sheet in pixels
  12.  
  13. String input;
  14. String input1;
  15. int x; // the x-coordinate
  16. int y; // the y-coordinate
  17. int steps;// the number of steps to be taken
  18.  
  19.  
  20. input = JOptionPane.showInputDialog ("Enter your ant's x-coordinate: ");
  21. x = Integer.parseInt (input);
  22.  
  23. if(x < 0 || x > WIDTH-1) //checking if it's in the range
  24. {
  25. JOptionPane.showMessageDialog (null,"The x-coordinate must be between 0 and " + (WIDTH-1));
  26. System.exit(-1);
  27.  
  28. }
  29. input = JOptionPane.showInputDialog("Enter your ant's y coordinate: ");
  30. y = Integer.parseInt(input);
  31.  
  32. if (y <0 || y > HEIGHT-1)
  33. {
  34. JOptionPane.showMessageDialog (null,"The y-coordinate must be between 0 and " + (HEIGHT-1));
  35. System.exit(-1);
  36.  
  37. }
  38.  
  39. input = JOptionPane.showInputDialog ("Enter your ant's direction");
  40.  
  41.  
  42. //the number of steps
  43. input1 = JOptionPane.showInputDialog ("How many steps should the ant take?");
  44. steps = Integer.parseInt(input1);
  45.  
  46. for (int z=1;z<=steps;z++)
  47. {
  48.  
  49.  
  50. if(input.equalsIgnoreCase("U"))//increasing or decresing the coordinates according to direction
  51. {
  52. y =++y;
  53. }
  54. else if (input.equalsIgnoreCase("UR"))
  55. {
  56. y =++y;
  57. x =++x;
  58. }
  59. else if (input.equalsIgnoreCase("UL"))
  60. {
  61. y =++y;
  62. x =--x;
  63. }
  64. else if (input.equalsIgnoreCase("R"))
  65. {
  66. x = ++x;
  67. }
  68. else if (input.equalsIgnoreCase("DR"))
  69. {
  70. y =--y;
  71. x =++x;
  72. }
  73.  
  74. else if (input.equalsIgnoreCase("D"))
  75. {
  76. y =--y;
  77. }
  78. else if (input.equalsIgnoreCase("DL"))
  79. {
  80. y =--y;
  81. x =--x;
  82. }
  83. else if (input.equalsIgnoreCase("L"))
  84. {
  85. x =--x;
  86. }
  87. else
  88. {JOptionPane.showMessageDialog (null,"Please enter one of the following: \n U,UR,UL,D,DR,DL,L,R");
  89. System.exit (-1);}
  90. }
  91.  
  92. // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
  93. if(x < 0)
  94. {
  95. x += WIDTH;
  96. }
  97. else if (x > WIDTH-1)
  98. {
  99. x=x-WIDTH;
  100. }
  101.  
  102. if (y < 0)
  103. {
  104. y += HEIGHT;
  105. }
  106. else if (y > HEIGHT-1)
  107. {
  108. y=y-HEIGHT;
  109. }
  110.  
  111.  
  112. JOptionPane.showMessageDialog (null,"The new x coordinate is: " + x + "\n the new y coordinate is: " + y);
  113.     }
  114.  
  115.     catch(Exception e){
  116. JOptionPane.showMessageDialog (null,D_Play," Alert", JOptionPane.ERROR_MESSAGE);
  117. }
  118. }
  119. }

6 2182
Dheeraj Joshi
1,123 Expert 1GB
wrap your code in try catch for handling the exceptional cases.

Regards
Dheeraj Joshi
Aug 29 '10 #2
safra
5
umm..i dont exactly know how to do that.i have very little knowledge in java..im still new to this field. do you think you can teach me how its done? please..
Aug 29 '10 #3
ahmee
36
Check this out.I used TRY CATCH statement.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import javax.swing.JOptionPane;
  3. class Stage2a
  4. {
  5. public static void main(String[] args)
  6. {
  7. String D_Play="the user has inputed a string where an integer is needed";
  8.     try{
  9.  
  10. final int WIDTH = 100; // width of the sheet in pixels
  11. final int HEIGHT = 100; // height of the sheet in pixels
  12.  
  13. String input;
  14. String input1;
  15. int x; // the x-coordinate
  16. int y; // the y-coordinate
  17. int steps;// the number of steps to be taken
  18.  
  19.  
  20. input = JOptionPane.showInputDialog ("Enter your ant's x-coordinate: ");
  21. x = Integer.parseInt (input);
  22.  
  23. if(x < 0 || x > WIDTH-1) //checking if it's in the range
  24. {
  25. JOptionPane.showMessageDialog (null,"The x-coordinate must be between 0 and " + (WIDTH-1));
  26. System.exit(-1);
  27.  
  28. }
  29. input = JOptionPane.showInputDialog("Enter your ant's y coordinate: ");
  30. y = Integer.parseInt(input);
  31.  
  32. if (y <0 || y > HEIGHT-1)
  33. {
  34. JOptionPane.showMessageDialog (null,"The y-coordinate must be between 0 and " + (HEIGHT-1));
  35. System.exit(-1);
  36.  
  37. }
  38.  
  39. input = JOptionPane.showInputDialog ("Enter your ant's direction");
  40.  
  41.  
  42. //the number of steps
  43. input1 = JOptionPane.showInputDialog ("How many steps should the ant take?");
  44. steps = Integer.parseInt(input1);
  45.  
  46. for (int z=1;z<=steps;z++)
  47. {
  48.  
  49.  
  50. if(input.equalsIgnoreCase("U"))//increasing or decresing the coordinates according to direction
  51. {
  52. y =++y;
  53. }
  54. else if (input.equalsIgnoreCase("UR"))
  55. {
  56. y =++y;
  57. x =++x;
  58. }
  59. else if (input.equalsIgnoreCase("UL"))
  60. {
  61. y =++y;
  62. x =--x;
  63. }
  64. else if (input.equalsIgnoreCase("R"))
  65. {
  66. x = ++x;
  67. }
  68. else if (input.equalsIgnoreCase("DR"))
  69. {
  70. y =--y;
  71. x =++x;
  72. }
  73.  
  74. else if (input.equalsIgnoreCase("D"))
  75. {
  76. y =--y;
  77. }
  78. else if (input.equalsIgnoreCase("DL"))
  79. {
  80. y =--y;
  81. x =--x;
  82. }
  83. else if (input.equalsIgnoreCase("L"))
  84. {
  85. x =--x;
  86. }
  87. else
  88. {JOptionPane.showMessageDialog (null,"Please enter one of the following: \n U,UR,UL,D,DR,DL,L,R");
  89. System.exit (-1);}
  90. }
  91.  
  92. // "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
  93. if(x < 0)
  94. {
  95. x += WIDTH;
  96. }
  97. else if (x > WIDTH-1)
  98. {
  99. x=x-WIDTH;
  100. }
  101.  
  102. if (y < 0)
  103. {
  104. y += HEIGHT;
  105. }
  106. else if (y > HEIGHT-1)
  107. {
  108. y=y-HEIGHT;
  109. }
  110.  
  111.  
  112. JOptionPane.showMessageDialog (null,"The new x coordinate is: " + x + "\n the new y coordinate is: " + y);
  113.     }
  114.  
  115.     catch(Exception e){
  116. JOptionPane.showMessageDialog (null,D_Play," Alert", JOptionPane.ERROR_MESSAGE);
  117. }
  118. }
  119. }
Aug 31 '10 #4
ahmee
36
This is an simple try catch statement program check it out it will help you to understand try catch better
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2.  
  3.    class demo
  4. {
  5.        public static void main(String[] args){
  6.   String Dplay= "you have not enter an integer";
  7.        try
  8. {
  9.        String  enter=JOptionPane.showInputDialog("enter num");
  10.        int  i=Integer.parseInt(enter);
  11.  
  12.        if(i==1){
  13.        JOptionPane.showMessageDialog(null,"you hit the rite number");
  14.  
  15. }}
  16. /////else if you enter any/////char then catch statement/////will excutes
  17.         catch(Exception e)
  18. {
  19.         JOptionPane.showMessageDialog(null,Dplay,"Alert",JOptionPane.ERROR_MESSAGE);
  20.  
  21. }
  22.  
  23. }
  24. }
Aug 31 '10 #5
safra
5
wow..!!! worked perfectly..!! owe you big time!!! thanks alooootttt!!!! yay!! happiness in da air!! hehehe
Aug 31 '10 #6
ahmee
36
you welcome...
Sep 1 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: indushekara | last post by:
Hi, We are having memory corruption in our application somewhere, unable to find out. one part of code we found that we are specifying wrong format specifier. Could anyone let me know if the...
10
by: bob | last post by:
Hello, I use Microsoft Visual C++ .NET (version 7.1.3088) Sometimes (with big codes?) when I get a compile error and click on the error, the cursor is placed next to the wrong piece of code. The...
0
by: GFro | last post by:
I have a calendar page that returns a date to a textbox on the parent page. It is returning the wrong format on the deployment server. On the development server the calendar returns to textbox in...
8
by: Dinesh Jain | last post by:
Hi al,, I have written a code which displays directory listing of files from FTP server directory.I display all the files in a listview with its associated icons. To display icons, I get the...
0
by: monomaniac21 | last post by:
hi im trying to do a query which takes rows of large amounts of text data and displays them one after another. the size of the fields is causing the page to be massive in size. so to try and...
0
eboyjr14
by: eboyjr14 | last post by:
I have this xml metacity thing, and it is giving off this error: The file format is invalid. <?xml version="1.0"?> <metacity_theme> <info> <name>Royale</name> <author>Devin...
1
by: JamesB | last post by:
I am trying to access a method in another VB6 application from my C# app. If I add a reference to the relevant DLL it all works, and one bit where I need to set a DB connection works by doing: ...
2
by: jay123 | last post by:
Hello, I am making a project in VS2008, C#. Background of prob: I have integrated a postcode(UK- its like if someone enters postcode and clicks button to retrieve the address, he gets all the...
3
by: vinod2vin | last post by:
hi i am using visual basic 6 and trying to open access table . when the form is run i get a error saying that the database format is un recognised please help me out in this issue .. thanks...
2
by: kummu4help | last post by:
Hi, i have a following form. <form id="frm_signup"> <b>please fill the following registration form.All the fields are compulsory</b><br/><br/> <table border="0" cellpadding=""...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.