I have a very bad experienced about the scanner because I am using GUI and JOptionPane. So I am not be able to do the program's interns of scanner because. I am new to it so Please help me, "cannot find the scanner". This is my code so far .
import java.io.*;
import java.util.*;
class MultiplicationTables{
public static void main(String args[]){
int n, c;
System.out.println("Enter an integer to print its multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}
If your Java is not version 1.5 or above, Scanner class is not provided.
go to command promt
type "java -version"
check your version.If you have outdated just update. Problem shoud be fixed..
and make sure your IDE or the JDK actually use it.
Code that you have here provided is correct so you can use it after you fix your problem.
First import is redundant.
import java.io.*; // you can remove it
Scanner is a class in java.util package.
Check weather you have properly set your jdk path in your ide, or in your system.
Depends on system that you use:
Windows: Advanced System Settings->Environment Variables->Path check weather there is your path to jdk.
UNIX in console print 'echo $PATCH' and check that if you have there jdk path properly added.
And then you can check your version of java independent to system in console writing
java --version
Related
this is my first time posting here and would like how to solve this error message. It appears only sometimes and only lets me build on a program called Main.java. I'm a begginer programmer so please bear with me, this is the code im trying to run:
import java.util.Scanner;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
Scanner priceScanner = new Scanner(System.in);
System.out.print("Price: ");
int price = priceScanner.nextInt();
Scanner interestScanner = new Scanner(System.in);
System.out.print("Interest rate: ");
double interest = interestScanner.nextDouble();
Scanner numberOfPaymentsScanner = new Scanner(System.in);
System.out.print("Number of payments: ");
int numberOfPayments = numberOfPaymentsScanner.nextInt();
Double monthlyInterest = interest / 1200;
Double result = ((double)price * ((interest * Math.pow((1 +
interest), (double)numberOfPayments))/((Math.pow((1 + interest),
(double)numberOfPayments)) - 1)));
NumberFormat currency = NumberFormat.getCurrencyInstance();
String mortgage = currency.format(result);
System.out.println("Your mortgage is: " + mortgage);
}
}
I haven't seen any comprehensible ways to solve this problem online, and the only thing i think could solve it is to reinstall java in another drive and change the classpath.
Thanks for your attention.
I solved it - my mistake. While executing the program using the terminal I was typing java Main.java, whereas the correct execution method was to type java Main.
With Single-file source-code programs which is a new way of executing 1 File Java programs is only available since Java 11. You can run the command:
java (Java File Name without .java extension)
java Main.java
Though please take into consideration that this way of executing only works if your Java project is only 1 Java File.
FYI:
This single-file source code will be executed fully in memory and you can only import code that came with the JDK you are working.
Finally if you want your code to run as fast as possible compile with javac before executing you program.
javac Main.java
java Main
Just be careful that there is no Main.class already in the folder, this may cause a confusion to the compiler.
Step 1:
javac + Filename.java
Step 2:
java + Filename // execute without add .java
I have created the following Java class and saved it as Test.java, then compiled into Test.class on the command line using javac Test.java:
public class Test {
public Test() {
}
public double power(double number) {
System.out.println("calculating...");
return number * number;
}
}
Furthermore, I have created the following R script and saved it as test.R:
library("rJava")
.jinit(classpath = getwd())
test <- .jnew("Test")
.jcall(test, "D", "power", 3)
When I execute it, for example using R CMD BATCH test.R on the command line, I get the following output, which is what I want:
calculating...
[1] 9
However, when I wrap this script in a Markdown document and compile it using knitr, I lose the message that is printed about the calculation taking place. For example, I save the following script in test.Rmd and compile it using RStudio:
```{r echo=TRUE, warning=TRUE, results='show', message=TRUE}
library("rJava")
.jinit(classpath = getwd())
test <- .jnew("Test")
.jcall(test, "D", "power", 3)
```
This only returns the following output, without the message:
## [1] 9
I read somewhere that the reason is that System.out.println in Java writes to stdout, and whether this is shown in the R console or not depends on the interpreter. For example, the output is shown on Unix systems but not on Windows or in knitr.
My questions:
Is the above interpretation correct?
How can I reliably capture or display the output of System.out.println in R, irrespective of operating system or interpreter?
If that's not possible, what is a better way of designing status messages about the current calculations and progress in Java, such that R can display these messages?
Thanks!
I'll take a stab at answering my own question... Looks like the RJava folks actually offer a built-in solution (thanks Simon Urbanek if you read this). On the side of the Java code, there is the LGPL-licensed JRI.jar, which is delivered with rJava (look at the jri sub-directory in the rJava package directory in your local R library path) and which can be copied/extracted into the Java library path. It's only 82kb, so fairly light-weight.
JRI offers a replacement of the default print stream in Java. Essentially, you redirect the system output into an RConsoleOutputStream provided by JRI. The code in my question above can be modified as follows to print to the R console instead of stdout.
import java.io.PrintStream;
import org.rosuda.JRI.RConsoleOutputStream;
import org.rosuda.JRI.Rengine;
public class Test {
public Test() {
Rengine r = new Rengine();
RConsoleOutputStream rs = new RConsoleOutputStream(r, 0);
System.setOut(new PrintStream(rs));
}
public double power(double number) {
System.out.println("calculating...");
return number * number;
}
}
I just started learning JAVA and Sublime Text 3 was proposed to me as a great compiler for JAVA code. I downloaded it, started programming and set my build system as JavaC. I wanted to create a quick program adding two numbers given by the user and displaying the result but nothing comes up in the "build" section. Do you have any idea to make that work ?
Here is the code I wanna try:
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int a,b;
Scanner input=new Scanner(System.in);
System.out.println("Enter a number:");
a=input.nextInt();
System.out.println("Enter a number:");
b=input.nextInt();
System.out.println("sum=" + (a+b));
}
}
For future references on how this windows command line or command prompt in windows works.
First check if your computer have the JRE (required to run a Java program) and JDK ( required compile and run Java programs) already install to verify this input the text "java -version" and javac -version into the Command Line. If the Javac is already intalls, however, doesn't show in the command line then you need to follow this sites on how to set up the path depending on your OS.
This has been asked countless times but I haven't gotten a solution for it. My code:
import java.util.Scanner;
import java.io.*;
public class Reverse
{
public static void main(String[] args)
{
File myFile = new File(args[0]);
try
{
Scanner input = new Scanner(myFile);
String message = "";
while(input.hasNext())
{
message = input.nextLine() + message;
}
System.out.println(message);
input.close();
}
catch(FileNotFoundException e)
{
System.exit(1);
}
}
}
Details:
Being coded through Eclipse on Mac
Ran on command line using javac Reverse.java then java Reverse.java (edit: java Reverse works)
- This has something to do with classpaths, but I have no clue what I'm supposed to do
I haven't done anything with regards to classpaths so any help is appreciated.
edit: My question now is, how does java -classpath . Reverse work? I don't really understand the -classpath tag and the '.' tag.
As mentioned by Pradeep's comment, you need to run java Reverse instead of java Reverse.java. Otherwise it will look for a class called Reverse.java, which does not exist.
Your class is called Reverse.
In response to the editted question, java -classpath . basically tells java to use the current working directory . as part of the classpath. The classpath is the path where java looks for classes to load and run.
I keep getting this message in BlueJ/Java.
http://cache.gyazo.com/19c325e77bbc120892d1035dcfda5377.png
I know there are several other questions like this already on StackOverflow, but none of the answers were specific enough for me, a Java noob. For example, one of them said to add something on the javac command line," and I have no idea what is that. So use that information to know how specific you must be with me. Sorry. Thanks!
open up command prompt in the directory with your source files. then type javac -Xlint:unchecked *.java
Please refer to the BlueJ FAQ which has the exact answer.
Edit:
Sorry for the runaround. This is what the FAQ says for Windows. Go to your Bluej installation directory and open lib\bluej.defs file. Then go to the section that says bluej.windows.vm.args and add the value the other user said.
So you have:
bluej.windows.vm.args=-Xlint:unchecked
I would listen to paulsm4's advice and start learning from command-line if you really want to understand java. This is the best I can do.
#viggom555:
Here is a complete command line example:
1) Create the file "ATest.java" (EXAMPLE: notepad ATest.java):
import java.util.*;
public class ATest {
public static void main (String[] args) {
ArrayList<String> test = new ArrayList<String>();
System.out.println ("My array has " + test.size() + " items");
test.add ("abc");
System.out.println ("My array has " + test.size() + " items");
}
}
2) Compile (EXAMPLE: "javac -Xlint:unchecked ATest.java"; you don't really need the "XLint" in this example; I'm just showing you where it would go if you wanted):
C:\temp>javac -Xlint:unchecked ATest.java
3) Run the test program:
C:\temp>java ATest
My array has 0 items
My array has 1 items
I hope that helps .. PSM