#Need basic help with methods.

1 messages · Page 1 of 1 (latest)

vague wave
#

I am new to java, cant understand why is it showing java: <identifier> expected and not working.

iron summitBOT
#

<@&987246399047479336> please have a look, thanks.

iron summitBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

vague wave
#
import java.util.Scanner;
public class RomanToInteger {
    public String romanToInt(input) {


        int total = 0;

        for (int i = 0; i < input.length(); i++) {
            char number = input.charAt(i);
            switch (number) {
                case 'I' -> total += 1;
                case 'V' -> total += 5;
                case 'X' -> total += 10;
                case 'L' -> total += 50;
                case 'C' -> total += 100;
                case 'D' -> total += 500;
                case 'M' -> total += 1000;
                default -> System.out.printf("%s is not a valid roman number, Ignored it  \n", number);
            }

        }
        System.out.println("The total is " + total);
    }

    public static void main (String[] args) {
        System.out.print("Enter a roman number: ");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        romanToInteger(input);
    }
}```
iron summitBOT
# vague wave ```java import java.util.Scanner; public class RomanToInteger { public Strin...

Detected code, here are some useful tools:

Formatted code
import java.util.Scanner;

public class RomanToInteger {
  public String romanToInt(input) {
    int total = 0;
    for (int i = 0; i < input.length(); i++) {
      char number = input.charAt(i);
      switch (number) {
        case 'I' -> total += 1;
        case 'V' -> total += 5;
        case 'X' -> total += 10;
        case 'L' -> total += 50;
        case 'C' -> total += 100;
        case 'D' -> total += 500;
        case 'M' -> total += 1000;
        default  -> System.out.printf("%s is not a valid roman number, Ignored it  \n", number);
      }
    }
    System.out.println("The total is " + total);
  }
  public static void main(String[] args) {
    System.out.print("Enter a roman number: ");
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine();
    romanToInteger(input);
  }
}
broken mirage
#

your method is romanToInt
you are calling it as romanToInteger

#

ah, also, input does not have a type in your method declaration

change it to String input in your method declaration

vague wave
#

Error:

java: non-static method romanToInt(java.lang.String) cannot be referenced from a static context```

```java
import java.util.Scanner;
public class RomanToInteger {
    public String romanToInt(String input) {


        int total = 0;

        for (int i = 0; i < input.length(); i++) {
            char number = input.charAt(i);
            switch (number) {
                case 'I' -> total += 1;
                case 'V' -> total += 5;
                case 'X' -> total += 10;
                case 'L' -> total += 50;
                case 'C' -> total += 100;
                case 'D' -> total += 500;
                case 'M' -> total += 1000;
                default -> System.out.printf("%s is not a valid roman number, Ignored it  \n", number);
            }

        }
        System.out.println("The total is " + total);
    }

    public static void main (String[] args) {
        System.out.print("Enter a roman number: ");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        romanToInt(input);
    }
}```
broken mirage
broken mirage
vague wave
#

Okay sir, thank you

broken mirage
#

basically:

static is a form of context

non-static means: attached to objects (variables which creates your classes with new)
static means: you don't need to create a object for using this function, instead, this method is attached to the class signature

vague wave
#

ohk understood

#

Now it says java: incompatible types: int cannot be converted to java.lang.String

broken mirage
#

you are not returning anything in romanToInt

vague wave
broken mirage
#

hmm

#

yeah, total is an int, while you are saying the function returns String

vague wave
#

In Line3 dosent string tell it that input is a String?

broken mirage
#

*String* romanToInt(...)

vague wave
#

ohk that one

#

So I should just change it to int keyword right?

broken mirage
#

yes

vague wave
#

One more error 🫠

java: cannot find symbol
 symbol:   variable total
 location: class RomanToInteger```
broken mirage
#

int total = romanToInt(input);

#

in main method

vague wave
#

Okay yea works now finally

#

Thank you so much sir

broken mirage
#

:D

still zenith
#

W

vague wave
#

After Python, learning Java is just depressing

broken mirage
vague wave
#

ye lol

#

Since past 2 hours I was stuck on this one RomanToInteger program peepo_sad_2

#

Any good source to learn Java effectively?

iron summitBOT
#

MOOC is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.

It consists of two parts, one at beginner, and another at intermediate level. The end of the course is marked by creating your own Asteroids game clone!

Even though the instructions show how to configure and use NetBeans for the course, you can use IntelliJ. To use IntelliJ, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.

Visit MOOC here: https://java-programming.mooc.fi/
(the course is available in both English and Finnish)

About the course - Java Programming

vague wave
#

Okay tysm!