-
Member
Need some help with Java
I know I ran across this last year, when I was in the intro class at school. I'm having trouble inputting integers and doubles. I know there was a fix for this. WHen I type in a number, it uses some other number. For example, 36 will be 51...K will be like 25...
++++++++++++++++++++
Here is my output:
+++++++++++++++++++++
F:\gringo>java SqrRt
Enter a number
36
The square root of51.0is7.14142842854285
F:\gringo>
++++++++++++++++++++++++++++++++++
Now here is my source code:
++++++++++++++++++++++++++++++++++
//8-8-2001
//Author hino@citlink.net
//Finds square root of input
public class SqrRt
{
public static void main(String[ ] args) throws Exception
{
double userInput;
System.out.println("Enter a number");
userInput=(double)System.in.read( );
double SR;
SR = Math.sqrt(userInput);
System.out.println("The square root of" + userInput + "is" + SR);
}
}
-
The problem is that it is taking the first byte and giving you the numerical ascii representation of it.
0 = 48
1 = 49
.
.
9 = 57
So, if you enter 003 or 03 or 0 it is going to give you the ascii numeric of the first 0 which is 48. All other bytes are ignored.
-
couldn't the problem be with the way that he is inputing it??/
this is the way that I input stuff...
==========================================
import java.io.*;
public class testing
{
public static void main(String[ ] args) throws Exception
{
double userInput;
double SR;
BufferedReader stdin= new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
System.out.flush();
userInput= Double.parseDouble(stdin.readLine());
SR = Math.sqrt(userInput);
System.out.println("The square root of " + userInput + " is " + SR);
}
}
==================================
this works just fine... it's 3:15 am, so I don't wanna figure out why urs isn't working, but this does, so ur free to use it
-Z
-
zskills - You are right. The way that he has implemented it, it behaves as I described above. He should implement the way you did or in that fashion. I only tried to figure out what exactly his code was doing not how to fix it.
-
Member
Thanks. I think I've learned that System.in.read() is only for inputting char data. I will try zskills proggie too. <a href="http://bbs.pcstats.com/amdzone/messageview.cfm?catid=6&threadid=47090">
Here is a link to the help I got at AMDZone. This one works.</a> Basically I think you have to input it as a string and then convert it to a double.
-
Banned
Bumbling diatribe whilst balancing on soapbox.
One thing I noticed is the total lack of validating user input in the above cases.
Both will generate an unhandled exception when the user enters char data. Another thing is the square root of -9 is 'NaN'. Now I know what 'NaN' is, but will a user.
You may poo-poo my point, but in the real world, people input some weird stuff.
Anyway, a try-catch around the casting of the input and another around the square root call would handle most stuff. I'll leave it as an exercise for the reader to figure out how to re-prompt for proper input.
Now for the double twisting back somersault soapbox dismount...
-
qball - actually, the original code will take any char, a0009 and it will return the ascii num of a. It will not crash.
-
qball, I certainly agree with you and know exactly what you are saying... however, the point of what I was demonstrating was how to input a double..... If I were trying to dummy-proof the program, I would probably incorporate a try-catch setup as well.
I am curious though... how would you reprompt the input if it were incorrect...
since it is such a small program and has such few inputs, I would probably just reprompt for the input in the catch statement. wouldn't this work??? I'm not at my computer right now, so I can't try for myself, but I'm curious how you would do it.
-Z
on a separate note... hahaha, ur right bobcat, even though you'd have to be thinking in ascii code to use the program properly!!!
-
Banned
Whatever code I ran crashed with char input and returned NaN on negative number, I'm not making this up. But that is neither here not there.
As far as showing how to input a double, yes it is the right way. My point is you should ALWAYS code thoroughly, period. Even small programs should be coded for all exceptions. No biggie.
zskillz,
How to validate input? You could use a try-catch, and code new input in catch, but what if they input junk again? The answer is simple and you may kick yourself. So howsa about some PDL.
-------------------
boolean invalid = true
while (invalid)
{
try {
//put input stuff here
invalid = false
}
catch (Exception e)
{
//output nasty message to user or
//you could log error or
//do nuffin
}
}
---------------------
The only way out of while loop is valid input. This can be used for virtually any input validation.
-
sheesh.. thx q, nesting the try/catch in the while loop is what I didn't think of
thanks
-Z
-
Banned
you are welcome.
<edit>
BTW, what follows is the code I ran that generated numberformat exception and output of NaN on negatives.
=========================
import java.io.*;
public class testing
{
public static void main(String[ ] args) throws Exception
{
double userInput;
double SR;
BufferedReader stdin= new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
System.out.flush();
userInput= Double.parseDouble(stdin.readLine());
SR = Math.sqrt(userInput);
System.out.println("The square root of " + userInput + " is " + SR);
}
}
=========================================
[This message has been edited by qball (edited 08-16-2001).]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke
|
Bookmarks