0

In android studio, when I run a simple code to get an integer number, it gives me an error regarding the Scanner class.

 import java.util.Scanner;
 public class MyClass {
 public static void main(String[] args){
 Scanner yadigar = new Scanner(System.in);
 int a = yadigar.nextInt();
 System.out.println("a");
 } 
}

I just expected it to print a number but unlike eclipse and other IDEs it gave an error. I actually tried to modify gradle, but it did not work(maybe I did something wrong, I am not sure honestly). Execution failed for task ':yad:MyClass.main()'.

Process 'command 'C:/Program Files/Android/Android Studio/jre/bin/java.exe'' finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
  • It's unclear what you're really trying to do here. Do you want to write command-line Java applications? (if so, why are you using Android Studio?). Or do you actually want to write Android apps? Because what you have written so far is not an Android app. – Michael Mar 25 '22 at 21:40
  • Actually, I have a background on java. It has just been one week that I have started android development. That is why I downloaded android studio. While just passing through basic concepts, it just caught my attention. Usually, I work on eclipse and I have not encountered such situation before. – Yadigar Yusifov Mar 25 '22 at 21:48
  • That's how you write a console Java app. Android is a whole different beast. You don't take input from a console, so you'd never use Scanner (in fact the class doesn't even exist). The language is the same, but the standard library has some changes, and a whole ton of additions for Android specific things. – Gabe Sechan Mar 26 '22 at 02:33
  • Oh okay. I got it now, thanks. – Yadigar Yusifov Mar 26 '22 at 10:05

1 Answers1

0

In Android studio you can create android apps . and if you want to run a standalone projects you can use this hacky way . The main function is not registered the default program to run in Android Studio.

I would suggest you to use Intellij idea community edition which has all the features you would have in android studio and can run standalone java projects with ease

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
  • so, sir, it is not possible to insert an element through Scanner class as in eclipse and other IDE s, right? – Yadigar Yusifov Mar 25 '22 at 21:55
  • In android, we do not use the Scanner class to take input. Think of it as the IDE (Android Studio) made in such a way to cater to android apps only .. SO there is no configuration to create a standalone java classes ( classes where you have the public static void main ) with a main method. If you have to take inputs and operate ,, Look into edittext, take input from user , button on click listener etc – Narendra_Nath Mar 25 '22 at 21:57
  • Clear now. Thank you so much for taking your time. – Yadigar Yusifov Mar 26 '22 at 10:04
  • Please accept the answer if it helped you..Welcome to stackoverflow – Narendra_Nath Mar 26 '22 at 16:12