0

whats wrong?

import java.util.Scanner; public class Program { public static void main(String[] args) { Program p = new Program(); String result = p.Cap (x); System.out.println (result); } public static String Cap (String x){ Scanner scan = new Scanner (System.in); x = scan.nextLine(); return x.substring (0, 1).toUpperCase() + x.substring (1); } }

2nd May 2020, 12:26 PM
Yahel
Yahel - avatar
5 odpowiedzi
+ 2
yahel add "String x" inside the static method Cap(). You are passing "x" to Cap() in the main method but not accepting a String parameter in the actual Cap(). Hope you get it now.
2nd May 2020, 1:28 PM
Avinesh
Avinesh - avatar
0
Your method cap() is static. To call it you don't need an instance of Program. String result = cap(x); In your main you don't have a variable x. If you want to ask for user input inside cap() then remove the parameter. public static String cap(){ } in your main: String result = cap(); But you can also put the Scanner in your main: Scanner scan = new Scanner(System.in); x = scan.nextLine(); String result = cap(x);
2nd May 2020, 12:47 PM
Denise Roßberg
Denise Roßberg - avatar
0
Denise Roßberg I did that and it didn't work... so I moved the Scanner to the Cap method... maybe I did something wrong... I will check and let you know. Thanks
2nd May 2020, 12:56 PM
Yahel
Yahel - avatar
0
Denise Roßberg look, its not working: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan = new Scanner (System.in); String x = scan.nextLine(); String result = Cap (x); System.out.println (result); } public static String Cap (){ return x.substring (0, 1).toUpperCase() + x.substring (1); } }
2nd May 2020, 1:10 PM
Yahel
Yahel - avatar
0
Avinesh thanks! I knew that but sometimes you just forget :)
2nd May 2020, 1:37 PM
Yahel
Yahel - avatar