We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,304 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Linked List project

So my java class is a joke, and we're expected to learn how to do our assignments via the web...I'm a hard worker, but I'm just not good at this.
Assignment pic
There's the assignment image. I think i got how to do the first part with the adding random numbers to the list and sorting...but I can't find other resources online that help me understand the rest. Here's my code right now

import java.util.*;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;


public class LinkedListProgram
{
    public static void main(String[] args) throws IOException
    {
        int i,number, ran;
        Scanner sc = new Scanner(System.in);
        LinkedList<Integer> list = new LinkedList<Integer>();
        ListIterator li;
        String line;
        Random random = new Random();
        int pick = random.nextInt(150);
        ListIterator listIterator = list.listIterator();
        System.out.println("Enter # of nodes");
        number = sc.nextInt();
        if (number > 0)
        {
            for (i = 0; i < number; i++)
            list.add(1+ (int)(Math.random()*150));
            Collections.sort(list);
            System.out.print(list + "\n");
            System.out.println(listIterator.nextIndex());

    }
        else
                System.out.println("\nnumber is less than 0\n");
    }
}

Thanks for any help solving this. I'm not familiar with any of this stuff, and everything I do know is based off of what I've found via google searches. I'm nowhere near as profficient at this as you may think.

2
Contributors
7
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
8
Views
Question
Answered
sonicx2218
Light Poster
42 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What is "the rest" that you are trying to do?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Out of everything I'm trying to do, the thing that gives me the most trouble is making my linked list include index numbers to the left of the actual numbers. Right now my list look like [45, 140, 78], but I want my numbers to have the index numbers to their left and look like:

0 45
1 140
2 78
(I'm not positive if the first index number is 0 or 1)

sonicx2218
Light Poster
42 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

That print out looks like what comes from the list class's toString() method. You will need to do your own formatting for the output you are looking for.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Ok that does help a bit, I never thought of trying that. Just a question. I placed the list into a string. I've tried to post the code I used but I get some weird error when I try to post here so i can't give you it now.
It prints without the brackets now, but I was wondering if you knew how to make a string print the numbers out one per line instead of them all on one line?

sonicx2218
Light Poster
42 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
String s = list.toString();
System.out.println(s.substring(1, s.length()-1));

here's the code, I think it'll let me post this time

sonicx2218
Light Poster
42 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

one per line

Use the println method to print what you want on a line by itself.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Got it to work via the iterator! Thanks for the help

sonicx2218
Light Poster
42 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by NormR1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0837 seconds using 2.77MB