-
help input/output numbers
I'm new to Java and this forum. I'm teaching myself java with a book I bought but I'm stuck on this one.
This program is supposed to read 3 numbers and add each digit of that number. Like if I type in 123 it should output 6 (because 1+2+3 = 6). Non negatives and more than 1000 are not allowed. The program should quit if I type 1000 or negatives.
This is what I have so far, but for some reason, I have to type in the number twice to get a result. I think I messed up on the if else statement.
There is a method called Area51.readLine() and Area51.readNonwhiteChar(). I made those to "log" what the user types in.
Anybody please help me see what I am overlooking here.
Thanks. El chupacabra
Code:
/* code begins here */
public class Area51
{
public static void main(String[] args)
{
System.out.println("Enter 3 numbers on one line. Then press Enter.");
char n1,n2,n3;
int all3, number_not_allowed;
String over4chars;
//reads the numbers the users just typed in
over4chars = Area51.readLine();
//calculates the lenght of that line and stores it
number_not_allowed = over4chars.length();
//will read every character of that line
n1 = Area51.readNonwhiteChar();
n2 = Area51.readNonwhiteChar();
n3 = Area51.readNonwhiteChar();
//it was always off by 144 points so I implemented this workaround
//all 3 characters are added
all3 = (n1+n2+n3)-144;
//if the length of the number entered is more than 4 the program will quit
//otherwise, it will process the numbers just fine
if ((number_not_allowed > 3) || (number_not_allowed < 0))
{
System.out.println("This number is not allowed here punk.");
}
else
{
System.out.println("The sum of the digits in " + n1 + n2 + n3 + " is " + all3);
}
}
}
[ArchAngel added 'code' tags - please use in future]
-
You've haven't posted the code which does all of the I/O (which is probably where your error is)!
I've re-written your program. You'll see I do a few things differently to you. I've listed them here because when you first start to program, it's good to get into good habits early - bad habits are much harder to get out of.
1. I do the length check FIRST. What's the point in calculating something if it's going to be wrong?
2. I use generally more expressive variable names e.g. 'digit1' rather than 'n1' (the variable 'number_not_allowed' doesn't actually contain the number not allowed - it contains the length of the input!).
3. I don't have any "workarounds". I can't urge you strongly enough to never go down this route, all you will do is provide a temporarily fix which you will promptly forget about. This will then result in hack, upon hack, upon hack - making your code harder to understand, harder to maintain and more prone to bugs.
4. Your range check wouldn't catch the error if only 2 digits were entered.
5. I've tried to limit the scope of all the variables as much as possible - only declaring them when they're necessary.
Code:
import java.io.*;
public class Area51 {
public static void main(String[] args) throws IOException {
System.out.print("Enter 3 numbers and then press [enter]: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
br.close();
if (line.length() != 3) {
System.out.println("Invalid length.");
}
else {
int digit1 = Character.getNumericValue(line.charAt(0));
int digit2 = Character.getNumericValue(line.charAt(1));
int digit3 = Character.getNumericValue(line.charAt(2));
int sum = digit1 + digit2 + digit3;
System.out.println("The sum of the " + digit1 + ", " + digit2 + " and " + digit3 + " is " + sum);
}
}
}
ArchAngel.
O:-)
-
thank you so much
dude, you da man! Thank you so much.
I really appreciate your tips and for being cool to newbies.
el chupacabra
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks