עבור לתוכן

JAVA | פרוייקט - תקוע בשילוב טבלת גיבוב עם קריאה מטקסט

Featured Replies

פורסם

טוב בגדול המטרה שלי היא לקבל מילים מתוך קובץ טקסט את המילים שאני מקבל אני צריך להכניס לטבלת גיבוב

את ההכנסה אני מבצע לפי מערך - כלומר יש לי מערך של מחרוזות של מילים אותם קיבלתי מהקובץ טקסט והאינדקס של כל מילה הוא המיקום בטבלה

אפשר לראות שהטבלה מצויירת יפה לעין :) אבל התקלה שאני לא מצליח לראות אולי בגלל שאני כבר 6 שעות יושב על זה היא השילוב של שני הפעולות

להלן הקוד

import java.util.Arrays;import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class MYPROJECT {


public static void main(String[] args) {

File file = new File("C:\\Dictionary.txt");
String[] theArray;
int HTsize;
int itemsinhashtable=0;


FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
int counter=0;

try {
fis = new FileInputStream(file);


// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
int linenumber = 0;


FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr);





while (lnr.readLine() != null)
{
linenumber++;
}
String[] elementsToAdd = new String[linenumber];
System.out.println("Total number of lines : " + linenumber);


lnr.close();
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0)
{

// this statement reads the line from the file and print it to
// the console.

for(int i=0;i<elementsToAdd.length;i++)
{
elementsToAdd[i]=dis.readLine();
}
for(int q=0;q<elementsToAdd.length;q++)
{
System.out.print(elementsToAdd[q]);
System.out.println();

}

System.out.println(dis.readLine());

}


// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();

HashFunction theFunc = new HashFunction(1000);

theFunc.hashFunction1(elementsToAdd,theFunc.theArray);

theFunc.displayTheStack();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void hashFunction1(String[] stringsForArray,String[] theArray)
{
for(int n=0;n<stringsForArray.length;n++)
{
String newElementVal = stringsForArray[n];

theArray[Integer.parseInt(newElementVal)] = newElementVal;
}
}
HashFunction(int size)
{
HTsize = size;

theArray = new String[size];

Arrays.fill(theArray, "-1");
}
public void displayTheStack()
{
int increment = 0;

for(int m=0;m<3;m++)
{
increment +=10;

for(int n=0;n<71;n++)
System.out.print("-");

System.out.println();

for(int n = increment -1;n<increment;n++)
{
System.out.format("|%3s " + " ",n);
}
System.out.println("|");

for(int n=0;n<71;n++)
System.out.print("-");

System.out.println();
for(int n=increment-10;n<increment;n++)
{
if(theArray[n].equals("-1"))
System.out.print("| ");
else
System.out.print(String.format("| %3s " + " ",theArray[n]));
}
System.out.println("|");
for(int n=0;n<71;n++)
System.out.print("-");

System.out.println();
}
}
}




אשמח לעצה או הכוונה או פשוט תיקון (אם תיקון אז בבקשה להדגיש אותו)

תודה מראש בן,

פורסם

בוא תסביר בצורה יותר מסודרת מה אתה מנסה לעשות. אני מבין שאתה רוצה לקרוא מילים מתוך קובץ ולהכניס אותם לתוך HashMap?

פורסם
  • מחבר

אוקי אז ככה

לקרוא מילים מתוך קובץ טקסט

לאחר הקריאה להכניס את המילים לתוך טבלת גיבוב

כרגע לאחר שינוי הקוד שלי הגעתי לקוד הבא

import java.util.ArrayList;import java.util.Array;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
/**
* Write a description of class Hashtable here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SpellingChecker
{

int HTsize;
int itemsinhashtable=0;
public static void main(String[]args)
{
File file = new File("C:\\Dictionary.txt");
String[] theArray;
int HTsize;
int itemsinhashtable=0;


FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
int counter=0;

try {
fis = new FileInputStream(file);


// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
int linenumber = 0;


FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr);





while (lnr.readLine() != null)
{
linenumber++;
}
String[] elementsToAdd = new String[linenumber];
System.out.println("Total number of lines : " + linenumber);


lnr.close();
// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0)
{

// this statement reads the line from the file and print it to
// the console.

for(int i=0;i<elementsToAdd.length;i++)
{
elementsToAdd[i]=dis.readLine();
}
for(int q=0;q<elementsToAdd.length;q++)
{
System.out.print(elementsToAdd[q]);
System.out.println();

}

System.out.println(dis.readLine());

}


// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();



theArray = new String[linenumber*linenumber];

for(int t=0;t<theArray.length;t++)
{
theArray[t] = "-1";
}


hashFunction1(elementsToAdd,theArray);

displayTheStack(theArray);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


public static void hashFunction1(String[] stringsForArray,String[] theArray)
{
for(int n=0;n<stringsForArray.length;n++)
{
String newElementVal = stringsForArray[n];

theArray[Integer.parseInt(newElementVal)] = newElementVal;
}
}

public static void displayTheStack(String[]theArray)
{
int increment = 0;

for(int m=0;m<3;m++)
{
increment +=10;

for(int n=0;n<71;n++)
System.out.print("-");

System.out.println();

for(int n = increment -1;n<increment;n++)
{
System.out.format("|%3s " + " ",n);
}
System.out.println("|");

for(int n=0;n<71;n++)
System.out.print("-");

System.out.println();
for(int n=increment-10;n<increment;n++)
{
if(theArray[n].equals("-1"))
System.out.print("| ");
else
System.out.print(String.format("| %3s " + " ",theArray[n]));
}
System.out.println("|");
for(int n=0;n<71;n++)
System.out.print("-");

System.out.println();
}
}
}


אם משהו לא ברור אשמח להסביר ותודה על המענה

פורסם

למה יש לך כ"כ הרבה streams? תתחיל בקטע קוד פשוט יותר שקורא מקובץ.

אח"כ, אתה יכול פשוט להזין אותם לתוך HashMap.

תשנה את שם הפונקציה שמכניסה לתוך HashMap. השם hash function מתאר פונקציית גיבוב כלשהי.

כמו כן, תבדוק לולאות for each

פורסם
  • מחבר

אוקי אז מבחינת טבלת גיבוב אני מסודר

נתקעתי בבעיה אחרת והקשבתי לעצתך...

הקוד הבא נותן לי רק שורות מתוך טקסט הוא לא מתייחס לרווחים

אני צריך להעתיק את כל המילים של הטקסט לתוך מערך של מחרוזות

File f = new File("C:\\Dictionary.txt");    FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String word = null;
ArrayList al = new ArrayList();
while ((word = br.readLine()) != null)
{
al.add(word);
}
br.close();
System.out.println("The Words From You're File Is: ");
System.out.println();
System.out.println(al);

איך אני יוכל לתקן?

פורסם

הפונקציה שלך קוראת שורות. ברגע שיש שורה, לא בעיה לפצל אותה - תבדוק את הפונקציה split

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים