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

1. 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

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 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)
{
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)
{
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. Re: Program doesn't work!

[code=java]
[/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.

3. 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. 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.

5. Re: Program doesn't work!

The mistake is in this function:
```public static void insert (int 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:
Path:
Location:
Line 41
Line 41
Type:
Java problem
Java problem

6. 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.

7. Re: Program doesn't work!

while (t/2 && heap(t)>heap(t/2))
^
symbol: method heap(int)
while (t/2 && heap(t)>heap(t/2))
^
symbol: method heap(int)
2 errors

8. 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?

Yes.

10. Re: Program doesn't work!

Read the API doc for the class to see how to use it.

11. 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. 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;
^```

13. Re: Program doesn't work!

First, I got error in this function:
```public static void insert (int 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;
}```
}

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. 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.

15. Re: Program doesn't work!

I tried to run program, after first correct output , I get this:
java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)

16. 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)