Calling a method works for predefined string, but not user input from scanner in (JAVA)

Question!

I'm writing a bigger program, but wrote this little one to check what I'm doing wrong. It has a method that converts roman numeral to an integer. This works perfectly fine if i call the method (called romanNumeralsValue) with the string directly in it like romanNumeralsValue(V). But I want to use user input from the terminal to a variable called romanNumeral, like romanNumeralsValue(romanNumeral). When I do this, it suddenly doesn't work. I'm sure it's something super easy I've overlooked, but I'm frustrated and just can't get ahead. Thanks!

import java.util.Scanner;
public class romannumeralsconverting {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Give a roman numeral: ");
        String romanNumeral = in.next();

        int output = romanNumeralsValue(romanNumeral);  

        System.out.print(output);
    }


    public static int romanNumeralsValue(String input)
    {
        if (input == "I") { return 1; }
        if (input == "V") { return 5; }
        if (input == "X") { return 10; }
        if (input == "L") { return 50; }
        if (input == "C") { return 100; }
        if (input == "D") { return 500; }
        if (input == "M") { return 1000; }
        else { return 0; }
    }
}


Answers

Try this:

import java.util.Scanner;

public class RomanNumerals {

    public static void main(final String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Give a roman numeral: ");
        String romanNumeral = in.next();

        int output = romanNumeralsValue(romanNumeral);

        System.out.print(output);
    }

    public static int romanNumeralsValue(final String input) {
        if(input.equals("I")) {
            return 1;
        }
        if(input.equals("V")) {
            return 5;
        }
        if(input.equals("X")) {
            return 10;
        }
        if(input.equals("L")) {
            return 50;
        }
        if(input.equals("C")) {
            return 100;
        }
        if(input.equals("D")) {
            return 500;
        }
        if(input.equals("M")) {
            return 1000;
        } else {
            return 0;
        }
    }
}


You got a nested dictionary back, so access it's values using the right keys:

d = json.loads(json_str)
lat, long = d['result']['latitude'], d['result']['longitude']
newurl = url + '?' + 'lat=' + lat + '&lon=' + lon

Please provide full output and url format for exact code.

By : Uriel Eli


If I am reading this correctly, Message would contain a List<MessageUser>.

If that's the case then this should get you close:

var List<Message> messages = 
    ( from m 
      in db.Messages 
      where 
          m.UserID == userid || m.MessageUsers.Any(mu => mu.UserID == userID) 
      select m).ToList();
By : Sam Axe


This video can help you solving your question :)
By: admin