Lu Kim Hui

Greenhorn
+ Follow
since Jul 06, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
9
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Lu Kim Hui

thanks alot guys! really appreciate for the helps
11 years ago

ohh...i i din't know about split/pattern/matcher.. is it same with StringTokenizer? cause currently what i am learning now is StringTokenizer ,but willing to learn more new codes if got that chance
11 years ago

Ulf Dittmer wrote:

Lu Kim Hui wrote:i add sum = 0 after line 50


That's the wrong place. You need to do that after a summation is done, not right in the middle of it.



thanks! it works, adding after line 56
11 years ago
Sorry i am new to java, this is my exercise given by my lecturer which must meet the requirement by using
o Use .equals(“”) from String class to make sure it is not reading an empty line
o Use .indexOf() from String class to verify whether the ‘+’ exist in each line.
If the ‘+’ exist use StringTokenizer token = new StringTokenizer(value, “+”)
to extract the numbers. Also use token.hasMoreTokens() to iterate through the numbers.


Well, this topic is abit confusing for me... that's why i am seeking for guidance

About the file name checking, it's my previous exercise which need me to do so... and this is just a add on exercise by using indexOf and StringTokenizer

[UD: For clarity, I removed your quote of Campbell's post. Quoting an entire post isn't useful, especially if you're posting right after it. ]
11 years ago

Ulf Dittmer wrote:You need to reset "sum" to zero whenever you have output the result of a summation.



umm.. i add sum = 0 after line 50 ,the results shows 1st and 3rd line 0... any more ideas ??
11 years ago
sales.txt
3000+200
1000
2000+1+2
5000



What I want the text file above display a new result which means sum them up whenever detect a "+" . Can someone please guide me how to code?
3200
1000
2003
5000

the result of the following code is
3200
1000
5203 <<< because sum = sum + first line result how to avoid this happen?
5000







Class SalesCounter



Class SalesCounterDemo

11 years ago

Campbell Ritchie wrote:Paper

There are two sorts of paper when I am around.

  • Type A paper: this is white and is a hazard to life and limb because I try to write on it and nobody can read it.
  • Type B paper: this is not all white, but has writing on already, and is a hazard to life and limb because the chances of my losing it are in proportion to the importance of the writing.

  • In my opinion, you should instantiate that List inside the constructorYou should also avoid if (...) b = true ; else b = false; Look here for a better example.
    Are you closing a Scanner using System.in? You shouldn’t do that. To find out why, add this line after line 20



    Thanks for the advice..... i am still a newbie
    11 years ago

    Winston Gutkowski wrote:Another tip for you: Get used to reading your stacktraces thoroughly.

    Go through them and work out exactly which statements are involved and the order they were invoked. Personally, I use a yellow marker pen; but I'm an old fart and I hate looking at large programs on screens (and I recycle my paper ).

    Winston



    ok thanks man!
    11 years ago

    Winston Gutkowski wrote:

    Lu Kim Hui wrote:THANKS ALOT!!!


    Another satisfied customer. And isn't it much better when you work it out for yourself?

    Winston



    Yup !
    11 years ago

    fred rosenberger wrote:This line:

    private ArrayList<Integer> salesList;

    says "Eventually, I will create an AraryList that can hold Integers, and I will refer to it by the name salesList".

    That's like saying "Someday, i will have a dog and call him 'Rover'". That doesn't mean you HAVE a dog - it just means that someday you will, and you've already picked out the name.

    You never create the ArrayList. So, when you further down say "add something into the ArrayList named 'salesList'", the JVM is saying "ummm....WHAT arraylist? There is no such thing!!!"



    thanks!
    11 years ago

    Winston Gutkowski wrote:

    Lu Kim Hui wrote:Erm... because this is an exercise given from my lecturer, and the requirement is reading from the file and store inside an INTEGER arraylist and lastly print them out ^^"


    No. That's the what. You're trying to find out why you're getting an NPE. Have a look at my last question again.

    Winston


    omg!! miss out the

    THANKS ALOT!!!
    11 years ago
    Erm... because this is an exercise given from my lecturer, and the requirement is reading from the file and store inside an INTEGER arraylist and lastly print them out ^^"

    saleList was the Integer arraylist
    11 years ago

    Campbell Ritchie wrote:Yes, that is what he means. Enter the following line before line 35 and after line 34:





    this is the result:

    Please enter file name to read from (sales.txt) :
    sales.txt
    salesList != null false


    but the NPE errors are still the same
    11 years ago
    java.lang.NullPointerException
    at SalesCounter.readFromFile(SalesCounter.java:35)
    at SalesCounterDemo.main(SalesCounterDemo.java:6)
    at __SHELL1.run(__SHELL1.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:724)
    java.lang.NullPointerException
    at SalesCounter.printSalesList(SalesCounter.java:48)
    at SalesCounterDemo.main(SalesCounterDemo.java:7)


    Sorry, this you mean?

    11 years ago
    sorry! edited... the full codes are up there
    11 years ago