compare biggest negative number

Discussion in 'Java' started by w00, Nov 22, 2006.

  1. w00

    w00 Guest

    Hello, i have to write a program in java where i have to enter three
    different numbers, and it will output the largest number. So for
    example:

    input: 2, 4 ,6 => output: 7
    input: 5, 2 ,1 => output: 5
    input: 7, 9 ,3 => output: 9

    My program works well, but not with negative values, cause it always
    returns a 0.
    So if i would enter the following:

    input: -2, -4 -,6

    Then the output should be -2, but i get a '0' instead. Any idea why
    this is happening and what i can do to fix it?
    Here's my code:

    import javax.swing.*;

    public class test
    {
    public static void main(String args[])
    {
    String input;
    int intNum;
    int prevNum=0;
    int output=0;

    for(int i=0; i<3; i++)
    {
    input = JOptionPane.showInputDialog(null,"Geef een
    nummer","input",JOptionPane.INFORMATION_MESSAGE);
    intNum = Integer.parseInt(input);

    if(prevNum < intNum)
    {
    prevNum = intNum;
    }

    }
    JOptionPane.showMessageDialog(null,"Het grootste getal is:
    "+prevNum,"getal",JOptionPane.INFORMATION_MESSAGE);


    }
    }
    w00, Nov 22, 2006
    #1
    1. Advertising

  2. w00

    Karl Uppiano Guest

    "w00" <> wrote in message
    news:...
    > Hello, i have to write a program in java where i have to enter three
    > different numbers, and it will output the largest number. So for
    > example:
    >
    > input: 2, 4 ,6 => output: 7
    > input: 5, 2 ,1 => output: 5
    > input: 7, 9 ,3 => output: 9
    >
    > My program works well, but not with negative values, cause it always
    > returns a 0.
    > So if i would enter the following:
    >
    > input: -2, -4 -,6
    >
    > Then the output should be -2, but i get a '0' instead. Any idea why
    > this is happening and what i can do to fix it?
    > Here's my code:
    >
    > import javax.swing.*;
    >
    > public class test
    > {
    > public static void main(String args[])
    > {
    > String input;
    > int intNum;
    > int prevNum=0;
    > int output=0;
    >
    > for(int i=0; i<3; i++)
    > {
    > input = JOptionPane.showInputDialog(null,"Geef een
    > nummer","input",JOptionPane.INFORMATION_MESSAGE);
    > intNum = Integer.parseInt(input);
    >
    > if(prevNum < intNum)
    > {
    > prevNum = intNum;
    > }
    >
    > }
    > JOptionPane.showMessageDialog(null,"Het grootste getal is:
    > "+prevNum,"getal",JOptionPane.INFORMATION_MESSAGE);
    >
    >
    > }
    > }


    You are initializing prevNum to zero. So with all negative inputs, it is the
    largest number in your test for the largest number. For this to work for all
    integers, you need to initialize prevNum to the most negative integer.
    Karl Uppiano, Nov 22, 2006
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    w00 schreef:
    > Hello, i have to write a program in java where i have to enter three
    > different numbers, and it will output the largest number. So for
    > example:
    >
    > input: 2, 4 ,6 => output: 7
    > input: 5, 2 ,1 => output: 5
    > input: 7, 9 ,3 => output: 9
    >
    > My program works well, but not with negative values, cause it always
    > returns a 0.
    > So if i would enter the following:
    >
    > input: -2, -4 -,6
    >
    > Then the output should be -2, but i get a '0' instead. Any idea why
    > this is happening and what i can do to fix it?
    > Here's my code:
    >
    > import javax.swing.*;
    >
    > public class test
    > {
    > public static void main(String args[])
    > {
    > String input;
    > int intNum;
    > int prevNum=0;


    Here you initiate the previous number to 0.

    > int output=0;
    >
    > for(int i=0; i<3; i++)
    > {

    <snip>
    >
    > if(prevNum < intNum)
    > {
    > prevNum = intNum;
    > }


    And here you set the previous number to the current number if it is
    smaller. Now think what would happen if all intNums are negative.

    <snip>

    Maybe you should choose a better number to initiate prevNum with. (Some
    static number in Integer comes to mind.)

    HTH, H.
    - --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFFZBIoe+7xMGD3itQRAhQIAJ43vgHJAZanbTzeLry2klJvNIb6SACfZl+y
    3yZnYPBFhDCWlkw/Ov7K730=
    =cLTk
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Nov 22, 2006
    #3
  4. w00

    w00 Guest

    Oh yeah, you're right. It works now. Thanks for your help :)
    w00, Nov 22, 2006
    #4
  5. w00

    Daniel Pitts Guest

    w00 wrote:
    > Hello, i have to write a program in java where i have to enter three
    > different numbers, and it will output the largest number. So for
    > example:
    >
    > input: 2, 4 ,6 => output: 7
    > input: 5, 2 ,1 => output: 5
    > input: 7, 9 ,3 => output: 9
    >
    > My program works well, but not with negative values, cause it always
    > returns a 0.
    > So if i would enter the following:
    >
    > input: -2, -4 -,6
    >
    > Then the output should be -2, but i get a '0' instead. Any idea why
    > this is happening and what i can do to fix it?
    > Here's my code:
    >
    > import javax.swing.*;
    >
    > public class test
    > {
    > public static void main(String args[])
    > {
    > String input;
    > int intNum;
    > int prevNum=0;
    > int output=0;
    >
    > for(int i=0; i<3; i++)
    > {
    > input = JOptionPane.showInputDialog(null,"Geef een
    > nummer","input",JOptionPane.INFORMATION_MESSAGE);
    > intNum = Integer.parseInt(input);
    >
    > if(prevNum < intNum)
    > {
    > prevNum = intNum;
    > }
    >
    > }
    > JOptionPane.showMessageDialog(null,"Het grootste getal is:
    > "+prevNum,"getal",JOptionPane.INFORMATION_MESSAGE);
    >
    >
    > }
    > }


    // FindMax.java
    import java.util.*;

    public class FindMax {
    public static void main(String[] args) {
    if (args.length == 0) {
    System.out.println("Usage: java FindMax [number] [number]
    [number...]");
    }
    String max = Collections.max(Arrays.asList(args), new
    Comparator<String>() {
    public int compare(String a, String b) {
    return
    Integer.valueOf(a).compareTo(Integer.valueOf(b));
    }
    });
    System.out.println("The largest number is: " + max);
    }
    }

    // End of FindMax.java

    javac FindMax.java
    java FindMax 10 -10 42 -301 4213 4444
    Daniel Pitts, Nov 22, 2006
    #5
  6. w00

    Lew Guest

    Hendrik Maryns wrote:

    > Maybe you should choose a better number to initiate prevNum with. (Some
    > static number in Integer comes to mind.)


    Or you could use element [0] of the array.

    - Lew
    Lew, Nov 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prem_eda
    Replies:
    5
    Views:
    7,845
    Pieter Hulshoff
    Oct 11, 2004
  2. Replies:
    29
    Views:
    757
    Ian Collins
    Mar 18, 2007
  3. EK
    Replies:
    0
    Views:
    315
  4. '2+
    Replies:
    1
    Views:
    328
  5. Javier Esteve

    biggest number

    Javier Esteve, Apr 25, 2009, in forum: Ruby
    Replies:
    2
    Views:
    115
    Joel VanderWerf
    Apr 25, 2009
Loading...

Share This Page