Alex Verstivskiy
Alex Verstivskiy

Reputation: 13

initialization problems?

I am trying to make a program that lets the user enter an unknown value of names and then output the longest name entered. This is my code so far. When i compile I have several errors and they are all the same "cannot find symbol". Do i need to initialize those variables if so where?

import java.util.Scanner; 
public class Name 
{ 
    public static void main(String[] args) 
    { 
        Scanner kb = new Scanner(System.in); 
        longestName(kb); 
    } 
    public static void longestName(Scanner sc) 
    {

        String name=kb.nextLine();
        biggestName=name;
        System.out.println("Type -1 if you want to quit");
        int number=kb.nextInt();
        While (number !=-1); 
        { 
            String name1=kb.nextLine();
            if (name1.length() > biggestName)
            {
                biggestName=name1;
            }
            System.out.println("Do you want to continue? Type -1 to quit.");
            int number1=kb.nextInt();
        }
        System.out.println("Longest name is "+biggestName);

    }
}

Thanks for the help guys fixed the errors, and some other changes and the program gives the correct output.

import java.util.Scanner; 
public class Name 
{    
    public static void main(String[] args) 
    { 
        Scanner kb = new Scanner(System.in); 
        longestName(kb); 
    } 
    public static void longestName(Scanner kb) 
    {

        String biggestName;
        System.out.println("Enter the first name");
        String name=kb.nextLine();
        biggestName=name;
        System.out.println("Type -1 if you want to quit");
        int number=kb.nextInt();
        while (number !=-1) 
        { 
            System.out.println("Enter another name");
            Scanner kb1 = new Scanner(System.in);
            String name1=kb1.nextLine();
            int length1=biggestName.length();
            int length2=name1.length();
            if (length2 > length1)
            {
                biggestName=name1;
            }
            System.out.println("Do you want to continue? Type -1 to quit.");
            number=kb.nextInt();
        }
        System.out.println("Longest name is "+biggestName);

    }
}

Upvotes: 0

Views: 165

Answers (5)

Devadas
Devadas

Reputation: 93

I can see the below problems in the code:

  1. longestName() method should be using the reference name sc instead of kb (since kb is having scope only in main method)
  2. The variable biggestName is not declared. It should be either declared as a class variable or a variable in longestName() method and should be of type String
  3. It is not While, it is while with 'w' in smaller case
  4. There should not be a semicolon after the while statement
  5. At the end of the while loop, the number to be compared for the while loop is to be calculated and is currently assigned to wrong variable. kb.nextInt() should be assigned to variable number and not to number1 since the variable number1 is never read/used.
  6. The > operator can not be applied for String types. In the line if (name1.length() > biggestName), we are comparing int with String and will result in compilation error. The line should be modified as if (name1.length() > biggestName.length())
  7. Method nextInt() will cause InputMismatchException to be thrown if you are providing an input which is not a number.

Now I feel I should have written a corrected code like Joe Elleson did. But hope this answer helps.

Upvotes: 0

Joe Elleson
Joe Elleson

Reputation: 1373

There are quite a few errors in your code. Without explaining every error in detail, here is an example of a modified version which works:

import java.util.Scanner; 
public class Name 
{ 
    public static void main(String[] args) 
    { 
        Scanner kb = new Scanner(System.in); 
        longestName(kb); 
    } 
    public static void longestName(Scanner sc) 
    {
        System.out.println("Enter name, or type '-1' if you want to quit");

        String name=sc.nextLine();
        String biggestName="";

        while (!name.equals("-1")) 
        { 
            if (name.length() > biggestName.length())
            {
                biggestName=name;
            }
            name=sc.nextLine();
        }
        System.out.println("Longest name is "+biggestName);

    }
}

Upvotes: 1

rgettman
rgettman

Reputation: 178333

You passed in your Scanner to longestName, but in longestName, you named the parameter sc. Use sc instead of kb in longestName.

Use lowercase while instead of While; remove the semicolon following the while; a semicolon there means that that is the body, instead of the { } block below it.

I assume that at the bottom of the while loop, that you want to assign the next integer to number, not a new variable number1 that immediately goes out of scope.

You didn't declare what biggestName is (or name).

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117675

1-

public static void longestName(Scanner sc)

Either change the name of the scanner to kb, or change every kb within the method to sc.

2- See Scanner issue when using nextLine after nextXXX

3- Use while instead of While, and remove the ;.

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382464

Two errors here :

While (number !=-1); 

While should be while, and the ; makes an infinite loop.

And another problem is that you don't change number in the loop anyway.

Upvotes: 0

Related Questions