#Need basic help with methods.
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
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);
}
}```
Detected code, here are some useful tools:
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);
}
}
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
What do you mean by type ?
Oh yeaa
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);
}
}```
String, int, boolean, float, double, your classes, others classes, etc.
put a static keyword after public in your romanToInt
Okay sir, thank you
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
ohk understood
Now it says java: incompatible types: int cannot be converted to java.lang.String
you are not returning anything in romanToInt
In Line3 dosent string tell it that input is a String?
*String* romanToInt(...)
yes
One more error 🫠
java: cannot find symbol
symbol: variable total
location: class RomanToInteger```
:D
W
After Python, learning Java is just depressing
I see, you didn't "typed" input when I came here, so I thought: Ohh, python dev
ye lol
Since past 2 hours I was stuck on this one RomanToInteger program 
Any good source to learn Java effectively?
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
Okay tysm!