Trying to create a 2 dimensional array for converting temperature and returning max. Having problems declaring max such as these 2 lines.
max = max(inputFahr[0][0]);
max2 = max2(inputCel[0][1]);

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project3;

import java.util.Scanner;/**
 *
  */
public class Program7 {

    /**
     * @param args the command line arguments
     */
      public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      
      	double fahrenheit; // degrees in Fahrenheit
	double celsius; // degrees in Celsius
	int choice; // 1 for F to C and 2 for C to F
        celsius = 0;
        fahrenheit = 0;
        double max;
        double max2;
         max = max(inputFahr[0][0]);
         max2 = max2(inputCel[0][1]);

      do {
      System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
      choice = input.nextInt();

      if (choice == 1)
          celsius = tempC(celsius);
      else if (choice == 2)
	  fahrenheit = tempF(fahrenheit);
      else if (choice == 3)
		exit();
      else System.out.println("Invalid selection");
        System.out.println("Maximum temperature is " + max);
        System.out.println("Maximum temperature is " + max2);
      }  while (choice != 3);  
        
    }

 public static double tempC (double celsius) {
    Scanner input = new Scanner(System.in);
        System.out.print("Enter degrees fahrenheit");
 double fahrenheit = input.nextDouble();
 double[][] inputFahr = new double[10][2];
     for (int i = 0; i < inputFahr.length; i++)
    inputFahr[i][0] = fahrenheit ;
 celsius = (5.0/9)*(fahrenheit - 32);
 for (int i = 0; i < inputFahr.length; i++)
     inputFahr[i][1] = celsius; 
 System.out.println(fahrenheit + " degrees fahrenheit equals " +
            celsius + " degrees celsius");
 return celsius;
 }
 public static double max(double[][] inputFahr) {
  double max = inputFahr[0][0];
     for (int i = 0; i < inputFahr[i].length; i++) {
  if (inputFahr[i][0] > max)
    max = inputFahr[i][0];
 }
return max;
 }      

public static double tempF (double fahrenheit) {
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter degrees celsius");
  double celsius = input.nextDouble();
  double[][] inputCel = new double[10][2];  
   for (int i = 0; i < inputCel.length; i++)
    inputCel[i][1] = celsius ;
  fahrenheit = (9.0/5)* celsius + 32;
    for (int i = 0; i < inputCel.length; i++)
     inputCel[i][0] = fahrenheit; 
   System.out.println(celsius + " degrees celsius equals " +
            fahrenheit + " degrees fahrenheit");
    return fahrenheit;
}

 public static double max2(double[][] inputCel) {
  
    double max2 = inputCel[0][1];
    for (int i = 0; i < inputCel[i].length; i++){
 if (inputCel[i][1] > max2)
    max2 = inputCel[i][1];
 }
return max2;
 } 

public static void exit (){
    System.out.println("Goodbye");
        // TODO code application logic here
}
 

      
      }

Recommended Answers

All 14 Replies

Don't declare variables with the same names as methods and if you want to share those arrays between methods, you'll need to declare them at the class level. Currently they are local to your methods and are not visible to any code outside those methods.

ok. this is what I have now, but I still need help declaring max and max 2

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project3;

import java.util.Scanner;/**
 *
 */
public class Program7 {

    /**
     * @param args the command line arguments
     */
      public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      
      	double fahrenheit; // degrees in Fahrenheit
	double celsius; // degrees in Celsius
	int choice; // 1 for F to C and 2 for C to F
        celsius = 0;
        fahrenheit = 0;
        double max;
        double max2;
         max = maxF(inputFahr[0][0]);
         max2 = maxC(inputCel[0][1]);

      do {
      System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
      choice = input.nextInt();

      if (choice == 1)
          celsius = tempC(celsius);
      else if (choice == 2)
	  fahrenheit = tempF(fahrenheit);
      else if (choice == 3)
		exit();
      else System.out.println("Invalid selection");
        System.out.println("Maximum temperature is " + max);
        System.out.println("Maximum temperature is " + max2);
      }  while (choice != 3);  
        
    }

 public static double tempC (double celsius) {
    Scanner input = new Scanner(System.in);
        System.out.print("Enter degrees fahrenheit");
 double fahrenheit = input.nextDouble();
 double[][] inputFahr = new double[10][2];
     for (int i = 0; i < inputFahr.length; i++)
    inputFahr[i][0] = fahrenheit ;
 celsius = (5.0/9)*(fahrenheit - 32);
 for (int i = 0; i < inputFahr.length; i++)
     inputFahr[i][1] = celsius; 
 System.out.println(fahrenheit + " degrees fahrenheit equals " +
            celsius + " degrees celsius");
 return celsius;
 }
 public static double maxF(double[][] inputFahr) {
  double max = inputFahr[0][0];
     for (int i = 0; i < inputFahr[i].length; i++) {
  if (inputFahr[i][0] > max)
    max = inputFahr[i][0];
 }
return max;
 }      

public static double tempF (double fahrenheit) {
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter degrees celsius");
  double celsius = input.nextDouble();
  double[][] inputCel = new double[10][2];  
   for (int i = 0; i < inputCel.length; i++)
    inputCel[i][1] = celsius ;
  fahrenheit = (9.0/5)* celsius + 32;
    for (int i = 0; i < inputCel.length; i++)
     inputCel[i][0] = fahrenheit; 
   System.out.println(celsius + " degrees celsius equals " +
            fahrenheit + " degrees fahrenheit");
    return fahrenheit;
}

 public static double maxC(double[][] inputCel) {
  
    double max2 = inputCel[0][1];
    for (int i = 0; i < inputCel[i].length; i++){
 if (inputCel[i][1] > max2)
    max2 = inputCel[i][1];
 }
return max2;
 } 

public static void exit (){
    System.out.println("Goodbye");
        // TODO code application logic here
}
 

      
      }

Ok this is what I have. After putting in one temerature it tells me maximum is 0.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project3;

import java.util.Scanner;/**
 *
 * 
 */
public class Program7 {

    /**
     * @param args the command line arguments
     */
      public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      
      	double fahrenheit; // degrees in Fahrenheit
	double celsius; // degrees in Celsius
	int choice; // 1 for F to C and 2 for C to F
        celsius = 0;
        fahrenheit = 0;
        double max;
        double max2;
        double[][] inputFahr = new double[10][2];
        double[][] inputCel = new double[10][2];
        max = maxF(inputFahr);
        max2 = maxC(inputCel);

      do {
      System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
      choice = input.nextInt();

      if (choice == 1)
          celsius = tempC(celsius);
      else if (choice == 2)
	  fahrenheit = tempF(fahrenheit);
      else if (choice == 3)
		exit();
      else System.out.println("Invalid selection");
        System.out.println("Maximum temperature is " + max);
        System.out.println("Maximum temperature is " + max2);
      }  while (choice != 3);  
        
    }

 public static double tempC (double celsius) {
    Scanner input = new Scanner(System.in);
        System.out.print("Enter degrees fahrenheit");
 double fahrenheit = input.nextDouble();
 double[][] inputFahr = new double[10][2];
      for (int i = 0; i < inputFahr.length; i++)
    inputFahr[i][0] = fahrenheit ;
 celsius = (5.0/9)*(fahrenheit - 32);
 for (int i = 0; i < inputFahr.length; i++)
     inputFahr[i][1] = celsius; 
 System.out.println(fahrenheit + " degrees fahrenheit equals " +
            celsius + " degrees celsius");
 return celsius;
 }
 public static double maxF(double[][] inputFahr) {
  double max = inputFahr[0][0];
     for (int i = 0; i < inputFahr[0].length; i++) {
  if (inputFahr[i][0] > max)
  {
    max = inputFahr[i][0];
  }
 }
return max;
 }      

public static double tempF (double fahrenheit) {
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter degrees celsius");
  double celsius = input.nextDouble();
  double[][] inputCel = new double[10][2];  
   for (int i = 0; i < inputCel.length; i++)
    inputCel[i][1] = celsius ;
  fahrenheit = (9.0/5)* celsius + 32;
    for (int i = 0; i < inputCel.length; i++)
     inputCel[i][0] = fahrenheit; 
   System.out.println(celsius + " degrees celsius equals " +
            fahrenheit + " degrees fahrenheit");
    return fahrenheit;
}

 public static double maxC(double[][] inputCel) {
  
    double max2 = inputCel[0][1];
    for (int i = 0; i < inputCel[0].length; i++){
 if (inputCel[i][1] > max2)
 {    max2 = inputCel[i][1];
 }
 }
return max2;
 } 

