Click here to Skip to main content
16,019,618 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to find the biggest number in an array in java?
i have tried this code and its showing exception can anyone help me out to debug the code.

What I have tried:

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// System.out.println("enter the no of number you want to enter");
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int digits[]=new int[a];
for(int i=0;i<a;i++)
{
Scanner number=new Scanner(System.in);
int inputnumber=number.nextInt();
digits[i]=inputnumber;
}
int max;
max=digits[0];
for(int j=0;j<a;j++)
{
if(digits[j+1]>digits[j])
{
max=digits[j+1];
}
}
System.out.println(max);


}
}
Posted
Updated 13-May-17 22:20pm
Comments
[no name] 13-May-17 18:08pm    
Google the exception, find out what the exception means, then logically apply the exception message to your code, then fix your code so that whatever the exception is, goes away.
Patrice T 13-May-17 18:09pm    
"i have tried this code and its showing exception"
Can you show exception too and position too

you could use a basic looping statement to find the maximum value

Java
<pre>class Biggest
{
	public static void main(String args[])
	{
		int[] arr = {1,2,3,4,5,6,7,8,9};
		int currentBiggestNo= Integer.MIN_VALUE; //do not assign 0
                //if every integers present in the array are negative then you cannot find the biggest one since 0 is bigger than all but not present in the array..
		for(int number:arr)
		{
			if(number>currentBiggestNo)currentBiggestNo=number;
		}
		System.out.println(currentBiggestNo);
	}
}
 
Share this answer
 
v2
Quote:
i have tried this code and its showing exception can anyone help me out to debug the code.

Use the debugger to see what the code is doing and why you get an exception.
Nota: The text of exception and position may help us to see the reason.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900