0

My enum type is not being changed from its default "null" value when displayed by a basic toString method. When the user inputs JPG,GIF, etc the if statement is supposed to recognize that the inputted string corresponds to enum. Why is the if statement not taking the user's input and converting it to match the enum?

public static void processPhotos()
{
    String value;
    String size;
    String name;
    String strType;
    double dSize;
    String photographer;
    int iValue = 1;
    Scanner kb = new Scanner(System.in);


    while(iValue>0)
    {
        System.out.print("Enter the Photo's name.");
        name = kb.nextLine();
        System.out.print ("\nEnter the Photo's type(JPG, GIF, PNG, BMP, or OTHER).");
        strType = kb.nextLine();
        strType = strType.toUpperCase ( );
        if(strType == "JPG")
        {
            type = Type.JPG;
        }
        if(strType == "GIF")
        {
            type = Type.GIF;
        }
        if(strType == "PNG")
        {
            type = Type.PNG;
        }
        if(strType == "BMP")
        {
            type = Type.BMP;
        }
        if(strType == "OTHER")
        {
            type = Type.OTHER;
        }

        System.out.print("\nEnter the Photo's size(IN Megabytes)");
        size = kb.nextLine();
        dSize = Double.parseDouble (size);
        System.out.print("\nEnter the Photo's Photographer");
        photographer = kb.nextLine();
        Photo p = new Photo(name,type,dSize,photographer);
        System.out.print (p.toString());
        System.out.print("\n\nEnter an integer greater than zero to continue. Enter ZERO to end.");
        value = kb.nextLine();
        iValue = Integer.parseInt(value);

    }
}

8 Answers8

1

Compare the strings with this approach instead.

if(strType.equals("JPG"))

Then you compare the two strings for having the same value.

The == compare that the two strings are in fact the same object stored in memory, of which they are not.

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
0

if(strType == "JPG")

it should be :

if(strType.equalsIgnoreCase( "JPG"))

so for every == change it to equalsIgnoreCase or equals

Ashish
  • 1,943
  • 2
  • 14
  • 17
0

You have to use equals instead of ==

type == Type.JPG // comparing references
Type.JPG.equals(type) //Compares valus
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
0

To compare strings you need to use.equals instead of == So your code should be if(strType.equals("JPG"))

public static void processPhotos()
{
    String value;
    String size;
    String name;
    String strType;
    double dSize;
    String photographer;
    int iValue = 1;
    Scanner kb = new Scanner(System.in);


    while(iValue>0)
    {
        System.out.print("Enter the Photo's name.");
        name = kb.nextLine();
        System.out.print ("\nEnter the Photo's type(JPG, GIF, PNG, BMP, or OTHER).");
        strType = kb.nextLine();
        strType = strType.toUpperCase ( );
        if(strType.equas("JPG"))
        {
            type = Type.JPG;
        }
        if(strType.equals("GIF")).
        {
            type = Type.GIF;
        }
        if(strType.equals("PNG"))
        {
            type = Type.PNG;
        }
        if(strType.equals("BMP"))
        {
            type = Type.BMP;
        }
        if(strType.equals("OTHER"))
        {
            type = Type.OTHER;
        }

        System.out.print("\nEnter the Photo's size(IN Megabytes)");
        size = kb.nextLine();
        dSize = Double.parseDouble (size);
        System.out.print("\nEnter the Photo's Photographer");
        photographer = kb.nextLine();
        Photo p = new Photo(name,type,dSize,photographer);
        System.out.print (p.toString());
        System.out.print("\n\nEnter an integer greater than zero to continue. Enter ZERO to end.");
        value = kb.nextLine();
        iValue = Integer.parseInt(value);

    }
}
Standin.Wolf
  • 1,224
  • 1
  • 10
  • 32
0

You dont need a switch case/condition check for this , you can convert your user input to enum as follows:

enum Type {
        JPG, GIF, PNG;
    }

    public static void main(String[] args) throws Exception {
        String input = "GIF";
        Type type = Type.valueOf(input);
        System.out.println(type);

    }
Sinto
  • 920
  • 5
  • 10
0

You should use equals instead. Here is a thread discussing why you should do that.

briefly

== tests for reference equality.

.equals() tests for value equality.

Community
  • 1
  • 1
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
0

override equals() method of Object class while comparing two string .

ex : str1.equals(str2);

pavan kumar
  • 108
  • 4
  • Why you want to override equals() method of Object class? equals() method is already been overriden by String class for String comparison. You just have to invoke it on a string object – Zeeshan Feb 17 '14 at 09:19
-1

Make your live easier and Parse the Enum. You won't have to write that much code and if the enum changes, you don't have to change the method. Also you can choose a default Type of your choise or do anything else if strType is not a Type in the Enum.

if (Enum.TryParse<Type>(strType, out type))
    type = Type.OTHER
anuka93
  • 74
  • 8