Solved

another error

Posted on 2002-05-07
15
189 Views
Last Modified: 2010-03-31
when i try to compile my code i get errors in the public int remove() saying that "Statement not reached" and "Incompatible type for return. Can't convert java.lang.String to int."

can someone please help as i don't know how i can fix it!

class QueueNode
{
     public int Position;
     public String DocumentName;
     public String Owner;
     public int Size;
     
     public QueueNode next;
     public QueueNode previous;    
}


import uuInOut;
class Queue extends QueueNode
{
     QueueNode start;
     QueueNode end;

     
          public Queue()
          {
               start = new QueueNode();
               start = null;
               end = new QueueNode();
               end = null;
          }
         
         
          public void add(int Position,String DocumentName,String Owner,int Size)
          {
               if (start ==null)
               {
                    start=new QueueNode();
                    start.Position = uuInOut.ReadInt();
                    start.DocumentName = uuInOut.ReadString();
                    start.Owner = uuInOut.ReadString();
                    start.Size = uuInOut.ReadInt();
                    start.next = null;
                    start.previous = null;
                    end = start;
               }
               else
               {
                    QueueNode temp = new QueueNode();
                    temp.Position = uuInOut.ReadInt();
                    temp.DocumentName = uuInOut.ReadString();
                    temp.Owner = uuInOut.ReadString();
                    temp.Size = uuInOut.ReadInt();
                    temp.next = end;
                    end.previous = temp;
                    end = temp;
               }
          }
         
          public boolean isEmpty()
          {
               return (start ==null);
          }
         
          public int remove()
          {
               QueueNode temp = new QueueNode();
               if (start ==null)
                    return -1;
               
               else if (start.previous ==null)
               {
               temp = start;
               start = null;
               return temp.Position;
               return temp.DocumentName;
               return temp.Owner;
               return temp.Size;
               }
               else
               {
               temp = start;
               start = start.previous;
               return temp.Position;
               return temp.DocumentName;
               return temp.Owner;
               return(temp.Size);
               }
          }
         
          public void displayAll()
          {
               QueueNode temp = new QueueNode();
               
               temp = start;
               while (temp !=null)
               {
                    System.out.println(temp.Position);
                    System.out.println(temp.DocumentName);
                    System.out.println(temp.Owner);
                    System.out.println(temp.Size);
                    temp = temp.previous;
               }
          }    
}
0
Comment
Question by:penfold69
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 2

Expert Comment

by:mzimmer74
ID: 6994810
Well, you can't have more than one return statement per block of code.  What I'd suggest doing is returning a QueueNode instead of an int.  For example, change the first else of "remove" to:

else if (start.previous ==null)
{
   temp = start;
   start = null;
   return temp;
}

Not sure what you're doing with it, but then you can get the data you want out of it.
0
 

Author Comment

by:penfold69
ID: 6994845
could you provide an example please?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994897
i don't know exactly what your project requirements states, but remove should probably return void (just like add())        

i'm assuming that this is how you want it...
when you add, add onto end of line (after fifth)
when you remove, remove start.

fifth->fourth->third->second->first
   |                            |
  end                         start

if so, your remove should look something like this:
     public void remove()
     {
          if (start == null) { // it's an empty queue do nothing
               return;
          } else if (start.previous == null) { // it's the last one
               start = null;
               end = null;
          } else {
               start.previous.next = null;
               start = start.previous;
          }
     }
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994904
alignment is off, start should point to "first"
0
 
LVL 2

Expert Comment

by:mzimmer74
ID: 6994950
Basically, you have some class that is calling remove (I'm assuming this).  Thus, you need something back from remove.  Let's say you want to print out the information of the node you're removing.  You'd do the following:

QueueNode node = queue.remove();
System.out.println(node.Position);
System.out.println(node.DocumentName);
System.out.println(node.Owner);
System.out.println(node.Size);


But the only way this works is if you return a QueueNode instead of an int like this:

public QueueNode remove()
{
  QueueNode temp = new QueueNode();
  if (start ==null)
    return null;
             
  else if (start.previous ==null)
  {
    temp = start;
    start = null;
    return temp;
  }
  //etc....
}
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994956
penfold: if you post the requirements of your project, it'd be easier for us to help you.
0
 

Author Comment

by:penfold69
ID: 6995043
its like a basic printer queue system

i have to be able to:

enter in the Position, DocumentName, Owner, Size,

list the contents of the queue

print a document from the queue

display how many documents in queue

display size of queue in KB

purge the queue


i was given this code (above) to modify and add to to make all the above work!


hope this is a better explanation!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:penfold69
ID: 6995050
i can offer a lot more points if a code example is given!
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6995088
we all know this is obviously homework...

so, for your benefit, we really can't do all of the above for you.

what we can do, however, is help you with each step of the project.

ie. let's take a look at your add routine

you want to be able to
> enter in the Position, DocumentName, Owner, Size,

so far, your add routine looks like this:

         public void add(int Position,String DocumentName,String Owner,int Size)
         {
              if (start ==null)
              {
                   start=new QueueNode();
                   start.Position = uuInOut.ReadInt();
                   start.DocumentName = uuInOut.ReadString();
                   start.Owner = uuInOut.ReadString();
                   start.Size = uuInOut.ReadInt();
                   start.next = null;
                   start.previous = null;
                   end = start;
              }
              else
              {
                   QueueNode temp = new QueueNode();
                   temp.Position = uuInOut.ReadInt();
                   temp.DocumentName = uuInOut.ReadString();
                   temp.Owner = uuInOut.ReadString();
                   temp.Size = uuInOut.ReadInt();
                   temp.next = end;
                   end.previous = temp;
                   end = temp;
              }
         }

now, i don't know exactly what uuInOut does but, i think what you want should look more like this:

     public void add(int Position,String DocumentName,String Owner,int Size)
     {
          if (start==null)
          {
               start=new QueueNode();
               start.Position = Position;
               start.DocumentName = DocumentName;
               start.Owner = Owner;
               start.Size = Size;
               start.next = null;
               start.previous = null;
               end = start;
          }
          else
          {
               QueueNode temp = new QueueNode();
               temp.Position = Position;
               temp.DocumentName = DocumentName;
               temp.Owner = Owner;
               temp.Size = Size;
                  end.previous = temp;
               temp.next = end;
               end = temp;
          }
     }

// NOTE: the above DOES NOT have the functionality for inserting a document at a position other than at the end of the queue (which as per your comment states, is one of the requirements) I'll leave this part to you...

also, for your remove(), you still didn't tell us what you need returned.  If nothing then, then it should look something like this:
     public void remove()
     {
          if (start == null) { // it's an empty queue do nothing
               return;
          } else if (start.previous == null) { // it's the last one
               start = null;
               end = null;
          } else {
               start.previous.next = null;
               start = start.previous;
          }
     }
if you need to display the results of the node you just removed (ie. "print" it) then take a look at mzimmer's comment above regarding returning a QueueNode from remove().  You would then pass this QueueNode to your print() function (which we haven't seen yet)

either way, what i'd suggest is to make sure your add() and remove() routines work properly... post them here and we can verify it (or you can just test it yourself).
Once you get that, the rest of the project should be a piece of cake.

good luck!

0
 

Expert Comment

by:selsted
ID: 6995949
There is a lot of comments here, which I haven't read through, so it is possible that your question has already been answered.

I will just explain the two errors, and then you can find them yourself. I think you learn the most from that.

Statement not reached comes if you have some code that the compiler knows for sure will never happen:

{
  boolean a=false;
  return a;
  a=true;
  return a;
}

The compiler knows that a=true will never be set, because you have just made a return a. A piece of code that would compile, but do the excact same looks like this:

{
  boolean a=false;
  if(true) return a;
  a=true;
  return a;
}

"Incompatible type for return. Can't convert java.lang.String to int.":

public int X()
{
  String str = "5";
  return str;
}

str is a String object, and not an int. Java doesn't differ between the String "5" and the String "String". The latter won't fit in an int. What you need to do, is convert your String to an int, which can be done using the Integer object.

public int X()
{
  String str = "5";
  int i = Integer.parseInt(str);
  return i;
}

Be aware that if str holds anything other than digits, it will throw a NumberFormatException.
0
 

Expert Comment

by:selsted
ID: 6995958
Another thing, why don't you make your method remove return a QueueNode instead?

public int remove()
{
  QueueNode temp = new QueueNode();
  // ...
  return temp;
}
0
 

Author Comment

by:penfold69
ID: 6996009
thanks i will try this and get back to you as soon as possible!
0
 

Expert Comment

by:selsted
ID: 6996065
> Another thing, why don't you make your method remove return a QueueNode instead?
>
> public int remove()
> {
>  QueueNode temp = new QueueNode();
>  // ...
>  return temp;
> }

It should of course be:
public QueueNode remove(
{
}
0
 

Expert Comment

by:selsted
ID: 6996094
I just took one more look at your code.

There is no need for Queue to extend QueueNode.

When extending, you should think of it this way. Is Queue a QueueNode, or is it just holding QueueNode's. In your code, it is the latter. Being a QueueNode gives Queue itself all the variables and methods of QueueNode.

A brief example:
A Car holds a Motor. Motor is a variable in Car.
A Jaguard is a kind of Car. It extends Car. By doing so, it automatically gets a Motor.
A Garage might hold one or more Car's. It doesn't care if the Car is a Jaguar, or if it even has a working Motor.

A Queue holds two QueueNodes.
A Queue is not a QueueNodes. It doesn't need a position or a DocumentName itself.
0
 

Author Comment

by:penfold69
ID: 6996696
thanks everyone fof your help!!!

:)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now