JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's not a problem, and you don't have to solve it. Just do what you have been told.
You have been given a very clear set of step-by-step instructions. Just follow them, one step at a time, exaclty like it says.
If you get stuck come back here and show where you have got to.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK.
Your main method is in the inner class InputMenu. It needs to be in the public outer class PersonalProgram

rproffitt commented: Stay classy. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't just say "getting errors:, tell us the complete error messages or worng behaviours.
Anyway, one thing is that you have two different Scanners both using System.in, which is guaranteed to cause problems.
Just open one Scanner at startup and use that for everything

Mitch_3 commented: Error: Main method not found in class personalprogram.PersonalProgram, please define the main method as: public static void main(String[] args) or +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like whatever is inrt.data at line 14 is not a valid string representation of an integer.
But since you don't show that data there's nothing more anyone can say.
You could printrt.data just before calling parseInt to see what values you are processing

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Those that I can answer with some effort that give me a chance to learn something new.

Oh yes. I wish I'd said that as well. 100% agree.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

what you get out of your membership?

I'm now retired with 50 years of programming experience about to follow me into the grave. What I get from DaniWeb is a way to pass on some of that experience and learning to a new generation. It's a way of repaying all those people who did the same for me over the years.

  1. I look for questions I think I can answer
  2. I'm a moderator, so I can everything I actually want to. Also Muhahha
  3. If you're asking a question include ALL the relevent information.
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

AMNA_5 commented: but i have only 2 hour

Then I'm sorry, but you have left it far too late. All you can do is to learn from this mistake and start your next project in good time.

shehzadkhan commented: ok now u can help me now i have 2 days for this +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hello. We can help you learn how to do this, but we won’t do it for you. What have you got so far?

shehzadkhan commented: but i have only 2 hour +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have 2 Scanners both based on System.in
At the end of the accept method you close one of them
Closing a Scanner also closes the underlying stream (see API doc), which is System.in
The you try to read from the other Scanner, but its underlying stream has been closed
... so you get the Exception.

In general you should never have 2 Scanners using the same input stream.

Other notes:
If you have a loop that will always be executed at least once, use a do...while, not a while, the code is much simpler and safer (see my previous post)

Advance warning:
If you mix nextInt() and nextLine() in a Scanner you will fall foul of a trap that the designers stupidly built into Scanner.
See here for explanation.

JC

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's a lot better!
I don't want to give you your homework, so here's an outline that illustrates the sort of structure you can use.
Its an arithmetic app that just adds two numbers ;) but, like yours it needs some values to be set and a result to be queried.
The user input loop goes in the main method because that's what you want o see when the app starts.

class Adder {
   int a,b; 
   getters and setters for a and b
   public int getSum() {return a+b;}

   public static void main() {
      do {
         Adder myAdder = new Adder();
         ask user for value of first number
         myAdder.setA(first number);
         ditto for second number
         print myAdder.getSum()   
         ask user: more?
      } while(more);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like you stopped reading the instructions about half way down. That's always a fatal mistake.

Go back to where it says "Specifications" and do what it says there.
Then write a main method that just does the user I/O and creates/uses an instance of the Reservation class.

The instructions also tell you to use LocalDate for the dates, so there's no reason for you to process month numbers/names yourself.
Hint:LocalDate.of(int year, int month, int dayOfMonth)

phpiscool commented: Hi, James, I responded to your post below. Thank you for responding! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In that case there's no need for marking visited, nor for recursion.
Simply for each square check the horizontal, vertical and diagonal directions until a word is found or you hit the edge.
That's basically 8 loops inside a row/col double loop, but with a bit of cunning you can generalise those 8 into one with suitable parameters.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What's your objective here?
If you just want to get something working quickly then a bit of copy/paste from the web may help
Or... you may be copying something that's poor quality, or not really what you needed, in which case you are in for a whole heap of pain - and how do you know?

If you want to learn how to program and/or develop your programming skills then you have to design and write code yourself.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Switch off your computer. Get a paper and a pencil.
Draw a letter grid and find all the words in it.
Write down exactly how you did that (you may need to do it a few times before you get this right - you need to get every step documented).
Now turn the computer back on and convert what you just wrote into code.

Don't be discouraged. At the beginning you won't be able to see the whole thing. Just make a start and keep going until it's done.

Nobody can do this for you. It's called "learning", and it's worth all the effort.

rproffitt commented: Old school! +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's nothing wrong in the code you posted, so the problem lies somewhere else. Without the whole code and the actual input there's nothing more we can do.

But in general the NoSuchElementException is thrown when the input stream has been closed, so cannot supply any more input for scanning. One way to achieve that is to create a new Scanner using System.in, then close the Scanner, then try to create another. Alternatively it may be a problem how you are running the program - eg multiple versions open at the same time?

rproffitt commented: "Number 5... is alive! Malfunction! Need input!" +15
Andreas_5 commented: I haven't closed any of the inputs and it worked! +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Write some code that searches for a known customer and prints the result!

It's a mistake that all learners make: they write far to much code before they start testing.

Here's an example how to do it so you find mistakes early and easily:
Compile the Customer class (if it needs the Rent class just include the absolute minimum version or Rent to make it possible).
Write main method in Customer with some simple hard-coded tests to create a customer and call each of its public methods, printing the results. Do not move on until those tests work perfectly. Keep that main method and re-run it every time you change the Customer class.
Finish the Rent class and test that as above.
Write the VideoShop class's utility methods and test those as above.
Finally write the user interface and test that.

The idea is that when you run tests there are only a few dozen lines of code that haven't already been proven to work, so any problems will be in those few lines and easy to find.

Remember: experienced coders test early and test often.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I canonly see one call to searchCustomersByName, and that passes null as the parameter.
I can't see any code to print it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So just start one writer and one reader thread instead of the 3 readers in that code.
Set the priority of the threads to give the priority you want.

What's weird about this code is that it creates theads, but just executes their run methods in the current thead.
Try calling start for the Theads on lines 60-63 instead of run

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have the same block of code repeated with only small changes to the variables.
What happens with 20 questions? Do you repeat the same code 20 times? What heppens when you add another question?

That tells you you should be using a loop wth an array answer[] instead of answer1, answer2, answer3 etc.

Refactor the code into a loop, then use either of the solutions I posted earlier.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Presumably you have a loop in which the quesions are asked and answered?
If so just maintain a counter of how many questions and exit the loop after 20,eg (pseudocode)

for (int count = 0; count <20; count++) {
   // do one question
}
endgame

but maybe you have retry loop for incorrect answers for each question, in which case this may work better for you

int count = 0;
mainloop: while true {
   process a question
   every time the user answers...
      if (++count >=20) break mainloop;
}
endgame
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, I thought you had tried a different algorithm.

Unfortunately I don't have the time to plough through uncommented code that defies all normal Java coding conventions, has misleading variable names and incorrect indentation. Eg : you have three things called arrays (one of which is not an array) with no explanation of why.

If you clean up your code, with proper variable names, and comments explaining the algorithm, you will almost certainly find places where you mis-use variables or the code does not match the comments. Fix those, then put sopme print statements in your loops so you can see how your arrays are being updated. That will reveal where things are going wrong.

In the meantime, here are some guesses...
lines 13-36 find the most common char?
line 38 appends it to the output list ... but just one char (should be added count times?)
lines 40-49 remove the char from the source array? (but leave it padded with zeros, which will soon become the most common char?)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It can be done using loops like that, but you have to do something like:

while some input remains...
  find the most frequent character (like in your code)
  add the right number of that character to the output
  remove all instances of that character from the input

A much simpler approach is

Create a Map<Character, Integer>
Popuate it with the chars in the input and their corresponding counts
Get the maps EntrySet as a List and sort it by value descending
(thats just 4 lines of code so far)

build the output string from the sorted list (this is actually the messiest part!)

Finally, if you know the input is just ASCII you can use an array[128] instead of a Map for a wonderfully simple solution (just 6 short and easy lines of code)...

create an array of 128 empty Strings
append each input char to the string at index = the char
sort the array on string length descending
append all the array members to the output

(use StringBuilders instead of Strings if performance is really important)
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A lack of tolerance, not just in the religious sense, is at the heart of most of the problems in the world today ... I value true neutrality

While I agree with that sentiment, I don't agree that we should tolerate everything just because someone believes in it. Racisim, supression of women's rights, FGM - I don't think we should tolerate them regardless of any cultural or religious justification. Anti-vaxxiers, flat earthers, young earth creationists - all promoting patent untruths to children and gullible adults - I don't think we should adopt a neutral policy towards them them by giving them the same significance that we give to evidence-based science.
Yes, of course there's a shortage of tolerance in the world, but we should save our tolerance for people who accept the basic human values of equality, fraternity, truth, and mutual tolerance.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you really have put it all in the main method then you will have no non-static variables, so you cannot get the same message about non-static variables!. Let's see your latest version of the PriceCalculator class.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't worry, it's not really your fault. You are roughly the one millionth person to be trapped by this stupidity in the design of the Scanner clsss.

Here's the explanation (it uses ints, but doubles work the same way).

You have some input with an int followed by some text, eg
101
John Doe
... and you try to read it with
int num = scanner.nextInt();
String name = scanner.nextLine();
... and name is an empty String ("")!

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

Possible fixes:

  1. Add add extra nextLine() between the nextInt and the real nextLine to clear the unwanted \n. This may be difficult if the nextInt and the nextLine are in different areas of code which are not always executed together.
  2. Give up on nextInt (etc) and just read whole lines and parse then into ints (etc) with Integer.parseInt (etc) - in which case you can junk the whole scanner and use a BufferedReader instead.
    new BufferedReader(new InputStreamReader(System.in))

  3. Use this class instead of Scanner:
    https://www.daniweb.com/programming/code/506346/a-simpler-safer-alternative-to-java-util-scanner
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's right.
The only thing the PriceCalculator class does is to run a test case. You can put all that into the main method. There's no need to create an instance, or to have instance variables. Local variables in the main method will do.

guffadi commented: It still gives me the same error message when I put everything in my main method +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 107
getArea is a method in the RoomDimension class, but you try to call it using an instance of RoomFlooring
(and vice-versa for the material costs)

ps
Line 102
your main method is declared as an instance method but it should be static

pps
Java conventions are to use camel case and no underscores for member names, eg roomDimensions, getMaterialCost

guffadi commented: If i make my main method static, it says : non-static variable room flooring cannot be referenced from a static context +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hints:

The String class has a method to convert into an array of chars
The Arrays class has a method to sort arrays of chars
The String class has a constructor that takes an array of chars to create a new String.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If it's so important and urgent then I assume you must have have already started to work on it yourself. Surely you're not waiting for someone else to do it for you (for free). Post what you have done so far.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to remove all the spaces (and maybe other punctuation or non-alphabetic characters) from the input before testing for being a palindrome.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Lots of people can. If you can't it's because you haven't bothered to try.
Try to do it. If you get stuck post your code here with an explanation of why you're stuck, and someone will help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

== and != for objects test for RH and LH expressions both referring to exactly the same object (or not).
The equals method tests that the two objects are equivalent in some sense that depends on what kind of object they are. Eg for two Strings it tests that they both comtain the same sequence of characters.
So if a refers to a String that happens to contain "" the first test will be true, but in general the second will not because they will be two different Strings that just happen to have the same characters.

What can make this a little confusing when trying it out is that Java is very smart about how it stores Strings, and it will re-use an existing String if it has a suitable one. eg

String a = "";  // creates a new ''" object
String b = "";  // re-uses the existing object
// a.equals(b)  and a==b are both true

but

String a = "";  // creates a new"" object
String b = new String("")  // creates another new "" object
// a.equals(b) is true but a==b is false
rproffitt commented: You're right. I was sleepy and know better to write as I doze off. Removing my answer. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That regex is almost right. It removes everything that's not a word character, but regex defines a "word character" as being made of any letters or numbers or underscores. (Presumably that was defined to be useful for parsing variables names ?) Your test data doesn't happen to have numbers or an underscore in it, so it doesn't reveal this bug.

If you just want to process letters in your palindrome test then the simplest correct regex would test explicitly just for letters, eg

s = s.toLowerCase().replaceAll("[^a-z]", ""); // delete everything that's not a-z
V3N0M commented: I am keeping the numbers, just in case I ever come across a question similar to #4 in project Euler in the future. +3
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

have resulted in errors.

What errors exactly? Help us to help you.

rproffitt commented: Yes. "Missing value on line 42" can really speed this along. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Indeed there are cases where a loop can be used instead of recursion, and in those cases the loop will carry less overhead. These are mainly encountered in programming courses when the concept of recursion is first introduced.
(I don't know about Python specifically, but a decent compiler will recognise a tail recursion and optimise the code ayway.)
And of course there are all those algorithms for which recursion is by far the simplest and safest implementation - eg traversing an arbitrary tree structure - in which cases the overhead is a good trade-off.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

RoboVM was a nice open source project that ended up being owned, then killed, by Microsoft in 2016.
In my opinion you would be mad to start a new development on a dead platform.

Moving from Java to Swift isn't that hard, and you'll love the way its designers learned from Java's problems and did it better. It also guarantees the earliest possible and fullest possible support for new IOS versions and devices. The XCode IDE is pretty good too.

rproffitt commented: Since Apple issues its own issue of Java, there's this and more. Get the Xcode IDE and go native. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Wow. When did we last see such a useless pile of nonsense. Almost every line contains an error. There's no way you could turn that into valid Java code.
And the link is to a useless tutorial that doesn't actually explain anything and doesn't even use normal Java coding standards.

If you are looking for a tutorial there's only one that is up-to-date, comprehensive, and correct, and that's Oracle's own...
https://docs.oracle.com/javase/tutorial/

rproffitt commented: Maybe they are trying to make the Kessel run in less than 12 parsecs? +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Driver line 2 is the error. It means you have two lists where you should only have one.
You have a list in your library class, you don’t need another one in the driver.
You add the item to the list in Library, then print the list in Driver - which is still empty.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your title and question are inconsistent!
Is it interfaces in inner classes, inner classes in interfaces?

Interfaces in inner classes:

Yes, sometimes.
Because interfaces are by definition static, they can only be declared in a static inner class.
Thus:

class Testing {
    static class Inner {
        interface InnerInterface {
        }
    }
}

is legal, but

class Testing {
    class Inner {
        interface InnerInterface {
        }
    }
}

is not.

Inner classes in interfaces:

Yes:

interface I {
    class C{}
}

is legal.

ps: You could have got a quicker answer by trying that code yourself!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Time for a confession: this application is close to my heart because, in years gone by, I worked for IBM as a programmer and spent 2 years on on a project to optimise traffic light timings. It had a detailed traffic flow model that synched with real life via inductive sensors on the road, and was used to model the effects of different traffic light timings in real time to select the best timings. IBM even filed a patent in my name for some of the optimisiation techniques. I realise that your app is going to be simpler, but at the same time I know where it could be going...

However... a couple of observations:

I would have to remove car from collection if it was outside the window

road would start and end on intersection or side of panel

Almost certainly a confusion about design. Nothing in your model should refer to the UI in any way. Ditch MVC and you are guaranteed a heap of spaghetti. Alternatively: if you are creating data structures to optimise the UI painting then you are suffering from premature optimisation. How do you even know that there will be a paint performance issue?
Get the model right first, add one or more UI's later.

I would choose C++, because with pointers and references to objects it would be much easier.

I don't have any axe to grind about the relative merits of different languages, but what do you …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Not sure why, but something seems uncomfortable about moving Cars around like that. I guess the code gets quite tangled?
How about just keeping a list of Cars, and having current location (instance of Lane or Intersection) as an instance member of Car?
My guess is that it would work well both for updating the model (updating the Car's current location) and displaying the Cars in their current position, with very little to worry about in terms of concurrency.
J.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You could synchronise drawing and modifying, but that would prevent them running concurrently and immediately halve the performance of the app and make the UI less responsive (not to mention the complexity and overhead of all that synch).

So if you just want to loop thru the list while drawing, despite the model updates happening concurrently...

either use a copy of the List to loop thru the contents:
for (car c : new ArrayList<Car>(cars)) {...

or use a CopyOnWriteArrayList

or use a ListIterator

... but maybe your problem is more complex - eg you are changing the roads and intersections... you can make each list safe individually, but it's harder to ensure that the two lists are consistent across threads. You need to provide more details if this may be the case.

ps: When you ask a question here it's normal to provide some feedback on the answers you get, so others looking for simlilar problems know whether the answer was useful. It's also good manners .

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

GIF uses lossless compression that does not degrade the quality of the image. As such there no a priori reason why steganography woudn't work.

nourimane commented: Changing some pixels only can made a small error in compressed format but if the image is large these errors remain invisible for the human eye. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Which address do you want?
Could be the public address supplied by your ISP
Could be the local address supplied by your router
Could be 127.0.0.1 as above if you want to refer to the computer you are on

rproffitt commented: And I forget to ask which Visual Basic. The actual code to find those other 2 may look very different in VB.Net or VB6. +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

the simplest solution is probably to call an extra nextLine() after the nextDouble()

Please read what I wrote more carefully. I said to add a nextLine(), not a println, and I explained why.

Once again:
The user's input looks something like
"Brown\n123\nSmith\n456\nJones\n789\n" etc

Now remember that nextLine gets all the text up to the newline character and discards the newline char.
But nextDouble gets all the digits that are part of a valid double, then stops. It does not discard any newline char

So in your loop you execute:
nextLine() - returns "Brown" - leaves "123\nSmith\n456\nJones\n789\n"
nextDouble() - returns 123 -leaves "\nSmith\n456\nJones\n789\n"
nextLine - returns "" - leaves "Smith\n456\nJones\n789\n"
nextDouble() - sees "S" whic is not a double. Throws an exception.

so after each nextDouble call you need a call to nextLine() to clear out the \n that was left in the input buffer.

It doesn't mater whether you start the loop by asking for the name or the score. Once the loop gets going you alternate between name and score, with the problem and solution I explained above.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I already told you what causes this probem and how to fix it.
You are wasting your time and ours by asking the same question twice.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thiis code is 8 years old, and was pretty obsolete even then. Java has moved on a lot, eg AWT (as used in this code) was superceeded by Swing in the LAST CENTURY! That's one very good reason not to use it.

The other very good reason is, of course, that you/re supposed to be learning to write programs, and copying some random code from some random student isn't the way to do that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1 At startup you are updating the content of a visible component on a non-swing thread - use invokeLater etc

2 Don't use fixed sizes. That destroys your portability.

3 see other post... paintComponent (no s)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

My machines all have an SSD for the system and applications, and a large HD for data. The large HD is partitioned so it has a bootable clone of the SSD and a second partition for data. I re-do the clone every month or so. So given a serious problem with the SSD I can boot from the HD and keep working / fix things.
That's in addition to normal backups (TrueImage for Windows, TimeMachine for Mac), and also keeping really important data in a DropBox folder on the HD to secure a remote live shared backup.
Is that overkill? I think not.
Is the average user capable of setting that up and maintaining it? I think not.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi ikoren
That example you linked to was created by Amruta Sriram (the originator of this thread) and is exactly the same as (s)he posted here.
The link you suggested is in the original post.
The people at coderanch did a perfectly good job of answering it, so I have no idea why it has been re-posted here.
It looks like Amruta Sriram is a time waster, and I wouldn't waste any of your time unless we get some clarification of why it was re-posted.

JC