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: Trying to write a program that reads from StdIn and findes the highest value

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Trying to write a program that reads from StdIn and findes the highest value

    Hi!
    I'm trying to write a program that counts the highest value in a matrix .
    It does'nt work. What is wrong with it? If I put System.out print at bottom of main all hell breaks loose.
    So...here it is:

    public class 
    {
    	public static void main(String[] args) {
    	//public static double[] [] readDouble2D()
     
    			int M = StdIn.readInt();
    			int N = StdIn.readInt();
    			double[] [] a = new double [M] [N];
    			for (int i = 0; i<M;i++)
    				for (int j = 0; j < N;j++)
    					a[i][j] = StdIn.readDouble();
    				return a;
     
    		}
    	public static double max(double[] a)
    		{
    			double max = Double.NEGATIVE_INFINITY;
    			for(int i = 0; i < a.length;i++)
    				if (a[i] > max) max = a[i];
    			return max;
    		}
    }




    return a;
    ^
    1 error



     
    public class p151 {
      public static void main(String[] args) {
     
     
        int max = Integer.MAX_VALUE;
     
        while (!StdIn.isEmpty()) {
          int N = StdIn.readInt();
          if (N > max) {
            max = N;
        }
        System.out.println( max);
      }
    }

    I can't use this because it only finds the highest number in an array.It does'nt count them.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to write a program that reads from StdIn and findes the highest value

    The error message is quite helpful. No 'void' method can have a return statement. Why is the return statement in your void main() method? What are you trying to do?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    JohnJohnson123 (September 30th, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Trying to write a program that reads from StdIn and findes the highest value

    My program is supposed to read a file and return the number of highest value in the terrain, count the peaks so to speak.

    My problem is the scope and print out.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to write a program that reads from StdIn and findes the highest value

    Specifically, is the "return a;" statement in main() required? I suspect not, especially since it's not allowed in a void method which main() must be.

    To print the value returned by your method max(), do something like:
    double maxValue = max( a );
    System.out.println( "The max is: " + maxValue );
    The 2 lines of code could be condensed to one, but I wanted to show the detail so you might understand it better.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    JohnJohnson123 (September 30th, 2013)

  7. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Trying to write a program that reads from StdIn and findes the highest value

    When I put the print statement in main and the second error message in static.

    This is my problem a have got the concept, almost(hahah), my problem is the scope and chooing the right method and statements.

    C:\P1529.java
    1529.java:12: error: cannot return a value from method whose
     result type is void
                                    return a;
                                           ^
    P1529.java:21: error: cannot find symbol
                            System.out.println("The max is: " + MaxValue);
                                                                ^
      symbol:   variable MaxValue
     
    2 errors
     
    C:\>javac P529.java
    1529.java:13: error: method max in class  cannot be applied to given types;
                                    double maxValue=max(a);
                                                    ^
      required: double[]
      found: double[][]
      reason: actual argument double[][] cannot be converted to double[] by method i
    nvocation conversion
    isthispossibleidoubt1529.java:14: error: cannot find symbol
                            System.out.println("The max is: " + MaxValue);
                                                                ^
      symbol:   variable MaxValue
      location: class 1529
    2 errors
    [COLOR="Silver"]


    --- Update ---

    This is the sad version that I got now.

    public class 
    {
    	public static void main(String[] args) {
    	//public static double[] [] readDouble2D()
     
    			int M = StdIn.readInt();
    			int N = StdIn.readInt();
    			double[] [] a = new double [M] [N];
    			for (int i = 0; i<M;i++)
    				for (int j = 0; j < N;j++)
    					a[i][j] = StdIn.readDouble();
    				//return a;
    				double maxValue=max(a);
    			System.out.println("The max is: " + maxValue);
    		}
    	public static double max(double[] [] a)
    		{
    			double max = Double.NEGATIVE_INFINITY;
    			for(int i = 0; i < a.length;i++)
    				for(int j=0;j<N;j++)
    				if (a[i] [i] > max) max = a[i] [i];
    			return max;
    		}
    }

    When I pipe in #4 I get:

    The max is:1.0

    Update:
    I think I should have it like this at the end: if (a[i]>max)

  8. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Trying to write a program that reads from StdIn and findes the highest value

    Check this out.

    What is wrong?
    public class
    {
    	//Finding the max and counting them
    	public static double max(double[] a, int lo, int hi) {
            if (lo < 0 || hi >= a.length || lo > hi)
                throw new RuntimeException("Subarray indices out of bounds");
            double max = Double.NEGATIVE_INFINITY;
            for (int i = lo; i <= hi; i++) {
                if (a[i] > max)
    			{
    			max = a[i];
    			MaxCounter++;
    			}
            }
            return max;
        }
     
    	public static void main (String[] args)
     
    	//(double[][] readDouble2D())
    	{
    	//Reading the array
    		int M = StdIn.readInt();
    		int N = StdIn.readInt();
    		int MaxCounter=0;
    		double[][] a = new double[M] [N];
    		for (int i = 0; i < M; i++)
    			for (int j = 0; j < N; j++)
    				a[i][j] = StdIn.readDouble();
    				int maxValue = max(a);
     
    				StdOut.println(MaxCounter);
    	}
    }
    Erorr:

    2.java:31: error: method max in class 2 cannot be applied to given types;
    int maxValue = max(a);
    ^
    required: double[],int,int
    found: double[][]
    reason: actual and formal argument lists differ in length
    1 error

  9. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to write a program that reads from StdIn and findes the highest value

    What about that error message don't you understand? I realize they can be a bit cryptic (sometimes), especially if you haven't taken the time to READ them and apply them to your code and the error about which they complain. Let's break it down:

    Omg2.java:31: error: method max in class Omg2 cannot be applied to given types;
    int maxValue = max(a);

    "31: method max cannot be applied to give types" - that means at line 31 the method max has been called with incorrect parameters.

    The next line points to the offending statement (The caret, '^', actually points to the occurrence of the error if the formatting is preserved.)

    "required: double[],int,int
    found: double[][]"

    The first line indicates the parameters required by the method max().
    The second line indicates the parameter(s) found in the offending call. Since the parameter(s) found don't match the parameters required, there's an error.

    Solution: Modify the code, either the method signature or the method call, so that the parameters match.

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    JohnJohnson123 (October 1st, 2013)

Similar Threads

  1. Java program that reads a file
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2013, 06:52 AM
  2. Replies: 11
    Last Post: March 10th, 2013, 07:40 PM
  3. stdIn.nextLine(); or stdIn.next(); are pulling the incorrect variables
    By kamari0 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 11th, 2013, 11:09 PM
  4. Write a Java program that reads file
    By Mehwish-S in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 28th, 2012, 03:00 PM
  5. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM