Jama Matrix printwriter error - java

I am using JAMA matrix in my project. I need to write down a Jama matrix in text file. For that I write down this code.
package Xdata;
import Jama.Matrix;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class File_r {
public static void main(String args[]) {
Matrix A = new Matrix(10, 10);
try {
PrintWriter write1 = new PrintWriter(new File("/home/robotics//IdeaProjects/Data_arrange/src/Xdata/mu_X.txt"));
A.print(PrintWriter write1,9,6);// error in this line
}
catch(FileNotFoundException ex) {
System.out.println(ex);
}
}
}
But it throws errors:
/home/robotics/IdeaProjects/Data_arrange/src/Xdata/File_r.java
Error:(13, 32) java: ')' expected
Error:(13, 33) java: not a statement
Error:(13, 39) java: ';' expected
I wtite down this code in intellj idea. Can any one tell me why I get this error?

I did check the Jama api for Matrix.java. it looks you are trying to use the print method with three parameter in the below snippet . please re-write it correctly.
fix it as below
A.print(write1,9,6);// error in this line

I solved this problem. I think it is helpful for those who are new to Jama Matrix and face a problem like that. Here is my Solution:
package Xdata;
import Jama.Matrix;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class File_r {
public static void main(String args[]) {
Matrix A = new Matrix(10, 10);
PrintWriter writer=null;
try {
writer = new PrintWriter("/home/robotics//IdeaProjects/Data_arrange/src/Xdata/mu_X.txt");// So basically I change this line
A.print(writer,2,2);
writer.close();// Add this line
}
catch(FileNotFoundException ex) {
System.out.println(ex);
}
}
}
This solve my problem. As there is very less documentation of JAMA Matrix I think this is really help ful for reader.

Related

Issue with the Scanner input

I am writing a code to get data from a radiometer sensor connected to one of the "COMs" of the computer, to get the measurement i have to communicate with that sensor throw "COM7" and write the command "gi" to get a value like ""9.919e-08" for example from the command user interface.
Now i have a problem with the code and it is giving me that error "No line found"
and here is the code
package reading_data;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import com.fazecast.jSerialComm.SerialPort;
public class main {
public static void main(String[] args) throws IOException, InterruptedException {
SerialPort sp= SerialPort.getCommPort("COM7");
sp.setComPortParameters(115200, 8, 1, 0);
sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
sp.openPort();
if(sp.isOpen()) {
System.out.println("Port is open");
PrintWriter output=new PrintWriter(sp.getOutputStream());
Scanner data=new Scanner(sp.getInputStream());
output.println("gi");
String ssss=data.nextLine();
System.out.println("--++++---->"+ssss);
}else {
System.out.println("Port is not open");
}
sp.closePort();
}
}
and here is the error i get
Port is open
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at reading_data.main.main(main.java:26)
May you please tell me where is my mistake?
thanks in advance
Try like this:
output.flush();
if(data.hasNextLine()) {
String ssss=data.nextLine();
}
If no next line, then don't call the nextLine.
Edit.: flush needs to be done.

Decoding .webm format doesn't return any BufferedImages

My code here is compiling correctly, but I am running into the problem that my ArrayList of BufferedImages is always empty. Honestly I don't have any knowledge regarding ImageIO or the likes!
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import net.sf.javavp8decoder.imageio.WebPImageReader;
import net.sf.javavp8decoder.imageio.WebPImageReaderSpi;
class MyProj{
public static void main(String[] args) throws IOException{
System.out.println("Main");
ArrayList<BufferedImage> collectedImg=getFrames();
}
static ArrayList<BufferedImage> getFrames() throws IIOException{
File MyWebM= new File("/users/case3/mcclusm4/workspace/LineTech/src/goal.webm");
ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
try{
ImageReader ir = new WebPImageReader(new WebPImageReaderSpi());
ir.setInput(ImageIO.createImageInputStream(MyWebM));
for(int i = 0; i < ir.getNumImages(true); i++)
frames.add(ir.read(i));
}catch(IOException e){}
return frames;
}
}
First of all dont catch Exception and do nothing:
catch (Exception e) {}
Now when an exception is catched it silently fails without any information.
Change catch to print stacktrace: e.printStackTrace() and post it.
Disclaimer: I have never tested the code in question myself. But...
From looking at the source code of net.sf.javavp8decoder.imageio.WebPImageReader it cannot decode WebM files. It only supports single frame WebP files.
If you stop swallowing the exception and ignoring it, as suggested by #robocoder, you should get an IIOException with the message "Bad WEBP signature!".

Using a delimiter to extract a hyperlink - Java

I'm a novice at java and I'm working on a project that scans the source code of a website, and extracts all the hyperlinks contained in it.
So far I have my project working so that it scans every 'word' of the source code using a Scanner (in.next())
However Ive been told to use delimiters to extract the hyperlinks from this, but I can barely find any information out there to help me use them!
Someone couldnt help explain to me delimiters and how I could use them in this project? It would be really appreciated.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.ArrayList;
public class HyperlinkMain {
public static void main(String[] args) {
try {
Scanner in = new Scanner (System.in);
String URL = in.next();
URL website = new URL(URL);
Scanner inWebsite = new Scanner (website.openStream());
String inputLine;
while ((inWebsite.hasNext())) {
// Process each 'word'.
System.out.println(inWebsite.next());
}
in.close();
} catch (MalformedURLException me) {
System.out.println(me);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
You could use Regular expression on strings. Below is an existing Stack Overflow on this topic.
How to use regular expressions to parse HTML in Java?

Please help me with JUnit test cases for the code below

I want to know the JUnit test cases for the following program.please help. I have not included the main method here. Want to know the JUnit test cases for the url() method in the code. This code is to read HTML from a website and save it in a file in local machine
package Java3;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Urltohtml
{
private String str;
public void url() throws IOException
{
try
{
FileOutputStream f=new FileOutputStream("D:/File1.txt");
PrintStream p=new PrintStream(f);
URL u=new URL("http://www.google.com");
BufferedReader br=new BufferedReader(new InputStreamReader(u.openStream()));
//str=br.readLine();
while((str=br.readLine())!=null)
{
System.out.println(str+"\n");
p.println(str);
}
}
catch (MalformedURLException ex)
{
Logger.getLogger(Urltohtml.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I would rename that class to UrlToHtml and write a single JUnit test class UrlToHtmlTest.
Part of the reason why you're having problems testing this is that the class is poorly designed and implemented:
You should pass in the URL you want to scrape, not hard code it.
You should return the content as a String or List, not print it to a file.
You might want to throw that exception rather than catch it. Your logging isn't exactly "handling" the exceptional situation. Let it bubble out and have clients log if they wish.
You don't need that private data member; return the contents. That lets you make this method static.
Good names matter. I don't like what you have for the class or the method.
Why are you writing this when you could use a library to do it?
Here's what the test class might look like:
public class UrlToHtmlTest {
#Test
public void testUrlToHtml() {
try {
String testUrl = "http://www.google.com" ;
String expected = "";
String actual = UrlToHtml.url(testUrl);
Assert.assertEquals(expected, actual);
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}

java.lang.NullPointerException when using Java with Ruby

I am trying to call a ruby function in java. but I got a NullPointerException when I run the program.
Here is my java code
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStream;
public class MyProgram
{
public static void main(String[] args) throws IOException, NoSuchMethodException
{
try
{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine rbEngine = mgr.getEngineByExtension("rb");
InputStream is = ClassLoader.getSystemResourceAsStream("src/myruby.rb");
Reader reader = new InputStreamReader(is);
rbEngine.eval(reader);
Invocable invocableEngine = (Invocable)rbEngine;
if (invocableEngine != null)
{
int set = (Integer) invocableEngine.invokeFunction("myfunc",6,6);
}
}
catch (ScriptException e)
{
System.out.println("\nScriptException = "+e);
}
}
}
And the myruby.rb file contains
def myfunc(a,b)
f=a+b
return f
end
The Error I am getting is,
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at MyProgram.main(MyProgram.java:22)
Please help me to find the problem.
Thanks in Advance.
InputStream is = ClassLoader.getSystemResourceAsStream("src/myruby.rb");
Here, is is null.
Try an absolute path to open your file.
If your file is found, then there is a problem with the ClassLoader.getSystemResourceAsStream.
As LaGrandMere said in his answer is is null here.
It is null because ClassLoader.getSystemResourceAsStream is not able to find the resource specified.
ClassLoader looks for the resource in the classpath specified.
To get this resource available, add myruby.rb in your class path.
Hope this helps !!

Categories