Problems while copying source code from a pdf - java

I need to copy some sections of source code from a pdf-file to a java project using eclipse neon.
Let me give you a quick example of the problem. I have code which looks like the following in the pdf:
import java.sql.Timestamp;
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusAdapter;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
public final class PrintSampleStream extends StatusAdapter {
public static void main(String[] args) throws TwitterException{
...
But in the workspace it is shown like this:
import java.sql.Timestamp; import twitter4j.FilterQuery; import twitter4j.Status; import twitter4j.StatusAdapter; import twitter4j.StatusDeletionNotice; import twitter4j.StatusListener; import twitter4j.TwitterException; import twitter4j.TwitterStream; import twitter4j.TwitterStreamFactory; public final class PrintSampleStream extends StatusAdapter { public static void main(String[] args) throws TwitterException{
How can i format the code and make it readable? Because formatting the code by hand would take too long.
Thanks for any ideas
P.S. CTRL + Shift +F doesn't help

It's a whitespace issue in the PDF or it's copy operation.
You can replace string ; (semicolon+space) by ;+ newline using the Eclispe search/replace dialog. See In Eclipse, how do I replace a character by a new line?

You say that ctrl+shift+F doesn't work for you. There is another way to format editor contents.
Go to: Window > Preferences > Java > Editor > Save actions and check the options Perform the selected actions on save, Format source code, Format all lines. Apply changes and then make small change in the editor (just to make it dirty) and save. Editor should automatically format.
Note: I am using Eclipse Luna but I believe that preference structure will be similar in Neon.
As for the reflow issues when copy pasting, you can check if this article helps you solve it.

This process isn't automated but you could use some free online code beautifiers such as
http://www.freecodeformat.com/java-format.php
http://jsbeautifier.org/
You basically have to copy-paste your unformatted code into a text-box. You can then "beautify" the code, by clicking a button. It then generates a formatted code. It isn't perfect, but it makes your code a lot more readable.
Example input:
import java.sql.Timestamp; import twitter4j.FilterQuery; import twitter4j.Status; import twitter4j.StatusAdapter; import twitter4j.StatusDeletionNotice; import twitter4j.StatusListener;public final class PrintSampleStream extends StatusAdapter { public static void main(String[] args) throws TwitterException{
Example output:
import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusAdapter;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
public final class PrintSampleStream extends StatusAdapter {
public static void main(String[] args) throws TwitterException {

Related

Difficulty Configuring JSON Authentication for Box from Java

I'm working on a simple automated java program for using the box api and am trying to use json. I've borrowed the first part of the checkstyle sample code from the Github's repo example SearchExamplesAsAppUser, figuring it should work.
When I run it, I get a this error
java.lang.NoClassDefFoundError: org/bouncycastle/operator/OperatorCreationException
The problem seems to be stemming from the statement:
api = BoxDeveloperEditionAPIConnection.getAppUserConnection(USER_ID, boxConfig, accessTokenCache);
The Jars which I am using are (aside from commons, all recommended by box):
bcpkix-jdk15on-1.52.jar
bcprov-jdk15on-1.52.jar
box-java-sdk-2.14.1.jar
jose4j-0.4.4.jar
minimal-json-0.9.1.jar
commons-codec-1.9.jar
commons-httpclient-3.1.jar
commons-logging-1.2.jar
I am using netbeans so all of the jars above are listed under the libraries to use fr compilation.
The code is as follows:
package boxapitest;
import com.box.sdk.BoxAPIConnection;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.box.sdk.BoxConfig;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import com.box.sdk.BoxItem;
import com.box.sdk.BoxMetadataFilter;
import com.box.sdk.BoxSearch;
import com.box.sdk.BoxSearchParameters;
import com.box.sdk.BoxUser;
import com.box.sdk.DateRange;
import com.box.sdk.IAccessTokenCache;
import com.box.sdk.InMemoryLRUAccessTokenCache;
import com.box.sdk.PartialCollection;
import com.box.sdk.SizeRange;
public final class BoxAPITest {
private static final String USER_ID = "***email address removed for privacy***";
private static final int MAX_DEPTH = 1;
private static final int MAX_CACHE_ENTRIES = 100;
private static BoxDeveloperEditionAPIConnection api;
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// Turn off logging to prevent polluting the output.
Logger.getLogger("com.box.sdk").setLevel(Level.SEVERE);
//It is a best practice to use an access token cache to prevent unneeded requests to Box for access tokens.
//For production applications it is recommended to use a distributed cache like Memcached or Redis, and to
//implement IAccessTokenCache to store and retrieve access tokens appropriately for your environment.
IAccessTokenCache accessTokenCache = new InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES);
Reader reader;
reader = new FileReader("\\My Path\\file.json");
BoxConfig boxConfig = BoxConfig.readFrom(reader);
api = BoxDeveloperEditionAPIConnection.getAppUserConnection(USER_ID, boxConfig, accessTokenCache);
//api = BoxAPIConnection.getAppUserConnection(USER_ID, boxConfig, accessTokenCache);
BoxUser.Info userInfo = BoxUser.getCurrentUser(api).getInfo();
System.out.format("Welcome, %s!\n\n", userInfo.getName());
}
}
Any assistance would be most appreciated.
Bentaye actually provided the answer. One of my jars was corrupt.

How do I open multiple tabs in IE instead of multiple windows using Selenium

I am using selenium Java. I need to open Multiple tabs and open different URL's in the newly opened tab. I am try to using getWindowHandles(), It is not working for Internet Explorer. Please suggest the proper solution for this.
Here is the code I have used:
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Multiple
{
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.ie.driver","C:\\Selenium\\IEDriverServer.exe");
WebDriver d = new InternetExplorerDriver();
d.get("https://www.google.co.in/");
d.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
d.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//d.switchTo().window(d.getWindowHandles().iterator().next());
ArrayList<String> tabs2 = new ArrayList<String> (d.getWindowHandles());
d.switchTo().window(tabs2.get(1));
d.get("https://www.facebook.com/login");
}
}
Using above code I'm able to open new tab but second URL is not entering.
Can any one give me the perfect solution for this?

Label not being recognized even though jxl import and API are there

I am trying to teach myself how to write to an excel file and just copy and pasted some code from a tutorial, and this code should work without errors as I have seen similar on several other tutorials. So why is Label (error is: constructor is undefined) and AddCell (error is: The method addCell(WritableCell) in the type WritableSheet is not applicable for the arguments (Label)) acting up on me?
private void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline); //error
sheet.addCell(label); //error
}
Imports:
import java.awt.Label;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import jxl.JXLException;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
In your imports you import two different Labels. One from java.awt and one from jxl.write. You get the error that the constructor is not defined so your code is most likely using the wrong Label wich does not have a constructor like that. And you also get the error that the addCell() method is not applicable for the arguments Label so again the code probably uses the wrong Label.
All of this can be easily fixed by adding the package to the Label like this:
jxl.write.Label label;
label = new jxl.write.Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
That should fix your problem.
Good luck :)

error while reading .xlsm file

I am trying to read a .xlsm file using POI.
My code is:
import java.io.*;
import java.util.List;
import jxl.*;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.read.biff.BiffException;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.*;;
public class ReadExcelSheet {
public static void main(String[] args) throws IOException {
final Workbook wb;
FileInputStream fileIn = new FileInputStream("C:\\Users\\my\\Desktop\\ExcelPORead\\Purchace.xlsm");
wb = WorkbookFactory.create(fileIn); // Error in this line
}
}
and I am getting an error at the line "wb = WorkbookFactory.create(fileIn)", it says to "configure Build Path".
I am using Eclipse and downloaded poi-ooxml-3.5-beta5.jar and add it to the Build path.
But I am not getting what I need to do to make it working.
Kindly suggest me how to remove this error or If you have any better way to read the .xlsm files in Java.
Thanks for your response.
Regards,
Raman

xuggler error - "cannot find symbol : class VideoImage" - what am i missing?

I am using xuggler to play video files from my code and the following is a snippet from the main code:
This snippet produces an error :
//The window we'll draw the video on.
private static VideoImage mScreen = null;
private static void updateJavaWindow(BufferedImage javaImage)
{
mScreen.setImage(javaImage);
}
// Opens a Swing window on screen.
private static void openJavaWindow()
{
mScreen = new VideoImage();
}
The error that i get is : cannot find symbol : class VideoImage
The header files used are :
import java.awt.image.BufferedImage;
import com.xuggle.xuggler.Global;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.IVideoResampler;
import com.xuggle.xuggler.Utils;
Am i missing in some import statement ? If not , here are the libraries i am using apart from JDK :
What is the reason i am getting that error ?
VideoImage Javadoc
You are not importing the right class.
com.xuggle.xuggler.demos.VideoImage
It seems like you are already using an IDE. It should automatically tell you what import you are missing if the correct library is in the build path.
You need to import the VideoImage class.

Categories

Resources