heres my code:

import java.util.Scanner;
import java.lang.Integer;  
public class hmwrk4
{

public static void main(String [] args)
{Scanner kbInput = new Scanner(System.in);
System.out.print("number of the element to delete: ");
int numEle = kbInput.nextInt();
Node n8 = new Node(new Integer(8), null);
Node n7 = new Node(new Integer(7), n8);
Node n6 = new Node(new Integer(6), n7);
Node n5 = new Node(new Integer(5), n6);
Node n4 = new Node(new Integer(4), n5);
Node n3 = new Node(new Integer(3), n4);
Node n2 = new Node(new Integer(2), n3);
Node n1 = new Node(new Integer(1), n2);

Node n0 = new Node(new Integer(0), n1);
Node head = new Node(new Integer(0), n0);
Node prev = new Node(null);
Node curr = new Node(new Integer(0),n0);

if(numEle > 8 || numEle < 0)
{System.out.println("error: out of bounds");
 System.exit(0);
}
else if(numEle == 0)
{head = head.getNext();
}
else
{
 
while(curr != null && numEle > curr.getItem())
{prev = curr;   
curr = curr.getNext();
}

if(curr.getNext() == null)
prev.setNext(null); 
else
prev.setNext(curr.getNext());// = curr.getNext();
}
System.out.println("result: ");
curr = head.getNext();
System.out.print(curr.getItem() + " ");

while(curr.getNext() != null)
{curr = curr.getNext();
 System.out.print(curr.getItem() + " ");
}

} // end of main
//end of class 

}

class Node{
private Object item;
private Node next;

public Node(Object newItem){
item = newItem;
next = null;
}

public Node(Object newItem, Node nextNode){
item = newItem;
next = null;
}

public void setItem(Object newItem){
item = newItem;
}

public Object getItem(){
return item;
}

public void setNext(Node nextNode){
next = nextNode;
}
 
public Node getNext(){
return next;
}

 

             }//end of class

The assignment: to delete a user input number of a linked list and printing the result.
My problem:
Error
hmwrk4.java:34: operator > cannot be applied to int,java.lang.Object
while(curr != null && numEle > curr.getItem())

In the event that you need some tips for doing this project, refer to the following links--

http://www.daniweb.com/forums/thread135648.html

http://www.daniweb.com/forums/thread135617.html

http://www.daniweb.com/forums/thread135539.html

--

and as for your error

public Object getItem(){
return item;
}

Object's cannot be used for integral/floating operations like '+' or '>'

You'll need to do something like this in order for it to work--

while(curr != null && numEle > (Integer)curr.getItem())

-- where it will down-cast the Object to its original type (Integer).

Now, behind the scenes the Integer object returned from the cast is "unwrapped" into an integer primitive type and replaced during the operation.

Unfortunately you don't get to see this. Also, don't try to cast an Object directly into an int primitive, because the compiler will not see a generic "Object" as a possible wrapper-class.

commented: Nie post +9

You may also want to consider using Generics over Objects, but it's your call.

import java.util.Scanner;

public class hmwrk4
{
public static void main(String [] args)
{
Scanner kbInput = new Scanner(System.in);
System.out.print("number of the element to delete: ");
int numEle = kbInput.nextInt();

Node n8 = new Node(8, null);
Node n7 = new Node(7, n8);
Node n6 = new Node(6, n7);
Node n5 = new Node(5, n6);
Node n4 = new Node(4, n5);
Node n3 = new Node(3, n4);
Node n2 = new Node(2, n3);
Node n1 = new Node(1, n2);
Node n0 = new Node(0, n1);
Node head = new Node(0, n0);
Node prev = null;
Node curr = head;

if(numEle > 8 || numEle < 0)
{System.out.println("error: out of bounds");
 System.exit(0);
}
else if(numEle == 0)
{head = head.getNext();
}
else
{
    while(curr != null && numEle > curr.getItem())
{prev = curr;
curr = curr.getNext();
}

if(curr.getNext() == null)
prev.setNext(null);
else
prev.setNext(curr.getNext());
}
System.out.println("result: ");
curr = head.getNext();
System.out.print(curr.getItem() + " ");

while(curr.getNext() != null)
{curr = curr.getNext();
 System.out.print(curr.getItem() + " ");
}

} // end of main
//end of class
}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
class Node{
private int item;
public Node next;

public Node(){
next = null;
}

public Node(int newItem){
item = newItem; 
next = null;
}

public Node(int newItem, Node nextNode){
item = newItem;
next = null;
}

public void setItem(int newItem){
item = newItem;  
}

public int getItem(){
return item;
}

public void setNext(Node nextNode){
next = nextNode;
}

public Node getNext(){
return next;   
}
}

this is my code now, it compiles but when I run it, I get a null pointer exception at line 38.

And that stack trace tells you exactly which line it occurs on, so where is it? Have you walked through the execution to see why that variable would still be null when you try to access it?

line 38

line 38

Ok here's the problem--

while(curr != null && numEle > curr.getItem()) // executes until either curr is null or
{                                                                  // numEle > curr.getItem()
    prev = curr;
    curr = curr.getNext();
}

if(curr.getNext() == null // curr was probably null, so accessing null.getNext() throws
                                    // unchecked exception

If you change curr != null to curr.getNext() != null, line 38-39 may not give you a problem

import java.util.Scanner;   

public class hmwrk4
{ 
public static void main(String [] args)
{  
Scanner kbInput = new Scanner(System.in);
System.out.print("number of the element to delete: ");
int numEle = kbInput.nextInt();
   
Node n8 = new Node(8, null);
Node n7 = new Node(7, n8);
Node n6 = new Node(6, n7);
Node n5 = new Node(5, n6);
Node n4 = new Node(4, n5);
Node n3 = new Node(3, n4);
Node n2 = new Node(2, n3);
Node n1 = new Node(1, n2);
Node head = new Node(0, n1);
Node prev = null;
Node curr = head;
//
if(numEle > 8 || numEle < 0)   
{//
  System.out.println("error: out of bounds");
  System.exit(0);
} else if(numEle == 0)
{//
  head = head.getNext();
} else
{//
 while(curr.getNext() != null && numEle > curr.getItem())//
 {//prev = curr;
curr = curr.getNext();
 }
}


prev.setNext(curr.getNext());
  
System.out.println("result: ");
curr = head.getNext();
System.out.print(curr.getItem() + " ");
  
while(curr.getNext() != null)
{curr = curr.getNext();
 System.out.print(curr.getItem() + " ");
} 
   
} // end of main
//end of class  
}
//**************************************************************************
//**************************************************************************
//**************************************************************************

  
class Node{
private int item;
public Node next;
  
public Node(){
next = null;
}
  
public Node(int newItem){
item = newItem; 
next = null;    
}
  
public Node(int newItem, Node nextNode){
item = newItem;
next = null;
}
  
public void setItem(int newItem){
item = newItem;  
}
  
public int getItem(){
return item;
}
  
public void setNext(Node nextNode){
next = nextNode;
}
 
public Node getNext(){
return next;
}

 
  
      }//end of class

I get a
Exception in thread "main" java.lang.NullPointerException
at hmwrk4.main(hmwrk4.java:40)
during runtime after making the correction

Node prev = null; // prev is null
Node curr = head;
//
if(numEle > 8 || numEle < 0)   
{//
  System.out.println("error: out of bounds");
  System.exit(0);
} else if(numEle == 0)
{//
  head = head.getNext();
} else
{//
 while(curr.getNext() != null && numEle > curr.getItem())//
 {//prev = curr;
curr = curr.getNext();
 }
}


prev.setNext(curr.getNext()); // calling null.setNext flags the null pointer error

Try to analyze your code carefully so you don't get null errors!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.