#a program that reads out twitter follower data but it would have an error when typing the file in
87 messages · Page 1 of 1 (latest)
alright
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
public class Driver {
static ArrayList users = new ArrayList();
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException {
String filename;
int userID;
/*
* Reading file and pulling in data
*/
/**/
System.out.print("nEnter the filename holding the user data:");
filename = input.nextLine();//*/
filename = "social_network.edgelist";
//filename = “finalTestList.edgelist”;
System.currentTimeMillis();
System.out.println("nTesting the file input:" );
new BufferedReader(newFileReader(filename));
System.out.println("tReading data from the file…");
new Stack();
new HashSet<Object>(1000000);
System.out.println("ntThe filename" + "filename"
+ "was not found.");
System.exit(1);
/**
* System.out.println(“ntUsers:”); for (TwitterUser t: users){
* System.out.println(“tUser ” + t); }/
**/
/*
* Unit test of the getNeighborhood function
*/
System.out
.print("nTest for the getNeighborhood(user, depth)function:"
+ "ntEnter the user you would like to test: ");
userID = input.nextInt();
UserID();
System.out
.print("tEnter the depth you would like to go to in yourneighborhood: ");
return;
}
private static Reader newFileReader(String filename) {
// TODO Auto-generated method stub
return null;
}
private static TwitterUser UserID() {
// TODO Auto-generated method stub
return null;
}
private static TwitterUser getUser() {
// TODO Auto-generated method stub
return null;
}
// Followers && Popularity begin
/**
* Walk through the list of users and find match
*
* @param followers
*
*/
private static void mapFollowers(Stack followers) {
while (!followers.isEmpty()) {
Integer[] anEntry = (Integer[]) followers.pop();
boolean aUserId = anEntry[1] != null;
List f = findFollowersForUser(
(Stack) followers.clone(), aUserId);
System.err.println("User " + aUserId + " has " + f.size()
+ " followers.");
TwitterUser aUser = getUser();
aUser.setFollowers(f);
}
}
/**
* given a userID id find all followers in stack
*
* @param sList
* @param aUserId
* @return a list of followers
*/
private static List findFollowersForUser(Stack sList,
boolean aUserId) {
List f = new ArrayList();
while (!sList.isEmpty()) {
Integer[] ids = (Integer[]) sList.pop();
if (aUserId = ids[1] == 0) {
f.add(ids[0]);
// System.err.println(ids[0] +” : “+ ids[1]);
}
}
return f;
}
public static void test_getFollowing(TwitterUser user) {
Collection followerUsers = user.getFollowing(user);
List followers = new ArrayList();
for (Object aUser : followerUsers) {
followers.add(((TwitterUser) aUser).getID());
}
System.out.println("User " + user.getID() + " has " +followers
+ " followers. ");
}
private static Object getByPopularity(int idx) {
Collections.sort(users);
return users.get(idx);
}
// Followers && Popularity end
}
Driver class
import java.util.Collection;
import java.util.List;
public abstract class TwitterUser implements Comparable, Cloneable {
private int id;
private ArrayList following = new ArrayList();
public TwitterUser() {
}
public TwitterUser(int userID) {
id = userID;
}
public void getNeighborhood(Object temp2, int depth) {
System.out.println("tUser " + temp2 + ", Friends: "
+ ((TwitterUser) temp2).getFollowing());
if (depth <= 0) {
// do nothing
} else {
for (int i = 0; i < depth; i++) {
Object temp = following.get(i);
getNeighborhood(temp, 0);
}
}
}
public void followUser(TwitterUser user) {
if (!following.contains(user)) {
following.add(user);
}
}
public void clearFollowing() {
following.clear();
}
public ArrayList getFollowing() {
ArrayList copy = new ArrayList();
TwitterUser[] follower = null;
for (TwitterUser t : follower); {
Object t = null;
copy.add(t);
}
return copy;
}
public int getID() {
return id;
}
public void setUserID(int newId) {
id = newId;
}
public int compareTo(TwitterUser t) {
if (id > t.getID()) {
return 1;
} else if (id < t.getID()) {
return -1;
} else {
return 0;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TwitterUser other = (TwitterUser) obj;
if (id != other.id)
return false;
return true;
}
public Object clone() {
try {
TwitterUser clonedUser = (TwitterUser) super.clone();
clonedUser.following = (ArrayList) following.clone();
return clonedUser;
} catch (CloneNotSupportedException e) {
return null;
}
}
public String toString() {
return Integer.toString(id);
}
// Followers begin
/*
*
* return a collection of the people who are following theuser
*/
public Collection getFollowing(TwitterUser user) {
Collection tUser = new ArrayList();
if (user.getID() == 415) {
System.out.print("nuser id: " + user.getID());
for (Object t : user.getFollowers()) {
}
return tUser;
}
return tUser;
}
private List followers = new ArrayList<>();
public void setFollowers(List f) {
for (Object i : f) {
if (!followers.contains(i))
followers.add(i);
}
}
public List getFollowers() {
return followers;
}
// Followers end
}
TwitterUser Class
import java.util.Comparator;
public abstract class UserComparator implements Comparator {
public int compare(TwitterUser o1, TwitterUser o2) {
int res = 0;
// 3. If two users have the same number of followers and arefollowing
// the same number of people, sort by user id (smallest tolargest)
if ((o1.getFollowers().size() == o2.getFollowers().size())
&& (o1.getFollowing().size() ==o2.getFollowing().size()))
res = (o1.getID() < o2.getID() ? 1 : 0);
// 2. If two users have the same number of followers, sort bythe number
// of people that user is following (largest to smallest)
if (o1.getFollowers().size() == o2.getFollowers().size())
res = o1.getFollowing().size() > o2.getFollowing().size() ? 1: 0;
// 1. Number of followers (largest to smallest)
if (o1.getFollowers().size() > o2.getFollowers().size())
res = 1;
return res;
}
}
UserComparator Class
newFileReader is returning null
So the bufferedreader is nullpointing
Since it's trying to read from null
Ahhh
I assume you want return new FileReader(new File(filename));
now it's starting to give this error on return;
You need to return something
I've never seen this but try removing throws clause?
I don't seem to see any throws clause in the program??
I'm sorry I'm still learning 😭
it's still giving out the same error sadly
honestly I'm not sure?
I tried giving a void to the public static the void but that just caused a problem with the return statement
which didn't fix that issue either
I'm running the Driver class
again I do apologize, I'm still getting use to working in java
Oh
Don't return anything from main
Remove return; from bottom of main
Well any returns at all from any void method
Well I tried removing all returns from Main
and that's now giving me a bunch more errors
Yea?
What are the errors
it's the same error as last time
now i'm just confused XD
trying anything I can but it's still giving me this 😭
Paste driver again
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
public class Driver {
static ArrayList users = new ArrayList();
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException {
String filename;
int userID;
/*
* Reading file and pulling in data
*/
/**/
System.out.print("nEnter the filename holding the user data:");
filename = input.nextLine();//*/
filename = "social_network.edgelist";
//filename = “finalTestList.edgelist”;
System.currentTimeMillis();
System.out.println("nTesting the file input:" );
new BufferedReader(newFileReader(filename));
System.out.println("tReading data from the file…");
new Stack();
new HashSet<Object>(1000000);
System.out.println("ntThe filename" + "filename"
+ "was not found.");
System.exit(1);
/**
* System.out.println(“ntUsers:”); for (TwitterUser t: users){
* System.out.println(“tUser ” + t); }/
**/
/*
* Unit test of the getNeighborhood function
*/
System.out
.print("nTest for the getNeighborhood(user, depth)function:"
+ "ntEnter the user you would like to test: ");
userID = input.nextInt();
UserID();
System.out
.print("tEnter the depth you would like to go to in yourneighborhood: ");
}
private static Reader newFileReader(String filename) {
// TODO Auto-generated method stub
}
private static TwitterUser UserID() {
// TODO Auto-generated method stub
}
private static TwitterUser getUser() {
// TODO Auto-generated method stub
}
// Followers && Popularity begin
/**
* Walk through the list of users and find match
*
* @param followers
*
*/
private static void mapFollowers(Stack followers) {
while (!followers.isEmpty()) {
Integer[] anEntry = (Integer[]) followers.pop();
boolean aUserId = anEntry[1] != null;
List f = findFollowersForUser(
(Stack) followers.clone(), aUserId);
System.err.println("User " + aUserId + " has " + f.size()
+ " followers.");
TwitterUser aUser = getUser();
aUser.setFollowers(f);
}
}
/**
* given a userID id find all followers in stack
*
* @param sList
* @param aUserId
* @return a list of followers
*/
private static List findFollowersForUser(Stack sList,
boolean aUserId) {
List f = new ArrayList();
while (!sList.isEmpty()) {
Integer[] ids = (Integer[]) sList.pop();
if (aUserId = ids[1] == 0) {
f.add(ids[0]);
// System.err.println(ids[0] +” : “+ ids[1]);
}
}
}
public static void test_getFollowing(TwitterUser user) {
Collection followerUsers = user.getFollowing(user);
List followers = new ArrayList();
for (Object aUser : followerUsers) {
followers.add(((TwitterUser) aUser).getID());
}
System.out.println("User " + user.getID() + " has " +followers
+ " followers. ");
}
private static Object getByPopularity(int idx) {
Collections.sort(users);
users.get(idx);
}
// Followers && Popularity end
}
newFileReader still returning null
Where if I may ask??
newFileReader method still returns null
do you mean
private static Reader newFileReader(String filename)
Yes
??
Alright I managed to fix the problem with the file reader but now I just have to fix up the twitteruser stuff for the program
@hollow crown did you solve this?
Still no dice for me
I got it to read out the data but I can't have it display the data
ok there's a ton of crazy stuff in those classes so i'm not sure what you're trying to accomplish there
but if you just want to read the contents of a file you can just use Files.readAllLines(new File(filename).toPath())
this will return the lines of the file as a List<String>
but there are loads of problems with that Driver class
I know 😭
alright that seemed to work
now I just need for the data to be displayed
#bot-cmds message
Well not really like that
I mean like taking the data inside of a file and display it out in the program
Sorry being a huge bother 😭
not sure what you mean
or you can explain what you want your program to do and we can go from there
Ok, so I'm basically trying to have the program display out data from a file called social_network.edgelist and display the data from that file in the program from greatest to least as twitter followers instead of just displaying out the filename in the program