Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 1st, 2007, 11:08 PM   #1
Mixmaster
Newbie
 
Mixmaster's Avatar
 
Join Date: Sep 2006
Posts: 8
Rep Power: 0 Mixmaster is on a distinguished road
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?
Mixmaster is offline   Reply With Quote
Old Jan 1st, 2007, 11:14 PM   #2
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
What are you trying to achieve here:
java Syntax (Toggle Plain Text)
  1. // This looks incorrect
  2. ((a !<= 0 || a !>= 0))
  3. // Try this instead
  4. (!(a <= 0) || !(a >= 0))

In the future you should post the line and text of the error messages.
Game_Ender is offline   Reply With Quote
Old Jan 1st, 2007, 11:34 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 909
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.");
            }
        }
        
    }
}
titaniumdecoy is offline   Reply With Quote
Old Jan 1st, 2007, 11:35 PM   #4
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
(!(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!
ReggaetonKing is offline   Reply With Quote
Old Jan 1st, 2007, 11:42 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 909
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Well, just simplify the statement:

!(a <= 0) || !(a >= 0)
a > 0 || a < 0
a != 0

Hence, 0 would fit that description.
titaniumdecoy is offline   Reply With Quote
Old Jan 1st, 2007, 11:56 PM   #6
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old Jan 2nd, 2007, 12:02 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jan 2nd, 2007, 12:33 AM   #8
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 53
Rep Power: 0 BinarySurfer is an unknown quantity at this point
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.
BinarySurfer is offline   Reply With Quote
Old Jan 2nd, 2007, 3:06 PM   #9
Mixmaster
Newbie
 
Mixmaster's Avatar
 
Join Date: Sep 2006
Posts: 8
Rep Power: 0 Mixmaster is on a distinguished road
Quote:
Originally Posted by BinarySurfer View Post
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.
Lot's of people tell me this and not even in stuff relating to coding. Guess it shows up in my code...


But thanks, and everyone who helped thank you too.
Mixmaster is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

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
What is wrong with this code? c0ldshadow Visual Basic .NET 5 Dec 5th, 2005 6:37 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 12:53 PM
How to post a question nnxion C++ 0 Jun 3rd, 2005 9:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 9:55 AM
Any ideas what i've done wrong? (Newb C Programmer) Code included Ramlag C++ 10 Mar 22nd, 2005 2:57 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:31 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC