techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Oct 28, 2010, 02:23 AM   #1
stinger608
Eligible for custom title
 
stinger608's Avatar
 
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
Send a message via MSN to stinger608

System Specs

JavaScript

Hey everyone, just getting started in JavaScript, and basically it is kicking my butt so far. It seems that the class is more for an intermediate learner than a beginner

None the less, I have this assignment due, and many of us students are completely in the dark here. LOL Anyhow, this is the question we must answer.

Write JavaScript code that anticipates and handles an error for an expected numeric field. This code is executed on keypress and the entered value is saved for you in a variable called enteredChar. Include the try block of JavaScript statements needed to check if the character is not a number or a non-alphanumeric character or if you throw an error message.

Any help would be much appreciated
__________________
#3 Forever A Fan!
Just Because I Don't Care Doesn't Mean I Don't Understand



Check our team Status on Free-DC
stinger608 is offline  
Crunching for Team TPU
Reply With Quote
Old Oct 28, 2010, 03:26 AM   #2
streetfighter 2
1000 Posts
 
streetfighter 2's Avatar
 
Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts

System Specs

Unfortunately my disturbingly large database of previously written code is offline right now so I'm doing this off google.

Code:
try {
       BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
       String line = stdin.readLine();
       int enteredChar = Integer.parseInt(line);
  }
  
  catch (IOException e) { System.out.println(e); }
  catch (NumberFormatException e) { System.out.println(e); }
What it does is grabs a line of characters (numbers/letters/etc) from the standard input and then attempts to parse it as an integer. If the input is not an integer it throws a "NumberFormatException", all other errors are related to IO and throw an "IOException".

I can't tell if the instructions are unclear or I'm just an idiot but you may want the variable "line" to be "enteredChar" and vice versa.

Last edited by streetfighter 2; Oct 28, 2010 at 03:39 AM.
streetfighter 2 is offline  
Reply With Quote
The Following User Says Thank You to streetfighter 2 For This Useful Post:
Old Oct 28, 2010, 03:42 AM   #3
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,577 (6.28/day)
Thanks: 1,755
Thanked 2,596 Times in 1,960 Posts

System Specs

That looks like Java, not JavaScript.

You'll want the parseInt (Ints do not have any values after the decimal point) or parseFloat (Floats may or may not have data after the decimal point) command:
http://www.javascripter.net/faq/convert2.htm

If they are invalid, it will return 0 or NaN (Not a Number) depending on browser. Pseudo-code:

Code:
var converted = parseInt(USERINPUT);
if ((converted == 0) || (converted == "NaN")) {
  // Return error message
} else {
  // Proceed with code
}
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following 2 Users Say Thank You to FordGT90Concept For This Useful Post:
Old Oct 28, 2010, 03:45 AM   #4
streetfighter 2
1000 Posts
 
streetfighter 2's Avatar
 
Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts

System Specs

Indeed it is Java. I didn't even notice I was supposed to write in "JavaScript". My bad.

The code you wrote looks good to me.

If you want a try-catch based script you'll need something like this:
Code:
var x=prompt("Enter a number between 0 and 10:","");
try
  { 
  if(x>10)
    {
    throw "Err1";
    }
  else if(x<0)
    {
    throw "Err2";
    }
  else if(isNaN(x))
    {
    throw "Err3";
    }
  }
catch(er)
  {
  if(er=="Err1")
    {
    alert("Error! The value is too high");
    }
  if(er=="Err2")
    {
    alert("Error! The value is too low");
    }
  if(er=="Err3")
    {
    alert("Error! The value is not a number");
    }
  }
streetfighter 2 is offline  
Reply With Quote
The Following User Says Thank You to streetfighter 2 For This Useful Post:
Old Oct 28, 2010, 03:49 AM   #5
stinger608
Eligible for custom title
 
stinger608's Avatar
 
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
Send a message via MSN to stinger608

System Specs

Hmm, from what I have been reading, and this example that I am posting, I am not understanding what your doing here.....sorry for my ignorance

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>onerror Event Handler</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
function processErrors(errMessage, errURL, errLineNum) {
window.alert("Error: " + errMessage + "\n"
+ "File: " + errURL + "\n"
+ "Line: " + errLineNum);
return true;
}
window.onerror=processErrors;
/* ]]> */
</script>
</head>
<body>
<script type="text/javascript">
/* <![CDATA[ */
document.wrte("My name is Don.");
/* ]]> */
</script>
</body>
</html>
This specific section of the text book is "Writing Custom Error Handling Functions"

I guess I am just confused on how this would be re-wrote to my class question LOL.
__________________
#3 Forever A Fan!
Just Because I Don't Care Doesn't Mean I Don't Understand



Check our team Status on Free-DC
stinger608 is offline  
Crunching for Team TPU
Reply With Quote
Old Oct 28, 2010, 03:50 AM   #6
stinger608
Eligible for custom title
 
stinger608's Avatar
 
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
Send a message via MSN to stinger608

System Specs

Quote:
Originally Posted by streetfighter 2 View Post
Indeed it is Java. I didn't even notice I was supposed to write in "JavaScript". My bad.

The code you wrote looks good to me.

If you want a try-catch based script you'll need something like this:
Code:
var x=prompt("Enter a number between 0 and 10:","");
try
  { 
  if(x>10)
    {
    throw "Err1";
    }
  else if(x<0)
    {
    throw "Err2";
    }
  else if(isNaN(x))
    {
    throw "Err3";
    }
  }
catch(er)
  {
  if(er=="Err1")
    {
    alert("Error! The value is too high");
    }
  if(er=="Err2")
    {
    alert("Error! The value is too low");
    }
  if(er=="Err3")
    {
    alert("Error! The value is not a number");
    }
  }
Ah, your first one was Java
This code makes much more sense LOL.

Wow guys, thanks for the ultra quick responses!!!
__________________
#3 Forever A Fan!
Just Because I Don't Care Doesn't Mean I Don't Understand



Check our team Status on Free-DC
stinger608 is offline  
Crunching for Team TPU
Reply With Quote
Old Oct 28, 2010, 03:57 AM   #7
streetfighter 2
1000 Posts
 
streetfighter 2's Avatar
 
Join Date: Jul 2010
Location: Philly
Posts: 1,599 (1.55/day)
Thanks: 1,004
Thanked 765 Times in 539 Posts

System Specs

I write code in so many different languages that I often get confused about which one is which. That and my memory is going...

As languages go JavaScript is one of the easier ones to learn because of it's syntactic simplicity. Though it's not quite as fundamental as brainfuck.
streetfighter 2 is offline  
Reply With Quote
The Following User Says Thank You to streetfighter 2 For This Useful Post:
Old Oct 28, 2010, 04:01 AM   #8
stinger608
Eligible for custom title
 
stinger608's Avatar
 
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
Send a message via MSN to stinger608

System Specs

Quote:
Originally Posted by streetfighter 2 View Post
I write code in so many different languages that I often get confused about which one is which. That and my memory is going...

As languages go JavaScript is one of the easier ones to learn because of it's syntactic simplicity. Though it's not quite as fundamental as brainfuck.
Holy crap, that is a lot of work to say "Hello World."
__________________
#3 Forever A Fan!
Just Because I Don't Care Doesn't Mean I Don't Understand



Check our team Status on Free-DC
stinger608 is offline  
Crunching for Team TPU
Reply With Quote
Old Oct 28, 2010, 04:17 AM   #9
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,577 (6.28/day)
Thanks: 1,755
Thanked 2,596 Times in 1,960 Posts

System Specs

Quote:
Originally Posted by stinger608 View Post
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>onerror Event Handler</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
/* <![CDATA[ */
function processErrors(errMessage, errURL, errLineNum) {
window.alert("Error: " + errMessage + "\n"
+ "File: " + errURL + "\n"
+ "Line: " + errLineNum);
return true;
}
window.onerror=processErrors;
/* ]]> */
</script>
</head>
<body>
<script type="text/javascript">
/* <![CDATA[ */
document.wrte("My name is Don.");
/* ]]> */
</script>
</body>
</html>
This code is just for formatting/displaying error messages in a popup window; not actually finding/catching input errors. Again, parseInt and parseFloat are the commands you are looking for.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Oct 29, 2010, 12:26 AM   #10
stinger608
Eligible for custom title
 
stinger608's Avatar
 
Join Date: Nov 2008
Location: Wyoming
Posts: 5,483 (3.31/day)
Thanks: 7,859
Thanked 2,531 Times in 1,776 Posts
Send a message via MSN to stinger608

System Specs

Thanks a ton guys! Of course I am still somewhat confused, but then again, that's not unusual
__________________
#3 Forever A Fan!
Just Because I Don't Care Doesn't Mean I Don't Understand



Check our team Status on Free-DC
stinger608 is offline  
Crunching for Team TPU
Reply With Quote
Reply


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

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
Javascript Hw Problem epicfail Programming & Webmastering 4 May 12, 2010 05:04 PM
Need javascript Help please wolf2009 Programming & Webmastering 1 Nov 30, 2009 11:06 PM
which Eclipse version for Javascript? Braveheart Programming & Webmastering 1 Oct 20, 2009 04:03 PM
Javascript return doesn't work? aximbigfan Programming & Webmastering 0 Mar 30, 2008 12:53 AM
JavaScript opens doors to browser-based attacks Alec§taar General Software 4 Jul 31, 2006 02:50 PM


All times are GMT. The time now is 07:27 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts