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 2 of 2

Thread: Return type of converting integer to string for linked list

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Return type of converting integer to string for linked list

    Here is what I am trying. I am trying to create a method that takes my object LString and converts the integer argument into an object LString. This is using Linked lists. I have a Node class which constructs and initializes nodes. This is the last method for me to complete on a project.

    I am confused on how to go about it however. I have been creating a string class using linked lists and nodes. How do I convert this integer argument into my object type of LString?

    Here are parts of my LString class that seem relevant to the question:

    HTML Code:
    public class LString{
    
       private Node front ;  //first val in list    *******CHANGED
       private Node back;   //last val in list
       private int size = 0;
       private int i;
       private int offset;
    
       public LString(){
          //construct empty list
          Node LString = new Node();
          front = null;
    
       }
    
       //return value of specified index
       public char charAt(int index){
          Node current = front;
          for(int i = 0; 0 < index; i++){
             current = current.next;
          }
          return current.data;
    
       }
    
       //return number of chars of lstring
       public int length(){
          int count = 0;
          Node current = front;
          while(current != null){
             current = current.next;
             count++;
          }
          return count++;
    
       }
    
       public String toString(){
          if(front == null){
             return "[]";
          } else {
             String result = "[" + front.data;
             Node current = front.next;
             while(current != null){
                result += current.data; //might need to add ", page 967
                current = current.next;
             }
             result += "]";
             return result;
          }   
       }

    my attempt although wrong:

    HTML Code:
    public static LString valueOf(int i){
        int c;
        char m;
        Node current = new Node();
        // convert the String to int
        for(int w = i;w < i; w++) {
            c = i % 10;
            i = i / 10;
            m = (char) ('0' + c);
            LString ans = new LString();
        }
        return LString;           
    }
           public void add(int value){
              if(front == null){
                 front = new Node(value);
              } else{
                  Node current = front;
                  while(current.next != null){
                    current = current.next;
                  }
                  current.next = new Node(value);
                }
           }            
        }

    My Node class:

    HTML Code:
    public class Node{
       public char data;
       public Node next;
    
       //constructors from page 956
       public Node()
       {
          this('\0',null);  //'\0' is null char for java
       }
    
       public Node(char initialData, Node initialNext)
       {
          data = initialData;
          next = initialNext;
       }
    
       public void addNodeAfter(char element)   
       {
          next = new Node(element, next);
       }
    
       public char getData()
       {
          return data;
       }
    
       public Node getNext(){  
          return next;   
       }
    
       public void setNext(Node n){
          next = n;
       }
    
       public void setData(char d){
          data = d;
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Return type of converting integer to string for linked list

    method that takes my object LString and converts the integer argument into an object LString.
    Is this the method you are asking about:
    LString theMethodToBeWritten(LString ls, int intArg) {
      //  create LString object to be returned by this method using ls and intArg
      ...
      return theLStringCreatedAbove;
    }
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  3. Help with code for converting 4 digit string to integer
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 09:38 PM
  4. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  5. String substring, concatenation operation on linked list
    By oaks12 in forum Collections and Generics
    Replies: 1
    Last Post: January 15th, 2009, 07:12 AM

Tags for this Thread