Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Whats wrong with newb code? (http://www.programmingforums.org/showthread.php?t=12294)

Mixmaster Jan 1st, 2007 10:08 PM

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?

Game_Ender Jan 1st, 2007 10:14 PM

What are you trying to achieve here:
:

  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.

titaniumdecoy Jan 1st, 2007 10:34 PM

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.");
            }
        }
       
    }
}


ReggaetonKing Jan 1st, 2007 10:35 PM

(!(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!

titaniumdecoy Jan 1st, 2007 10:42 PM

Well, just simplify the statement:

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

Hence, 0 would fit that description. :)

ReggaetonKing Jan 1st, 2007 10:56 PM

Awwwh, I see! lol :beard:

EDIT: Why the f*ck would anyone write it like that anyways? I never seen that before!

DaWei Jan 1st, 2007 11:02 PM

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.

BinarySurfer Jan 1st, 2007 11:33 PM

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.

Mixmaster Jan 2nd, 2007 2:06 PM

Quote:

Originally Posted by BinarySurfer (Post 121998)
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.


All times are GMT -5. The time now is 4:04 PM.

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