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. -
/**
-
* A program that simulates an ant taking a step on a sheet of paper. stage 2
-
*
-
* @author (safra)
-
* @version (18/8/2010)
-
*/
-
import java.util.Scanner;
-
import javax.swing.JOptionPane;
-
public class Stage2a
-
{
-
public static void main(String[] args)
-
{
-
final int WIDTH = 100; // width of the sheet in pixels
-
final int HEIGHT = 100; // height of the sheet in pixels
-
-
String input;
-
String input1;
-
int x; // the x-coordinate
-
int y; // the y-coordinate
-
int steps;// the number of steps to be taken
-
-
-
input = JOptionPane.showInputDialog ("Enter your ant's x-coordinate: ");
-
x = Integer.parseInt (input);
-
-
if(x < 0 || x > WIDTH-1) //checking if it's in the range
-
{
-
JOptionPane.showMessageDialog (null,"The x-coordinate must be between 0 and " + (WIDTH-1));
-
System.exit(-1);
-
-
}
-
input = JOptionPane.showInputDialog("Enter your ant's y coordinate: ");
-
y = Integer.parseInt(input);
-
-
if (y <0 || y > HEIGHT-1)
-
{
-
JOptionPane.showMessageDialog (null,"The y-coordinate must be between 0 and " + (HEIGHT-1));
-
System.exit(-1);
-
-
}
-
-
input = JOptionPane.showInputDialog ("Enter your ant's direction");
-
-
-
//the number of steps
-
input1 = JOptionPane.showInputDialog ("How many steps should the ant take?");
-
steps = Integer.parseInt(input1);
-
-
for (int z=1;z<=steps;z++)
-
{
-
-
-
if(input.equalsIgnoreCase("U"))//increasing or decresing the coordinates according to direction
-
{
-
y =++y;
-
}
-
else if (input.equalsIgnoreCase("UR"))
-
{
-
y =++y;
-
x =++x;
-
}
-
else if (input.equalsIgnoreCase("UL"))
-
{
-
y =++y;
-
x =--x;
-
}
-
else if (input.equalsIgnoreCase("R"))
-
{
-
x = ++x;
-
}
-
else if (input.equalsIgnoreCase("DR"))
-
{
-
y =--y;
-
x =++x;
-
}
-
-
else if (input.equalsIgnoreCase("D"))
-
{
-
y =--y;
-
}
-
else if (input.equalsIgnoreCase("DL"))
-
{
-
y =--y;
-
x =--x;
-
}
-
else if (input.equalsIgnoreCase("L"))
-
{
-
x =--x;
-
}
-
else
-
{JOptionPane.showMessageDialog (null,"Please enter one of the following: \n U,UR,UL,D,DR,DL,L,R");
-
System.exit (-1);}
-
}
-
-
// "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
-
if(x < 0)
-
{
-
x += WIDTH;
-
}
-
else if (x > WIDTH-1)
-
{
-
x=x-WIDTH;
-
}
-
-
if (y < 0)
-
{
-
y += HEIGHT;
-
}
-
else if (y > HEIGHT-1)
-
{
-
y=y-HEIGHT;
-
}
-
-
-
JOptionPane.showMessageDialog (null,"The new x coordinate is: " + x + "\n the new y coordinate is: " + y);
-
-
}
-
}
Check this out.I used TRY CATCH statement. - import java.util.Scanner;
-
import javax.swing.JOptionPane;
-
class Stage2a
-
{
-
public static void main(String[] args)
-
{
-
String D_Play="the user has inputed a string where an integer is needed";
-
try{
-
-
final int WIDTH = 100; // width of the sheet in pixels
-
final int HEIGHT = 100; // height of the sheet in pixels
-
-
String input;
-
String input1;
-
int x; // the x-coordinate
-
int y; // the y-coordinate
-
int steps;// the number of steps to be taken
-
-
-
input = JOptionPane.showInputDialog ("Enter your ant's x-coordinate: ");
-
x = Integer.parseInt (input);
-
-
if(x < 0 || x > WIDTH-1) //checking if it's in the range
-
{
-
JOptionPane.showMessageDialog (null,"The x-coordinate must be between 0 and " + (WIDTH-1));
-
System.exit(-1);
-
-
}
-
input = JOptionPane.showInputDialog("Enter your ant's y coordinate: ");
-
y = Integer.parseInt(input);
-
-
if (y <0 || y > HEIGHT-1)
-
{
-
JOptionPane.showMessageDialog (null,"The y-coordinate must be between 0 and " + (HEIGHT-1));
-
System.exit(-1);
-
-
}
-
-
input = JOptionPane.showInputDialog ("Enter your ant's direction");
-
-
-
//the number of steps
-
input1 = JOptionPane.showInputDialog ("How many steps should the ant take?");
-
steps = Integer.parseInt(input1);
-
-
for (int z=1;z<=steps;z++)
-
{
-
-
-
if(input.equalsIgnoreCase("U"))//increasing or decresing the coordinates according to direction
-
{
-
y =++y;
-
}
-
else if (input.equalsIgnoreCase("UR"))
-
{
-
y =++y;
-
x =++x;
-
}
-
else if (input.equalsIgnoreCase("UL"))
-
{
-
y =++y;
-
x =--x;
-
}
-
else if (input.equalsIgnoreCase("R"))
-
{
-
x = ++x;
-
}
-
else if (input.equalsIgnoreCase("DR"))
-
{
-
y =--y;
-
x =++x;
-
}
-
-
else if (input.equalsIgnoreCase("D"))
-
{
-
y =--y;
-
}
-
else if (input.equalsIgnoreCase("DL"))
-
{
-
y =--y;
-
x =--x;
-
}
-
else if (input.equalsIgnoreCase("L"))
-
{
-
x =--x;
-
}
-
else
-
{JOptionPane.showMessageDialog (null,"Please enter one of the following: \n U,UR,UL,D,DR,DL,L,R");
-
System.exit (-1);}
-
}
-
-
// "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
-
if(x < 0)
-
{
-
x += WIDTH;
-
}
-
else if (x > WIDTH-1)
-
{
-
x=x-WIDTH;
-
}
-
-
if (y < 0)
-
{
-
y += HEIGHT;
-
}
-
else if (y > HEIGHT-1)
-
{
-
y=y-HEIGHT;
-
}
-
-
-
JOptionPane.showMessageDialog (null,"The new x coordinate is: " + x + "\n the new y coordinate is: " + y);
-
}
-
-
catch(Exception e){
-
JOptionPane.showMessageDialog (null,D_Play," Alert", JOptionPane.ERROR_MESSAGE);
-
}
-
}
-
}
6 2182
wrap your code in try catch for handling the exceptional cases.
Regards
Dheeraj Joshi
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..
Check this out.I used TRY CATCH statement. - import java.util.Scanner;
-
import javax.swing.JOptionPane;
-
class Stage2a
-
{
-
public static void main(String[] args)
-
{
-
String D_Play="the user has inputed a string where an integer is needed";
-
try{
-
-
final int WIDTH = 100; // width of the sheet in pixels
-
final int HEIGHT = 100; // height of the sheet in pixels
-
-
String input;
-
String input1;
-
int x; // the x-coordinate
-
int y; // the y-coordinate
-
int steps;// the number of steps to be taken
-
-
-
input = JOptionPane.showInputDialog ("Enter your ant's x-coordinate: ");
-
x = Integer.parseInt (input);
-
-
if(x < 0 || x > WIDTH-1) //checking if it's in the range
-
{
-
JOptionPane.showMessageDialog (null,"The x-coordinate must be between 0 and " + (WIDTH-1));
-
System.exit(-1);
-
-
}
-
input = JOptionPane.showInputDialog("Enter your ant's y coordinate: ");
-
y = Integer.parseInt(input);
-
-
if (y <0 || y > HEIGHT-1)
-
{
-
JOptionPane.showMessageDialog (null,"The y-coordinate must be between 0 and " + (HEIGHT-1));
-
System.exit(-1);
-
-
}
-
-
input = JOptionPane.showInputDialog ("Enter your ant's direction");
-
-
-
//the number of steps
-
input1 = JOptionPane.showInputDialog ("How many steps should the ant take?");
-
steps = Integer.parseInt(input1);
-
-
for (int z=1;z<=steps;z++)
-
{
-
-
-
if(input.equalsIgnoreCase("U"))//increasing or decresing the coordinates according to direction
-
{
-
y =++y;
-
}
-
else if (input.equalsIgnoreCase("UR"))
-
{
-
y =++y;
-
x =++x;
-
}
-
else if (input.equalsIgnoreCase("UL"))
-
{
-
y =++y;
-
x =--x;
-
}
-
else if (input.equalsIgnoreCase("R"))
-
{
-
x = ++x;
-
}
-
else if (input.equalsIgnoreCase("DR"))
-
{
-
y =--y;
-
x =++x;
-
}
-
-
else if (input.equalsIgnoreCase("D"))
-
{
-
y =--y;
-
}
-
else if (input.equalsIgnoreCase("DL"))
-
{
-
y =--y;
-
x =--x;
-
}
-
else if (input.equalsIgnoreCase("L"))
-
{
-
x =--x;
-
}
-
else
-
{JOptionPane.showMessageDialog (null,"Please enter one of the following: \n U,UR,UL,D,DR,DL,L,R");
-
System.exit (-1);}
-
}
-
-
// "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
-
if(x < 0)
-
{
-
x += WIDTH;
-
}
-
else if (x > WIDTH-1)
-
{
-
x=x-WIDTH;
-
}
-
-
if (y < 0)
-
{
-
y += HEIGHT;
-
}
-
else if (y > HEIGHT-1)
-
{
-
y=y-HEIGHT;
-
}
-
-
-
JOptionPane.showMessageDialog (null,"The new x coordinate is: " + x + "\n the new y coordinate is: " + y);
-
}
-
-
catch(Exception e){
-
JOptionPane.showMessageDialog (null,D_Play," Alert", JOptionPane.ERROR_MESSAGE);
-
}
-
}
-
}
This is an simple try catch statement program check it out it will help you to understand try catch better -
import javax.swing.*;
-
-
class demo
-
{
-
public static void main(String[] args){
-
String Dplay= "you have not enter an integer";
-
try
-
{
-
String enter=JOptionPane.showInputDialog("enter num");
-
int i=Integer.parseInt(enter);
-
-
if(i==1){
-
JOptionPane.showMessageDialog(null,"you hit the rite number");
-
-
}}
-
/////else if you enter any/////char then catch statement/////will excutes
-
catch(Exception e)
-
{
-
JOptionPane.showMessageDialog(null,Dplay,"Alert",JOptionPane.ERROR_MESSAGE);
-
-
}
-
-
}
-
}
wow..!!! worked perfectly..!! owe you big time!!! thanks alooootttt!!!! yay!! happiness in da air!! hehehe
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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=""...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |