Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java program to find the minimum and maximum values of input data

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java program to find the minimum and maximum values of input data

    Hey i'm writing this code...and well.

    I'm very new to java ><...

    There are two things that I am trying to do,

    Basically, without using Arrays,

    - I want to write a program that asks the user how many integers that they want to input, then have the user input those variables and then have the program determine and display the minimum and maximum values of whatever the user inputs.
    This is what I have so far but it isn't working...

    // Import Scanner Class
    import java.util.Scanner;
    public class maxMinValues
    {
    // main method is the start point of java applications
    public static void main(String args[])
    {
    // variable to read user input
    Scanner input = new Scanner(System.in);
     
    //Title
    System.out.printf("\t\n Finding the largest & smallest Value ");
    System.out.printf("\t\n How many integers are ");
     
    //Variables
     
    int lowValue=0;
    int a=0;
    int b=0;
    int c=0;
    int d=0;
    int e=0;
     
    while ( a >= 0 )
    {
    System.out.printf("\n\n Enter Integer: ");
    a = input.nextInt();
     
    if ( a == -100 )
     
    {
    break;
    }
    } 
     
    if (a <= b && a <= c && a <= d && a <= e || a == b || a == c || a == d || a == e)
    lowValue = a;
    else if (b <= a && b <= c && b <= d && b <= e || a == b || b == c || b == d || b == e)
    lowValue = b;
    else if (c <= a && c <= b && c <= d && c <= e || c == a || c == b || c == d || c == e)
    lowValue = c;
    else if (d <= a && d <= b && d <= c && d <= e || d == a || d == b || d == c || d == e)
    lowValue = d;
    else if (e <= a && e <= b && e <= c && e <= d || e == a || e == b || e == c || e == d)
    lowValue = e;
    System.out.printf ("\n Min Value:%d", lowValue); 
    }
    int highValue=0;
    int a=0;
    int b=0;
    int c=0;
    int d=0;
    int e=0; 
    {
    if (a >= b && a >= c && a >= d && a >= e)
    highValue = a;
    else if (b >= a && b >= c && b >= d && b >= e)
    highValue = b;
    else if (c >= a && c >= b && c >= d && c >= e)
    highValue = c;
    else if (d >= a && d >= b && d >= c && d >= e)
    highValue = d;
    else if (e >= a && e >= b && e >= c && e >= d)
    highValue = e;
    System.out.printf ("\n Max Value:%d", highValue); 
     
     
    } // end main()
     
    } // end class

    Then...

    - program that converts temperatures from -5 to 15 C. by increments of 1 to Fahrenheit. Repeating it 3 times 1 with a for loop, 1 with while and 1 with do.

    // Import Scanner Class
    import java.util.Scanner;
    public class tempConvert
    {
    // main method is the start point of java applications
    public static void main(String args[])
    {
    // variable to read user input
    Scanner input = new Scanner(System.in);
     
    //Title
    System.out.printf("\t\n Celcius To Fahrenheit (-5 to 15) ");
     
    int getFahrenheit=0;
     
     
    for (int f= getFahrenheit(); f <= 212; f= getFahrenheit())
    System.out.println(f+"F = "+convertToC(f)+"C");
     
    System.out.println("Enter temperature in "); // prompt for input
    x = input.nextInt(); // read first integer in farenheit
     
    result = (5.0/9)*(x-32); 
    System.out.println("Fahrenheit Celsius", result);
     
    } //end method main
     
    } // end class

    - Im not sure how to input the other 2 loops aswell >< ugh.

    no idea what to do after this point. I'm using blueJ btw:


    if someone could help that would be wonderful.

    thanks. >.<
    Last edited by Deep_4; November 7th, 2012 at 01:03 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: code help 2 programs. MinMax Values & Converting Temps.

    Hello awake77.

    Welcome to the Java Programming Forums.

    I am going to look at your first program to start with..

    I am a bit confused as to what you want it to do. Can you please give me an example input and expected output?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: code help 2 programs. MinMax Values & Converting Temps.

    For a start off I sure your class name should start with a capital letter, (Java Convention ) so
    public class maxMinValues
    should read
    public class MaxMinValues

    To answer JavaPF's question
    What the program should do, we have 5 variables 'a' to 'e', the program finds the lowest number and the highest number, example lets say we have a=3 b=4 c=1 d=9 and e=4
    it gives 1 as the lowest number and 9 as the highest number.

    but I see we have trouble getting the numbers into 'a' to 'e' you could just change the code assign a number to the variable when you declare it, and make sure that part of the code works. hang on, why not just have a array of variables like a[0] to a[4] then you input the numbers that way otherwise you would have to repeat the input variable part of the code 5 times.

    like

    System.out.printf("\n\n Enter Integer A: ");
    a = input.nextInt();

    System.out.printf("\n\n Enter Integer B: ");
    b = input.nextInt();
    Last edited by Eric; December 2nd, 2008 at 12:07 PM. Reason: class name should start with a capital letter, (Java Convention )

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: code help 2 programs. MinMax Values & Converting Temps.

    Quote Originally Posted by Eric View Post
    hang on, why not just have a array of variables like a[0] to a[4] then you input the numbers that way otherwise you would have to repeat the input variable part of the code 5 times.

    like
    System.out.printf("\n\n Enter Integer A: ");
    a = input.nextInt();

    System.out.printf("\n\n Enter Integer B: ");
    b = input.nextInt();
    I was thinking the same thing but apparently he cannot use an Array? Check the first post..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: code help 2 programs. MinMax Values & Converting Temps.

    As for the second program there is a error with

    for (int f= getFahrenheit(); f <= 212; f= getFahrenheit())
    System.out.println(f+"F = "+convertToC(f)+"C");

    I might try

    for (int f= getFahrenheit(); f <= 212; getFahrenheit++)
    System.out.println(f+"F = "+convertToC(f)+"C");
    Last edited by Eric; November 29th, 2008 at 01:19 AM.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: code help 2 programs. MinMax Values & Converting Temps.

    Is there a reason why you cant use Arrays? Here is an example ive coded using Arrays which works perfectly:

    import java.util.Scanner;
    import java.util.Arrays;
     
    public class MaxMinValues {
     
     public static void main(String[] args) {
     
      int largest;
      int smallest;
     
      int numOfInt = 0;
      int[] vars;
      int count = 1;
     
      Scanner input = new Scanner(System.in);
     
      System.out.println("\t\n Finding the largest & smallest number.");
      System.out.println("\t\n How many int values?");
      System.out.println("");
     
      numOfInt = input.nextInt();
      vars = new int[numOfInt];
     
      for (int a = 0; a < numOfInt; a++) {
     
       System.out.println("");
       System.out.println("Enter value for int #" + count + ":");
       input = new Scanner(System.in);
       vars[a] = input.nextInt();
       count++;
     
      }
     
      System.out.println("");
      System.out.println("All " + numOfInt + " int values entered.");
      System.out.println("");
     
      Arrays.sort(vars);
     
      smallest = vars[0];
      largest = vars[numOfInt - 1];
     
      System.out.println("SMALLEST int value is " + smallest);
      System.out.println("LARGEST int value is " + largest);
     }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: code help 2 programs. MinMax Values & Converting Temps.

    I am still waiting for my Hexadecimal answer on a different thread

    ............ in the mean time only a country full of twitters would want to keep the Fahrenheit system.. Water the most useful liquid on the plant freezes at 0ºC and evaporates at 100ºC
    Tell me/us what freezes at 0ºF and evaporates at 100ºF

    OK sorry about that, I must go back and focus on Java, have a nice day.

Similar Threads

  1. GUI program won't take 'char' input code, can't seem to find the correct one...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2012, 07:12 AM
  2. when i run the program to find the y values it always prints y2 instead
    By jamesR in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 30th, 2012, 08:38 AM
  3. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  4. Replies: 1
    Last Post: June 11th, 2011, 05:39 AM
  5. [ask]input data from java file to another java
    By bontet in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 29th, 2010, 10:50 AM