public static void exit (){
    System.out.println("Goodbye");
        // TODO code application logic here
}
 

      
      }

It happened because you calling maxF and maxC methods before you are filling arrays inputFahr and inputCel with data. And your maximum goes through arrays, filled by default values (in your case it's zeros).

And one more thing: (correct me if I'm wrong) you want the user to input each number directly. If so, you should correct your methods, what filling arrays. You have only this to read data entered by user to fill arrays:

double fahrenheit = input.nextDouble();

and

double celsius = input.nextDouble();

that means, that you're reading only the first value, entered by user and filling by it whole array. If it must be different values (within the limits of each array), you should place that into cycle.

commented: Helpful advice. +16

Should I not use input.nextDouble? Then what should I put for user to input temperature?

Atenka is saying that you are only collecting a single value from the user, but you are using an array in your method. If you need many values in an array, you need to prompt the user to enter each one. The "nextDouble" part is just fine - you just aren't collecting enough values to fill the array.

this is the code i have now. it says maximum is 0 after i push 3 to end program.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project3;

import java.util.Scanner;/**
 *
 * 
 */
public class Program7 {

    /**
     * @param args the command line arguments
     */
      public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      
      	double fahrenheit = 0; // degrees in Fahrenheit
	double celsius = 0; // degrees in Celsius
	int choice; // 1 for F to C and 2 for C to F
        double[] inputFahr = new double[10];
        double[] inputCel = new double[10];
        double max;
        double max2;
        max = maxF(inputFahr);
        max2 = maxC(inputCel);

 

      do {
      System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
      choice = input.nextInt();

      if (choice == 1)
          celsius = tempC(celsius);
      else if (choice == 2)
	  fahrenheit = tempF(fahrenheit);
      else if (choice == 3)
		exit();
      else System.out.println("Invalid selection");
        
      }  while (choice != 3);  
     
      System.out.println("Maximum temperature is " + max + " fahrenheit");
      System.out.println("Maximum temperature is " + max2 + " celsius");   
    }

 public static double tempC (double celsius) {
    Scanner input = new Scanner(System.in);
        System.out.print("Enter degrees fahrenheit");
 double fahrenheit = input.nextDouble();
        double[] inputFahr = new double[10];
       for (int i = 0; i < inputFahr.length; i++)
    inputFahr[i] = fahrenheit;
 celsius = (5.0/9)*(fahrenheit - 32);
 
 System.out.println(fahrenheit + " degrees fahrenheit equals " +
            celsius + " degrees celsius");
 return celsius;
 }
 public static double maxF(double[] inputFahr) {
  double max = 0;
     for (int i = 0; i < inputFahr.length; i++) {
  if (inputFahr[i] > max)
  {
    max = inputFahr[i];
  }
 }
return max;
 }      

public static double tempF (double fahrenheit) {
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter degrees celsius");
  double celsius = input.nextDouble();
  double[] inputCel = new double[10];  
   for (int i = 0; i < inputCel.length; i++)
    inputCel[i] = celsius ;
  fahrenheit = (9.0/5)* celsius + 32;
     System.out.println(celsius + " degrees celsius equals " +
            fahrenheit + " degrees fahrenheit");
    return fahrenheit;
}

 public static double maxC(double[] inputCel) {
  
    double max2 = 0;
    for (int i = 0; i < inputCel.length; i++){
 if (inputCel[i] > max2)
 {    max2 = inputCel[i];
 }
 }
return max2;
 } 

public static void exit (){
    System.out.println("Goodbye");
        // TODO code application logic here
}
 

      
      }
max = maxF(inputFahr);
        max2 = maxC(inputCel);

Err.. Doc, that happens cause, you are calculating the maximum temperature even before you have taken the input. By default in Java primitive numeric data types are initialized to "0", hence your Arrays -- inputFahr, inputCel contain only zeroes in them, when you se them in the above two statements and hence the maximum value returned to you is zero.

Also you are not really using the inputCel and inputFahr arrays declared in the main method, (Note -- the inputCel and inputFahr arrays declared in the tempC() and tempF() methods respectively are different.)
<EDIT>
If you ant to just fix your Code as opposed to programming it correctly, just declare the the two arrays "inputCel" and "inputFahr" as static class members and remove their corresponding declarations from the main and the tempC() and temF() methods.
Also move the two statements mentioned at the top of this post to below the while loop.
</EDIT>

Another point I would like to mention is you have currently used Java like any other Structured Programming Language my suggestion is the true power of Java lies in using it i the OOP way. You can check the sticky at the top for more links on improving your Java knowledge and Learn more about object oriented programming, here is also another useful link.

Another request would be to align your code consistently, it kind off looks awful to read the way it is currently. In case you would like know more about Coding Conventions in Java you can go here.

there.
if it works good for you mark thread as solved, and please read more about java or oo programing in general.

import java.util.ArrayList;
import java.util.Scanner;

public class Program7 {

    //if you dont know how many data you want to store, use some dynamic structure
    //and make them availible in all class!!!

    public static ArrayList <Double> inputFahr = new ArrayList();
    public static ArrayList <Double> inputCel = new ArrayList();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // do NOT use same variable names in diffrend code blocks its very confusing
        // read about variable range, because this is your main mistake
        // most of the variables in the main method was not really needed

        int choice; // 1 for F to C and 2 for C to F

        do {
            System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
            choice = input.nextInt();

            if (choice == 1) {
                tempC();
            } else if (choice == 2) {
                tempF();
            } else if (choice == 3) {
                exit();
            } else {
                System.out.println("Invalid selection");
            }

        } while (choice != 3);

        //why to pass function return value to variable if you use it only once?

        System.out.println("Maximum temperature is " + max(inputFahr) + " fahrenheit");
        System.out.println("Maximum temperature is " + max(inputCel) + " celsius");
    }

    // you get the value inside the method so you dont need to pass an argument
    public static double tempC() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter degrees fahrenheit");
        double fahrenheit = input.nextDouble();


        double celsius = (5.0 / 9) * (fahrenheit - 32);
        
        // adding entered value as well as converted one into teperatures arrays
        inputFahr.add(fahrenheit);
        inputCel.add(celsius);

        System.out.println(fahrenheit + " degrees fahrenheit equals " +
                celsius + " degrees celsius");
        return celsius;
    }


    public static double tempF() {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter degrees celsius");
        double celsius = input.nextDouble();

        double fahrenheit = (9.0 / 5) * celsius + 32;

        // adding entered value as well as converted one into teperatures arrays
        inputFahr.add(fahrenheit);
        inputCel.add(celsius);

        System.out.println(celsius + " degrees celsius equals " +
                fahrenheit + " degrees fahrenheit");
        return fahrenheit;
    }

    /// you need only one max method, list as a parameter 

    public static double max(ArrayList <Double> list) {

        double result = 0;
        for (double p : list) {
            if (p > result) {
                result = p;
            }
        }
        return result;
    }

    public static void exit() {
        System.out.println("Goodbye");
    // TODO code application logic here
    }
}

works great except it is only supposed to get 10 inputs. Can you explain the p in max class or provide a site where I can read about it, I have never seen this before.

the loop in max method is a foreach loop, so this:

for (double p : list) {
            if (p > result) {
                result = p;
            }
        }

have the same result as:

for (int i=0; i< list.size(); i++) {
            double p = list.get(i);
            if (p > result) {
                result = p;
            }
        }

more about foreach is here

i hope you can figure out reducing input to max 10 elements by your own

Sorry but I haven't learned about arraylist yet. I tried puttin 10 :
public static ArrayList <Double> inputFahr = new ArrayList <Double>(10);
but that didn't work. everything else i tried created errors. i've tried searching for information about arraylist, but cannot find anything that tells me how to set the size and I really don't know what to do.

You need to count how many elements are inserted and when you reach 10 exit.

If I understood correctly you will need this:

int count = 0;

do {
System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
choice = input.nextInt();
if (choice == 1) {
count++;
tempC();
} else if (choice == 2) {
count++;
tempF();
} else if (choice == 3) {
exit();
} else {
System.out.println("Invalid selection");
}
if (count==10) {
  choice = 3;
}
} while (choice != 3);

and about arraylist: it changes size dynamically, that's the beauty of it.
the restriction can be put in main loop just like javaAddict wrote

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.