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

Dani AI

Generated

The compiler error comes from the method signatures: both Largest() and First() are declared to return double but have no return on all code paths. Make the intent explicit — either return a value (and use it in main) or change the methods to void since they only print results. Also check the First() logic: several nested checks are mutually contradictory (for example if (i1 < 0) { ... if (i1 > 0) { ... } }) which looks like misplaced braces and will never execute as intended. Remove unused variables (b, b1, i3) and pick a clearer approach for ordering strings.

A simple, robust pattern is to create one Scanner in main, pass it to helper methods, and let those methods be void. This avoids multiple Scanners on System.in and makes resource management clearer.

Example: read ten integers safely, track the largest, and print it.

public static void printLargest(Scanner in) {
    int largest = Integer.MIN_VALUE;
    for (int i = 0; i < 10; i++) {
        System.out.print("Enter an integer: ");
        while (!in.hasNextInt()) {
            System.out.print("Please enter a valid integer: ");
            in.next();
        }
        int n = in.nextInt();
        if (n > largest) largest = n;
    }
    System.out.println("Largest Number: " + largest);
}

For the three-name alphabetical task, use an array and sort (case-insensitive) instead of deeply nested compares:

public static void printSortedNames(Scanner in) {
    String[] names = new String[3];
    in.nextLine(); // consume leftover newline if needed
    for (int i = 0; i < 3; i++) {
        System.out.print("Enter name " + (i+1) + ": ");
        names[i] = in.nextLine();
    }
    Arrays.sort(names, String.CASE_INSENSITIVE_ORDER);
    for (String s : names) System.out.println(s);
}

Follow-up tips: close the Scanner only in main, handle input mismatches, prefer meaningful names (printLargest, printSortedNames), and stick to Java naming conventions. correctly pointed out the missing return; is right that micro-optimizing i++ vs ++i is unnecessary.

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.