好的,所以我必须创建一个链接列表,询问用户文本文件名(我已经导入了文本文件,只是提示用户输入)。文件只是数字列表,但系统必须一次与其中两个进行交互。在每个节点,系统必须存储每个列表中的一个变量(总共两个)。我的代码是完整的,但是当我运行它时,它不会提示我输入列表,只打印一个空的链表,我该怎么办?
这是我的FileIn类:
import java.io.BufferedReader;
//import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class FileIn
{
String fname1, fname2;
public void fileIn() throws FileNotFoundException
{
getFileName();
readFileContents();
}
public void readFileContents() throws FileNotFoundException
{
InputStream input1 = new FileInputStream(new File(fname1));
InputStream input2 = new FileInputStream(new File(fname2));
boolean loop;
BufferedReader in1, in2;
String x, y = null;
int j, k, length;
//char ch;
try
{
in1 = new BufferedReader(new InputStreamReader(input1));
in2 = new BufferedReader(new InputStreamReader(input2));
LinkedList list = new LinkedList();
loop = true;
while(loop)
{
if(null == (x = in1.readLine()) && null == (y = in2.readLine()))
{
loop = false;
in1.close();
in2.close();
}
else
{
System.out.println("line = "+ x + " " + y);
j = 0;
k = 0;
length = x.length();
while((x = in1.readLine())!=null)
{
System.out.println("line["+j+"] = " +x.charAt(j));
}
while((y = in2.readLine())!=null)
{
System.out.println("line["+k+"] = " +y.charAt(k));
}
}
list.insertBack(x, y);
}
}
catch(IOException e)
{
System.out.println("Error " + e);
}
}
public void getFileName()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter first file name: ");
fname1 = in.nextLine();
System.out.println("Enter second file name: ");
fname2 = in.nextLine();
System.out.println("File name entered was " + fname1);
System.out.println("File name entered was " + fname2);
in.close();
}
}
这是我的Node类
public class Node
{
private String x, y;
private Node next;
public Node(String x, String y)
{
this.x = x;
this.y = y;
this.next = null;
}
public Node getNext()
{
return next;
}
public String getX()
{
return x;
}
public String getY()
{
return y;
}
public void setNext(Node n)
{
next = n;
}
}
这是我的LinkedList类:
import java.io.FileNotFoundException;
public class LinkedList
{
public Node head;
//private int n;
private double size, meanX, meanY, stanDevX, stanDevY, sumX, sumY, sqSumX,
sqSumY, xTimesYsum, r, rSq, slope, intercept, regression;
public static void main(String[] args) throws FileNotFoundException
{
FileIn f = new FileIn();
LinkedList list = new LinkedList();
System.out.println(list.showList());
}
public LinkedList()
{
this.head = null;
}
public void insertBack(String x, String y)
{
if(head == null)
{
head = new Node(x, y);
}
else
{
Node newNode = new Node(x, y);
Node curr = head;
while(curr.getNext() != null)
{
curr = curr.getNext();
}
curr.setNext(newNode);
size++;
}
}
public String showList()
{
int i = 0;
String retStr = "List nodes:\n";
Node curr = head;
while(curr != null)
{
i++;
retStr += "Node " + i + ": " + curr.getX() + ", " + curr.getY() + "\n";
curr = curr.getNext();
}
return retStr;
}
public double getXsum()
{
sumX = 0;
Node curr = head;
while(curr != head)
{
String text = curr.getX();
double k = Double.parseDouble(text);
sumX += k;
curr = curr.getNext();
}
return sumX;
}
public double getYsum()
{
sumY = 0;
Node curr = head;
while(curr != head)
{
String text = curr.getY();
double k = Double.parseDouble(text);
sumY += k;
curr = curr.getNext();
}
return sumY;
}
public double calcMeanX()
{
getXsum();
meanX = 0;
meanX = sumX/size;
return meanX;
}
public double calcMeanY()
{
getYsum();
meanY = 0;
meanY = sumY/size;
return meanY;
}
public double getSqSumX()
{
calcMeanX();
sqSumX = 0;
Node curr = head;
while(curr != null)
{
String text = curr.getX();
double k = Double.parseDouble(text);
sqSumX += (k-meanX)*(k-meanX);
curr = curr.getNext();
}
return sqSumX;
}
public double getSqSumY()
{
calcMeanY();
sqSumY = 0;
Node curr = head;
while(curr != null)
{
String text = curr.getY();
double k = Double.parseDouble(text);
sqSumY += (k-meanY)*(k-meanY);
curr = curr.getNext();
}
return sqSumY;
}
public double calcStdX()
{
calcMeanX();
getSqSumX();
stanDevX = Math.sqrt(sqSumX/(size-1));
return stanDevX;
}
public double calcStdY()
{
calcMeanY();
getSqSumY();
stanDevY = Math.sqrt(sqSumY/(size-1));
return stanDevY;
}
public double getxTimesYsum()
{
xTimesYsum = 0;
Node curr = head;
while(curr != null)
{
String text1 = curr.getX();
String text2 = curr.getY();
double k1 = Double.parseDouble(text1);
double k2 = Double.parseDouble(text2);
xTimesYsum += (k1 * k2);
curr = curr.getNext();
}
return xTimesYsum;
}
public double calcCorrelation()
{
r = 0;
getXsum();
getYsum();
getSqSumX();
getSqSumY();
getxTimesYsum();
r = (size * xTimesYsum - sumX * sumY)/
(Math.sqrt((size * sqSumX - (sumX*sumX)) * (size * sqSumY - (sumY * sumY))));
return r;
}
public double calcCorrelationSq()
{
rSq = 0;
calcCorrelation();
rSq = (r * r);
return rSq;
}
public double calcSlope()
{
slope = 0;
getxTimesYsum();
calcMeanX();
calcMeanY();
getSqSumX();
slope = ((xTimesYsum) - (size * meanX * meanY))/
((sqSumX) - (size * (meanX * meanX)));
return slope;
}
public double calcIntercept()
{
intercept = 0;
calcMeanX();
calcMeanY();
calcSlope();
intercept = meanY - (slope * meanX);
return intercept;
}
public double calcRegression()
{
regression = 0;
calcIntercept();
calcSlope();
calcMeanX();
regression = intercept + (slope * meanX);
return regression;
}
}