import java.util.Scanner;
public class LoopPatterns {

/**
* @param args
*/

public static double Largest (){
Scanner s = new Scanner(System.in);
int num, largest = 0;

for ( int i = 1; i <= 10 ;i ++) {
System.out.print("Enter a number : ");
num = s.nextInt();
if ( num > largest)
largest = num;
}
System.out.println("Largest Number : " + largest);
}


public static double First(){

String name1, name2, name3 ;
boolean b, b1;
int i, i1, i2,i3;
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter first string: ");
name1 = keyboard.nextLine();

System.out.print("Enter second string: ");
name2 = keyboard.nextLine();

System.out.print("Enter third string: ");
name3 = keyboard.nextLine();

b = name1.equals(name2);
// Check if identical ignoring case

b1 = name1.equalsIgnoreCase(name2);

i = name1.compareTo(name2);

if (i < 0)
{
i1 = name1.compareToIgnoreCase(name3);

if (i1 < 0)
{
System.out.println(name1);
if (i1 > 0)
{
System.out.println(name3);
i2 = name2.compareToIgnoreCase(name3);
if (i2<0)
{
System.out.println(name2);
System.out.println(name3);
}
if (i2>0)
{
System.out.println(name3);
System.out.println(name2);
}
}
}
}
else if (i > 0)
{

i1 = name2.compareToIgnoreCase(name3);

if (i1 < 0)
{
System.out.println(name2);
if (i1 > 0)
{
i2 = name1.compareToIgnoreCase(name3);
if (i2<0)
{
System.out.println(name1);
System.out.println(name3);
}
if (i2>0)
{
System.out.println(name3);
System.out.println(name1);
}
}
}
}

}

public static void main(String[] args) {
Largest();
First();

}
}

line 9,23- this method must return a type of double

Recommended Answers

All 3 Replies

In line 8, you declare that the function is to return a double, but you do not return a value in your function. At the end of the function, return a double or change the return type of the function.

public static int Largest()
{
    Scanner s = new Scanner(System.in);
    int num, largest;

    for (int i = 0; i < 10 ; ++i) 
    {
        System.out.print("Enter a number : ");
        num = s.nextInt();
        if (i == 0)
            largest = num;
        else if ( num > largest)
            largest = num;
    }
    System.out.println("Largest Number : " + largest);
    return largest;
}

The changes to your function that I made:
1. Applied proper indentation to make it easier to read. Also add a blank line after the variable declarations for the same reason.
2. Because you are asking the user for integers, and largest is int, then the return type should be int.
3. 0 might be a valid value, or all the values could be negative. Your method would return 0 as a sentinal value, which would be incorrect. The first time through the loop, assign to largest. On all other passes, compare the value to see if it is the new largest
4. I started at 0 instead of 1 and ending before 10. This will give 10 passes through the loop, but the comparison is a little simpler. Also, I changed the increment to a preincrement instead of postincrement; the performance is a little better.
5. Included the return value for the function.

I changed the increment to a preincrement instead of postincrement; the performance is a little better.

This may have been true some time in the distant past (by "a little better, maybe 0.00001% faster overall?), but for any current compiler these three forms should generate exactly the same optimised byte code:

for (int i = 0; i < 10 ; i++) ...
for (int i = 0; i < 10 ; ++i) ...
for (int i = 0; i < 10 ; i+=1) ...

In any case, trying to optimise a single op code like this is never a sensible use of time.

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.