![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2006
Posts: 8
Rep Power: 0
![]() |
Whats wrong with newb code?
import java.io.* ;
public class life42 {
private static BufferedReader stdin = new BufferedReader (
new InputStreamReader ( System.in) ) ;
public static void main (String[] args) {
System.out.println( "Enter a number") ;
String input = stdin.readLine();
int a = Integer.parseInt ( input) ;
if ((a !<= 0 || a !>= 0)) {
System.out.println("Maybe if you enter a number..") ;}
while ( a <= 0 || a >= 0) {
System.out.println ("Input a number") ;
input = stdin.readLine();
a = Integer.parseInt(input) ;
System.out.println(a) ;
}
if (a == 42) {
System.exit;
}
}
}Im doing aome programming exercises. This one is supposed to accept input and output. It prints the number and closes when 42 is entered. Looks good to me, but it wont let me compile gives some ridiculous error messages. Any help? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
What are you trying to achieve here:
java Syntax (Toggle Plain Text)
In the future you should post the line and text of the error messages. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I fixed up your code. If you have any questions, feel free to ask.
import java.io.*;
public class Life42 {
public static void main(String[] args) {
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String input;
int a;
while (true) {
try {
System.out.print("Input a number: ");
input = stdIn.readLine(); // throws IOException on failure
a = Integer.parseInt(input); // throws NumberFormatException on failure
System.out.println(a);
if (a == 42)
System.exit(0);
} catch (NumberFormatException e) {
System.out.println("Maybe if you enter a number...");
} catch (IOException e) {
System.err.println("There was a problem reading your input.");
}
}
}
} |
|
|
|
|
|
#4 |
|
Sexy Programmer
|
(!(a <= 0) || !(a >= 0))
I still understand that statement. It's saying if this number is not less or equal to zero or this number is not greater or equal to zero. Tell me a number that would fit that description!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Well, just simplify the statement:
!(a <= 0) || !(a >= 0) a > 0 || a < 0 a != 0 Hence, 0 would fit that description. ![]() |
|
|
|
|
|
#6 |
|
Sexy Programmer
|
Awwwh, I see! lol :beard:
EDIT: Why the f*ck would anyone write it like that anyways? I never seen that before!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Attaboy, Titanium; boolean algebra/DeMorgan's Theorem isn't just for gate-slinging hardware weenies. However, non-zero 'a' gives the true evaluation.
@king: a simplification often makes the original expression look silly, as in this case. There are cases, however, where the tested values might be coming from a port in inverted form or some other weird combination. Devising an exclusive-nor (if one doesn't have an exclusive-or instruction) can actually be done in fewer steps if one complicates the expression.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Dec 2006
Posts: 50
Rep Power: 2
![]() |
It's good practice to have decent code structure and organization. It makes reading code and distinguishing ends of operations easier. For example, your { 's should line up with the corresponding if, class, or while operation. As seen in titaniumdecoy's code, it's organized and easy to read.
On a side note, you code in an abstract way that seems to branch from the objective. I think you need to focus or fully understand what you intend to do and how. |
|
|
|
|
|
#9 | |
|
Newbie
Join Date: Sep 2006
Posts: 8
Rep Power: 0
![]() |
Quote:
But thanks, and everyone who helped thank you too. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is wrong with this code? | c0ldshadow | Visual Basic .NET | 5 | Dec 5th, 2005 5:37 PM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |
| Any ideas what i've done wrong? (Newb C Programmer) Code included | Ramlag | C++ | 10 | Mar 22nd, 2005 1:57 PM |