0

first steps

hello i write my first java program after i finished the basic tutorial.. now i getting a few errors and i cannnot find it... has anyone an idea? public class test{ public static void main(String[] args){ import java.util.Scanner; class firsttry{ Scanner vareingabe = new.Scanner(System.in); System.out.println(vareingabe.next()); if(vareingabe==pass){ System.out.println("nice give me a number:"); Scanner nummer = newScanner(System.in); System.out.println(nummer.next()); res = nummer * nummer; System.out.println(nummer); }else{ Sytem.out.println("sorry"); } } } }

22nd Nov 2016, 10:40 PM
Dominik Pollhammer
2 Respuestas
+ 2
classes must be imported in the first lines import java.util.Scanner; public class Test{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); System.out.print("type something: "); String input = sc.nextLine(); if(input.equals("password")){ System.out.println("Correct"); }else { System.out.println("Sorry"); } }
22nd Nov 2016, 10:54 PM
( ͡° ͜ʖ ͡°)
- 1
try somthing like: import java.util.Scanner; public class Test{ public static void(String[] args){ Scanner vareingabe = new Scanner(System.in); // read text/string String var = vareingabe.next(); // pass has to be a variable. if you want to check whether var is equal to // "pass" you have to write if(var == "pass") if( var == pass ){ System.out.println("Your text"); // print "Your text" int nummer = vareingabe.nextInt(); // read number System.out.println(nummer); // print number int res = nummer * nummer; System.out.println(res); // print square of number from input }else{ System.out.println("Sorry"); } } } Your mistakes have been in a few lines. First there is no dot between new and the class you want to instantiate. Next mistake is that a Scanner is an object which reads in values. So your first comparison cannot work because you try to compare an object to something unidentified. This brings us to your next mistake. You forgot to declare from which data type pass is. I think you wanted to compare it to the string "pass". In this case you have to write if( variable == "pass"). You repeated the 2 mistakes above on reading in nummer and assign res. At the beginning you defined a class in an class. That is no possible. You can create two classes in the same file. In this case you have a single public class and all others are classes. Here is an example: public class Test{ public static void main(String[] args){ // do something } } class Test2{ //constructor, attributes or methodes of class Test2 } There are many mistakes but you will get it. Keep practicing and good luck.
22nd Nov 2016, 11:07 PM
Andreas K
Andreas K - avatar