|
Thank you. How would I go about doing that? I am fairly new to android studio and this is all new to me.
|
|
|
|
|
|
|
Hi Guys,
This is my first post in this community. And I hope I am not screwing up.
Anyways I built a package to process Json data as a string into objects as nodes stored in nodes in a tree. It is far from complete, but the parsing and node tree building methods are set and the manager class is commenting.
This is also important to me as I am building a portfolio, and I i would be grateful if someone can take another look to see if anything can be done more efficiently and possible let me know where i went wrong with commenting and examples of how you would do it.
Anyways the main class link in github is bellow:
JVJsonManager/Json.java at master · Clowcadia/JVJsonManager · GitHub[^]
|
|
|
|
|
Sorry, this forum is for technical questions. Also, most people here have day jobs so their time is limited. But Java already has built in support for JSON: java json - Google Search[^].
|
|
|
|
|
Thank you for response, I am aware of the existing Json libraries out there, the reason i made my own is to populate my portfolio with something as i think a starter portfolio will tend to end up rewriting the wheel . (I am sure this is the case most of the time anyways, as most application for most functions are already existent). As for this is not the correct community for this, does anyone have suggestions where I could get some advice?
|
|
|
|
|
So i am working on a hacker rank question and able to make it work but in certain cases it takes way too much time
<pre>static long repeatedString1(String u, long n) {
String[] a = u.split("");
int s = a.length;
int t = (int) n;
int r = (t / s) + 1;
String d = new String(new char[t]).replace("\0", u);
String[] m = d.split("");
long p = 0;
if (s == 1) {
p = t;
} else {
for (int i = 0; i < t; i++) {
if (m[i].equals("a")) {
p++;
}
}
}
return p;
}
any sugessions how i can make it fast?
The question is i will given a randon string and i have to count no of "a" in the string where "n" is the no of words that will be repeated.https://www.hackerrank.com/challenges/repeated-string/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup[^]
|
|
|
|
|
I am not sure why you are splitting the string into an array of characters. You can do a simple count by iterating over the elements of the string testing for 'a'. You can use the string length and the number value to calculate how many times the string is repeated. Using that information should lead you to a faster solution. Just remember to allow for a truncated string at the end. And your code would be much easier to understand if you used meaningful names for your variables rather than single letters.
|
|
|
|
|
Thx Sir, In the question there are different case like "ababacc", "aba","a" So what i am doing is i split the words into an array and then add the words as per the letter. there is one more approach that i want to ask will that will more efficiently or not
So i will divide the n from the array size of the string and then (string.length- reminder) and I will have the no of times it is divisible and i will also calculate the no of "a" present in the word
|
|
|
|
|
Like I said, there is no need to split the string. Just count the number of 'a's in the string. From that number, the length of the string, and the repeat count, you can easily calculate the total.
|
|
|
|
|
|
No post
modified 13-Jan-20 9:36am.
|
|
|
|
|
|
Many people would want to help but we're all pretty bad at mind reading. At least most people on this site think that about me.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I think I know what you are thinking now ...
|
|
|
|
|
hello guys!
I need a java project on BUS RESERVATION AND TICKETTING SYSTEM done by object oriented concepts.
so if any one to help me please send me a source code!
|
|
|
|
|
Sorry, but this site does not provide code to order.
|
|
|
|
|
Member 14697150 wrote: please send me a source code! Nope. And it's incredibly rude of you to even ask for that.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
a java project on us reservation sytem
|
|
|
|
|
Yes, that would be a good project.
|
|
|
|
|
Hi guys, this is my code but I got some error. Anyone can help me?
I have an error in executer part.
public class ReaderWriter{
public static void main(String args[]){
ReadWriteLock RW = new ReadWriteLock();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
}
}
class Writer implements Runnable
{
private ReadWriteLock rwlock;
private int writerNum;
public Writer(int w, ReadWriteLock rw) {
writerNum = w;
rwlock = rw;
}
public void run() {
while (true){
SleepUtilities.nap();
System.out.println("writer " + writerNum + " wants to write.");
rwlock.WriteLock(writerNum);
SleepUtilities.nap();
rwlock.WriteUnLock(writerNum);
}
}
}
class Reader implements Runnable
{
private ReadWriteLock rwlock;
private int readerNum;
public Reader(int readerNum, ReadWriteLock database) {
this.readerNum = readerNum;
this.rwlock = database;
}
public void run() {
while (true) {
SleepUtilities.nap();
System.out.println("reader " + readerNum + " wants to read.");
rwlock.ReadLock(readerNum);
SleepUtilities.nap();
rwlock.ReadUnLock(readerNum);
}
};
}
class ReadWriteLock {
private int readerCount;
private Semaphore mutex;
private Semaphore rwlock;
public ReadWriteLock() {
readerCount = 0;
mutex = new Semaphore(1);
rwlock = new Semaphore(1);
}
public void ReadLock(int readerNum) {
try{
mutex.acquire();
}
catch (InterruptedException e) {
}
++readerCount;
if (readerCount == 1){
try{
rwlock.acquire();
}
catch (InterruptedException e) {
}
}
System.out.println("Reader " + readerNum + " is reading. Reader count = " + readerCount);
mutex.release();
}
public void ReadUnLock(int readerNum) {
try{
mutex.acquire();
}
catch (InterruptedException e) {}
--readerCount;
if (readerCount == 0){
rwlock.release();
}
System.out.println("Reader " + readerNum + " is done reading. Reader count = " + readerCount);
mutex.release();
}
public void WriteLock(int writerNum) {
try{
rwlock.acquire();
}
catch (InterruptedException e) {}
System.out.println("Writer " + writerNum + " is writing.");
}
public void WriteUnLock(int writerNum) {
System.out.println("Writer " + writerNum + " is done writing.");
rwlock.release();
}
}
class SleepUtilities
{
public static void nap() {
nap(NAP_TIME);
}
public static void nap(int duration) {
int sleeptime = (int) (NAP_TIME * Math.random() );
try { RW.sleep(sleeptime*1000); }
catch (InterruptedException e) {}
}
private static final int NAP_TIME = 5;
}
|
|
|
|
|
So you have an error somewhere in your code. Do you not think that it would help if you explained exactly what the error is, and which line of code it occurs on?
|
|
|
|
|
The constructor Writer(ReadWriteLock) is undefined
The constructor Reader(ReadWriteLock) is undefined
I have this errors. I made a mistake in main part but I dont know how can I fix it?
|
|
|
|
|
The issue is that the constructor you are using for your reader and writer objects is not defined: you have defined a constructor for each of these classes which takes an integer as first argument, and a readerwriterlock object as second argument, but you actually call non-existant constructors accepting only a readerwriterlock as single argument.
You can either:- define constructors accepting as single readerwriterlock object as argument;
- change your actual constructor calls and add the integer argument which you seem to have forgotten.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
I rewrited main part like this and it worked. But I want to write it with using executer services. But in that way still it is not working.
public class ReaderWriter{
public static final int NUM_OF_READERS = 3;
public static final int NUM_OF_WRITERS = 2;
public static void main(String args[]){
ReadWriteLock rwlock = new ReadWriteLock();
Thread[] readerArray = new Thread[NUM_OF_READERS];
Thread[] writerArray = new Thread[NUM_OF_WRITERS];
for (int i = 0; i < NUM_OF_READERS; i++) {
readerArray[i] = new Thread(new Reader(rwlock));
readerArray[i].start();
}
for (int i = 0; i < NUM_OF_WRITERS; i++) {
writerArray[i] = new Thread(new Writer(rwlock));
writerArray[i].start();
}
}
Also like you said I wrote my functions which take just one argument and removed SleepUtlization class.
class Reader implements Runnable
{
private static int readers = 0;
private ReadWriteLock rwlock;
private int readerNum;
public Reader(ReadWriteLock rwlock) {
this.rwlock = rwlock;
this.readerNum=Reader.readers++;
}
public void run() {
while (true) {
final int DELAY = 5000;
try
{
Thread.sleep((int) (Math.random() * DELAY));
}
catch (InterruptedException e) {
}
System.out.println("reader " + readerNum + " wants to read.");
rwlock.ReadLock(readerNum);
try
{
Thread.sleep((int) (Math.random() * DELAY));
}
catch (InterruptedException e) {
}
rwlock.ReadUnLock(readerNum);
}
};
}
|
|
|
|