is there a way in eclipse to have a log on a separate window?
Like System out/err but on a separate tab. It's just for debugging and then I'll remove the command.
I wouldn't want to create any other appenders in Log4j or similar.
In my case I want to keep the list of names by iterating around 100k variables.
for (VariableBean var : variables) {
if("Integer".equals(var.getType())) {
// do something
} if("String".equals(var.getType())) {
// do something
} if("Data".equals(var.getType())) {
System.....println("Data Variable: " + var);
} else {
// do something else
}
}
Thanks!
Related
I downloaded my extended listening history from Spotify and I am trying to make a program to turn the data into a list of artists without doubles I can easily make sense of. The file is rather huge because it has data on every stream I have done since 2016 (307790 lines of text in total). This is what 2 lines of the file looks like:
{"ts":"2016-10-30T18:12:51Z","username":"edgymemes69endmylifepls","platform":"Android OS 6.0.1 API 23 (HTC, 2PQ93)","ms_played":0,"conn_country":"US","ip_addr_decrypted":"68.199.250.233","user_agent_decrypted":"unknown","master_metadata_track_name":"Devil's Daughter (Holy War)","master_metadata_album_artist_name":"Ozzy Osbourne","master_metadata_album_album_name":"No Rest for the Wicked (Expanded Edition)","spotify_track_uri":"spotify:track:0pieqCWDpThDCd7gSkzx9w","episode_name":null,"episode_show_name":null,"spotify_episode_uri":null,"reason_start":"fwdbtn","reason_end":"fwdbtn","shuffle":true,"skipped":null,"offline":false,"offline_timestamp":0,"incognito_mode":false},
{"ts":"2021-03-26T18:15:15Z","username":"edgymemes69endmylifepls","platform":"Android OS 11 API 30 (samsung, SM-F700U1)","ms_played":254120,"conn_country":"US","ip_addr_decrypted":"67.82.66.3","user_agent_decrypted":"unknown","master_metadata_track_name":"Opportunist","master_metadata_album_artist_name":"Sworn In","master_metadata_album_album_name":"Start/End","spotify_track_uri":"spotify:track:3tA4jL0JFwFZRK9Q1WcfSZ","episode_name":null,"episode_show_name":null,"spotify_episode_uri":null,"reason_start":"fwdbtn","reason_end":"trackdone","shuffle":true,"skipped":null,"offline":false,"offline_timestamp":1616782259928,"incognito_mode":false},
It is formatted in the actual text file so that each stream is on its own line. NetBeans is telling me the exception is happening at line 19 and it only fails when I am looking for a substring bounded by the indexOf function. My code is below. I have no idea why this isn't working, any ideas?
import java.util.*;
public class MainClass {
public static void main(String args[]){
File dat = new File("SpotifyListeningData.txt");
List<String> list = new ArrayList<String>();
Scanner swag = null;
try {
swag = new Scanner(dat);
}
catch(Exception e) {
System.out.println("pranked");
}
while (swag.hasNextLine())
if (swag.nextLine().length() > 1)
if (list.contains(swag.nextLine().substring(swag.nextLine().indexOf("artist_name"), swag.nextLine().indexOf("master_metadata_album_album"))))
System.out.print("");
else
try {list.add(swag.nextLine().substring(swag.nextLine().indexOf("artist_name"), swag.nextLine().indexOf("master_metadata_album_album")));}
catch(Exception e) {}
System.out.println(list);
}
}
Find a JSON parser you like.
Create a class that with the fields you care about marked up to the parsers specs.
Read the file into a collection of objects. Most parsers will stream the contents so you're not string a massive string.
You can then load the data into objects and store that as you see fit. For your purposes, a TreeSet is probably what you want.
Your code will throw a lot of exceptions only because you don't use braces. Please do use braces in each blocks, whether it is if, else, loops, whatever. It's a good practice and prevent unnecessary bugs.
However, everytime scanner.nextLine() is called, it reads the next line from the file, so you need to avoid using that in this way.
The best way to deal with this is to write a class containing the fields same as the json in each line of the file. And map the json to the class and get desired field value from that.
Your way is too much risky and dependent on structure of the data, even on whitespaces. However, I fixed some lines in your code and this will work for your purpose, although I actually don't prefer operating string in this way.
while (swag.hasNextLine()) {
String swagNextLine = swag.nextLine();
if (swagNextLine.length() > 1) {
String toBeAdded = swagNextLine.substring(swagNextLine.indexOf("artist_name") + "artist_name".length() + 2
, swagNextLine.indexOf("master_metadata_album_album") - 2);
if (list.contains(toBeAdded)) {
System.out.print("Match");
} else {
try {
list.add(toBeAdded);
} catch (Exception e) {
System.out.println("Add to list failed");
}
}
System.out.println(list);
}
}
I'm trying to use Jenkins Groovy console to modify configuration of many jobs. I need to access a field containing additional properties passed to maven by this plugin:
https://wiki.jenkins.io/display/JENKINS/Release+Plugin
So i figured out how to reach plugin classes:
for(item in Hudson.instance.items) {
if (item instanceof hudson.maven.MavenModuleSet)
{
println("\njob $item.name ");
rw = item.getBuildWrappers().get(hudson.plugins.release.ReleaseWrapper.class);
if (rw == null)
{
println("release build not configured");
}
else
{
println("\nrelease build configured");
println(rw.getParameterDefinitions());
println("\n");
println(rw.getPreBuildSteps());
println("\n");
for(step in rw.getPreBuildSteps()){
println("\nPROPERTIES: " + step.getProperties())
for(property in step.getProperties()){
println("\nPROP: " + property)
}
}
}
}
}
PROPERTIES: [settings:jenkins.mvn.DefaultSettingsProvider#307e9334,
class:class hudson.tasks.Maven, maven:null,
globalSettings:jenkins.mvn.DefaultGlobalSettingsProvider#a1e5a0,
usePrivateRepository:false,
descriptor:hudson.tasks.Maven$DescriptorImpl#5481ba47,
injectBuildVariables:true, targets:release:prepare,
requiredMonitorService:NONE]
Unfortunately properties field is a list of strings describing groovy object fields instead of data I'm looking for. It should be a simple String containing additional properties for maven.
https://javadoc.jenkins.io/hudson/tasks/Maven.html#properties
It looks like Groovy has overwritten default method and field with its magic. Is there a way to reach properties anyway?
I found it myself.
step.properties
reaches to Groovy properties.
step.#properties
reaches to the original class method.
I currently have an issue in my script where I use a for loop to iterate through an array of elements and check for their existence in the GUI. My issue is the for loop always skips the first entry of the array.
My current script is as follows:
public class GUIFunctionality {
static Properties config = Data.getProperties("config");
static int Pass = 0;
static Screen s = new Screen();
#Test(priority = 0)
public static void loginGUI() {
WebDriver driver = AutomationWebDriver.getWebDriver("firefox", config.getProperty("url"));
// Test all GUI elements on login screen.
String[] login_elements = { "loginbutton.png", "logintitle.png", "smalllogo.png", "remembermechecked.png",
"signupbutton.png", "signuptitle.png", "changelanguage.png" };
ArrayList<String> passed = new ArrayList<String>();
ArrayList<String> failed = new ArrayList<String>();
for (String base : login_elements) {
String file = String.format("imagerepo/config/%s", base);
if (s.exists(file) != null) {
System.out.println(file + " has been successfully found.");
passed.add(file);
Pass++;
} else {
System.out.println(file + " has not been found.");
failed.add(file);
}
}
This script completely ignores "loginbutton.png", almost as though it never existed in the script at all. I'm really stumped as to why. Here is the console output:
imagerepo/config/logintitle.png has been successfully found.
imagerepo/config/smalllogo.png has been successfully found.
imagerepo/config/remembermechecked.png has been successfully found.
imagerepo/config/signupbutton.png has been successfully found.
imagerepo/config/signuptitle.png has been successfully found.
imagerepo/config/changelanguage.png has been successfully found.
Found elements: [imagerepo/config/logintitle.png, imagerepo/config/smalllogo.png, imagerepo/config/remembermechecked.png, imagerepo/config/signupbutton.png, imagerepo/config/signuptitle.png, imagerepo/config/changelanguage.png]
Missing elements: []
I'm wondering what I need to alter so the first entry of the String[] login_elements is included in the for loop. What's also interesting is that adding one more entry to the String[] login_elements will completely fix it.
Making this minor change: (nobutton.png is an image that exists within the repository, but not on the page under test)
String[] login_elements = { "nobutton.png", "loginbutton.png", "logintitle.png", "smalllogo.png",
"remembermechecked.png", "signupbutton.png", "signuptitle.png", "changelanguage.png" };
This one change will now print this to the console:
imagerepo/config/nobutton.png has not been found.
imagerepo/config/loginbutton.png has been successfully found.
imagerepo/config/logintitle.png has been successfully found.
imagerepo/config/smalllogo.png has been successfully found.
imagerepo/config/remembermechecked.png has been successfully found.
imagerepo/config/signupbutton.png has been successfully found.
imagerepo/config/signuptitle.png has been successfully found.
imagerepo/config/changelanguage.png has been successfully found.
Found elements: [imagerepo/config/loginbutton.png, imagerepo/config/logintitle.png, imagerepo/config/smalllogo.png, imagerepo/config/remembermechecked.png, imagerepo/config/signupbutton.png, imagerepo/config/signuptitle.png, imagerepo/config/changelanguage.png]
Missing elements: [imagerepo/config/nobutton.png]
This console output includes every entry within that array. Deleting "nobutton.png", from the array will bring us back to our original issue.
So what the heck is going on? The only thing I can possibly think of is a minimum number of strings in an array to include the first entry, but that just seems downright silly.
Edit: s.exists(String) is an instance of the Sikuli screen using the exists function to check for the existance of elements on the page. I really do not think this has anything to do with the error. I also could be completely wrong about this. I've learned most of the Sikuli library through trial-and-error (time-crunch around release dates is a horrible thing), so my ignorance on "why" is pretty high, which is why I'm here.
Edit: Remember, adding one more entry to the array completely fixes the problem.
Edit: Added the instance of s. The line WebDriver driver = AutomationWebDriver.getWebDriver("firefox", config.getProperty("url")); is an instance of a Selenium WebDriver I use to start the instance of WebDriver which I have to use alongside Sikuli because our web application is fubar (6 years of legacy code).
Another Edit: Source code for Region.exists() method and documentation.
Source Code
Documentation
This question has been answered. #Berger and #Andy Thomas have also provided some insight into what happens with the loop:
I think I have found the source code. exists uses a while loop based on a timeout value, among other things, so a subsequent call with the same parameter, could well return another result, see : https://github.com/RaiMan/SikuliX-2014/blob/master/API/src/main/java/org/sikuli/script/Region.java - #Berger
I see from another Sikuli source file that the default autoWaitTimeout is 63 seconds, making the race condition easy to observe. Two important lessons from this question are: 1) A default case is frequently useful, especially if it's not expected to occur -- and 2) If you want a single return value, make a single call. - #Andy Thomas
You don't have a default case. You're using an if-elseif rather than an if-else.
for (String base : login_elements) {
String file = String.format("imagerepo/config/%s", base);
if (s.exists(file) != null) {
...
} else if (s.exists(file) == null) {
...
}
}
Your second condition includes a second call to s.exists(file). If neither branch is entered, the value returned must be changing between calls.
You could handle this by adding a default case. An easy way would be to eliminate the second condition.
for (String base : login_elements) {
String file = String.format("imagerepo/config/%s", base);
if (s.exists(file) != null) {
...
} else {
...
}
}
A debugger can help you find problems like this. If you set a breakpoint on the first condition, you'd see that the first file is being considered by the loop.
I believe the Java code should be:
String[] login_elements = {
"loginbutton.png",
"logintitle.png",
"smalllogo.png",
"remembermechecked.png",
"signupbutton.png",
"signuptitle.png",
"changelanguage.png"
};
ArrayList<String> passed = new ArrayList<String>();
ArrayList<String> failed = new ArrayList<String>();
for (String base : login_elements) {
String file = String.format("imagerepo/config/%s", base);
File s = new File(file);
if (s.exists()) {
System.out.println(file + " has been successfully found.");
passed.add(file);
}
else {
System.out.println(file + " has not been found.");
failed.add(file);
}
}
System.out.println("Found elements: " + passed);
System.out.println("Missing elements: " + failed);
This is because you are not facing all possibilities:
if (s.exists(file) != null) {
System.out.println(file + " has been successfully found.");
passed.add(file);
} else {
System.out.println(file + " has not been found.");
failed.add(file);
}
Will throw an error with your same code...
So i'm trying to make a text game in java for a project and i have a problem in the main loop.I have the available commands in a hashmap named commands in the class CommandWords and i want to check if the user input exists in the hashmap and if it does to execute the associated object.But i can't exactly find a way.Here is my code.I understand it's probably an if but i don't know how to check.
public void play()
{
System.out.println("Welcome to the world of JZork " +player.name);
printWelcome();
boolean finished = false;
while (! finished) {
Command command = parser.getCommand();
if(command == null) {
System.out.println("I don't understand...");
}
}
}
It is possible to do it the way you said, but you would need to learn to use Java reflection. This isn't THAT hard, but you aren't going to like it.
if (command.equals("quit")) {
quit();
}
else if (command.equals("whatever")) {
whatever();
}
This isn't that elegant, but it's easy to implement.
i am working on a large application which has differnt conditions(ifs) and different methods associated with it.Please suggest a way to optimize below mentioned code(reduce as much of nested ifs as possible).I would like the code to able incorporate any other specific condition with ease.(I use property files to fetch conditions)
.
public getParameter(String parameter)
{
if(parameter=specific condition1
||parameter=specific condition2)
{
do this
}
if(parameter=specific condition3)
{
do something else
}
if(parameter=specific condition4)
{
do something else
}
if(parameter=general condition)
{
do something else
}
else {
do something else
}
Say you have a property file with
do1=val1,val2,val3
do2=val5,val6,val7
(it seems you have a fixed set of actions)
You may load it with
HashMap<String, HashSet<String>> rules = new HashMap<String, HashSet<String>>();
for(String key : properties.stringPropertyNames()) {
HashSet<String> set = new HashSet<String>();
set.addAll(Arrays.asList(properties.getProperty(key).split(",")));
rules.put(key, set);
}
Now you have a map linking action names ("do1", etc.) to sets of possible values ("val1", etc.).
You may execute the rules with
if (rules.get("do1").contains(parameter)) do1();
if (rules.get("do2").contains(parameter)) do2();
(I let you add the necessary checks to avoid null pointer exceptions for example)
you could use a switch case.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html