Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Program doesn't work!

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program doesn't work!

    I hope that I translate task well :
    A bread is being sold at the bakery. At the start of the day n bread are baked each of them with quality x. As time passes new bread are baked with various qualities, customers come in and buy the bread. When a customer comes to the salesperson: "Give me the best bread you've got!" The salesperson takes the best bread and sells it to the customer. Help the bakery to have better bussines by always selling their customers the best bread. n(1<=n<=100000) is given and after that n numbers x. The number x describes the quality of the bread. The number m follows after that, then m(1<=m<=100000) numbers y. y describes the events during the day. If y is zero. it means that a customer came and bought the best bread , so it has to write out the quality of the sold bread or "No" if there is no bread. If y isn't zero, it means that a new bread has been baked of the quality y which can be bought from that moment on.

    Input:
    5
    15 32 4 18 29
    12
    0 0 50 0 24 97 0 0 0 0 0 0

    Print:
    32 29 50 97 24 18 15 4 No

    (Task is like binary tree)
    I programmed this task in C++ and works.


    Code:

    #include <iostream>
    #include <vector>
    using namespace std;

    vector<int> heap;

    void insert(int x)
    {
    heap.push_back(x);
    int t=heap.size()-1;
    while (t/2 && heap[t]>heap[t/2])
    {
    int temp=heap[t/2];
    heap[t/2]=heap[t];
    heap[t]=temp;
    t/=2;
    }
    }

    void out()
    {
    heap[1]=heap.back();
    heap.pop_back();
    int t=1,r;
    while(1)
    {
    if (t*2+1<heap.size())
    {
    if(heap[t*2]>heap[t*2+1]) r=t*2;
    else r=t*2+1;
    }
    else if(t*2<heap.size()) r=t*2;
    else break;
    if(heap[r]>heap[t])
    {
    int temp=heap[t];
    heap[t]=heap[r];
    heap[r]=temp;
    t=r;
    }
    else break;
    }
    }

    int main ()
    {
    heap.push_back(0);
    int n,x;
    cin>>n;
    for(int i=0;i<n;i++)
    {
    cin>>x;
    insert(x);
    }
    int m,y; cin>>m;
    for(int i=0;i<m;i++)
    {
    cin>>y;
    if (!y)
    {
    if (heap.size()>1)
    {
    cout<<heap[1]<<' ';
    out();
    }
    else cout<<"No";
    }
    else insert(y);
    }
    return 0;
    }

    But, when I try translate in Java it doesn't work . I programmed in Eclipse, when compiled I get Java exception breakpoints (2 items) -unknown location, and can't insert data entry (numbers) .

    import java.util.*;
    public class Rad 
    {
    public static java.util.ArrayList<Integer> heap = new java.util.ArrayList<Integer>();
    public static Scanner cin = new Scanner(System.in);
    public static void insert(int x)
    {
    	heap.add(x);
    	int t = heap.size() - 1;
    	while (  t/2!=0 && (heap.get(t) > heap.get(t/2)))
           {
            int temp = heap.get(t / 2);
    	heap.set(t / 2, heap.get(t));
    	heap.set(t, temp);
    	t /= 2;
    }}
    public static void out()
    {
    	heap.set(1, heap.get(heap.size()));
    	heap.remove(heap.size());
    	int t = 1,r;
    	while (true)
    	{
            if (t * 2 + 1 < heap.size()){
    	if (heap.get(t * 2) > heap.get(t * 2 + 1))
    	r = t * 2;
            else	r = t * 2 + 1;}
    	else if (t * 2 < heap.size())
    	r = t * 2;
    	else break;
    	if (heap.get(r) > heap.get(t))
           {
            int temp = heap.get(t);
    	heap.set(t, heap.get(r));
    	heap.set(r, temp);
    	t = r;
            }
    	else break;
    }}
    public static void main(String[] args)
    {
    	heap.add(0);
    	int n;
    	int x;
     
    	n = cin.nextInt();
    	for (int i = 0;i < n;i++)
    	{
    		x = cin.nextInt();	
    		insert(x);
    	}
    	int m;
    	int y;
    	m = cin.nextInt();
    	for (int i = 0;i < m;i++)
    	{
            y = cin.nextInt();
    	if (y != 0)
    	{
             if (heap.size() > 1)
    	{
            System.out.print(heap.get(1));
    	System.out.print(' ');
    	out();
            }
    	else
    	{
             System.out.print("No");}}
    	else
    		insert(y);
    }}}

    Can you help me to translate well or fix the code?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Move all the }s to their own line and align them with the statement with the pairing {


    it doesn't work
    Please explain what the code does when compiled and executed and what is wrong with the results.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program doesn't work!

    I found mistake and fixed 1 line, but, can you tell me ,how to defined heap(int) in this line : while (t/2 && heap(t)>heap(t/2))
    Java problem: The method heap(int) is undefined for the type "Rad"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    Please copy the full text of the error messages and paste it here.


    The post code needs to be wrapped in code tags and properly formatted.
    Unformatted code is hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program doesn't work!

    The mistake is in this function:
    public static void insert (int x)
    {
    	heap.add(x);
    	int t = heap.size() - 1;
    	while (t/2 && heap(t) > heap(t/2))   // error 
           {
            int temp = heap.get(t / 2);
    	heap.set (t / 2, heap.get(t));
    	heap.set (t, temp);
    	t / = 2;
           }
    }
    Description:
    The method heap(int) is undefined for the type Rad
    The method heap(int) is undefined for the type Rad
    Resource:
    Rad.java
    Rad.java
    Path:
    /Rad/src
    /Rad/src
    Location:
    Line 41
    Line 41
    Type:
    Java problem
    Java problem

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    Can you use the javac compiler to compile the code and generate error messages. I'm not familiar with what you have posted.

    heap(t) > heap(t/2)
    Where is the heap() method defined? The above code has two calls to a method named: heap.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program doesn't work!

    Rad.java:41: error: cannot find symbol
    while (t/2 && heap(t)>heap(t/2))
    ^
    symbol: method heap(int)
    location: class Rad
    Rad.java:41: error: cannot find symbol
    while (t/2 && heap(t)>heap(t/2))
    ^
    symbol: method heap(int)
    location: class Rad
    2 errors

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    symbol: method heap(int)
    The compiler can not find a method named heap.
    There is an ArrayList named heap. Are you trying to get at its contents?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program doesn't work!

    Yes.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    Read the API doc for the class to see how to use it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program doesn't work!

    Can you tell me what this error mean:
    error: Class names, 'Rad', are only accepted if annotation processing is explicitly requested
    1 error

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    Please post the full text of the compiler's error message that includes the source statement with the error.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program doesn't work!

    First, I got error in this function:
    public static void insert (int x)
    {
    	heap.add(x);
    	int t = heap.size() - 1;
    	while (t/2 && heap.get(t)>heap.get(t/2))  
           {
            int temp = heap.get(t / 2);
    	heap.set (t / 2, heap.get(t));
    	heap.set (t, temp);
    	t / = 2;
           }
    }


    Rad.java:40: error: bad operand types for binary operator '&&'
    while (t/2 && heap.get(t)>heap.get(t/2))
    ^
    first type: int
    second type: boolean
    1 error


    Then,I changed line:
    while (t/2 && heap.get(t)>heap.get(t/2))
    to
    while( t/2 != 0 && heap.get(t)>heap.get(t/2))
    Now, when I compile program, nothing happened (I think program work now), but I can't insert input number to check exactness.
    In Eclipse,compile working, but when I run the program,got unknown problem.I can post all code if you need to see the problem.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    when I compile program, nothing happened
    That's good. It means there were no compiler errors.
    Or are you talking about when the code is executed?
    What does the program not do that makes you say "nothing happened"?

    What statements did not execute that you think should be executed? Are the statements inside a loop? What was the value of the boolean expression that controls the loop?
    Add a println() statement to print out the values of the variables used in the boolean expression so you can see what the computer sees when it executes the code.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program doesn't work!

    I tried to run program, after first correct output , I get this:
    Exception in thread "main" 32
    java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Rad.out(Rad.java:44)
    at Rad.main(Rad.java:25)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Program doesn't work!

    Exception in thread "main" 32 java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Rad.out(Rad.java:44)
    The arg to the get() method on line 44 is past the end of the arraylist. Remember indexes range in value from 0 to the size-1. If the arraylist has 6 items, the max index is 5.
    Check that the code does not use an index past the arraylist size-1
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. No idea why this program doesn't work
    By Diesel298 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2012, 12:01 PM
  2. Program works fine but with friend it doesn't work
    By Rizza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 1st, 2011, 12:20 PM
  3. Replies: 6
    Last Post: November 25th, 2011, 02:58 PM
  4. Replies: 3
    Last Post: October 4th, 2011, 09:03 PM
  5. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM