עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

java-בעיה בקריאה מקובץ

Featured Replies

פורסם

שלום,

אני אמור לקרוא מקובף טקסט מספרים (כ-int) ואז לשלוח אותם לפונקציה שכבר כתבתי שמכווצת את הקובץ....

בחרתי להשתמש באובייקט של: BufferedReader br

כאשר כל פעם אני קורא מחרוזת- ממיר אותה לint ושולח לפונקציה המכווצת write(int) כפי שניתן לראות:

while(s!=null)
{
int num=Integer.parseInt(s);
cos.write(num);
s=br.readLine();

}

הבעיה היא ש- s (ממחלקת String ) למרות שמקבל null בסוף הקובץ- משום מה עדיין יש כניסה לתוך גוף הלולאה!

אשמח לכל הצעה

תודה

פורסם

במה אתה משתמש לקריאה מהקובץ? FILEINPUTSTREAM? תוכל לצרף את שאר הקוד שקשור לקריאה מהקובץ?

פורסם
  • מחבר

אני מקווה שזה מספיק...

public static void main(String []args) //throws IOException
{
try{
int NumberOfBits; //num of bits- read it from first line in txt file
String s=null; //we read strings from txt file and then parse them to integers

//String inputfile=args[0];
//String outputfile=args[1];

InputStream is= new BufferedInputStream(new FileInputStream(/*inputfile*/"input.txt"));
OutputStream os= new BufferedOutputStream(new FileOutputStream(/*outputfile*/"output.compact"));

BufferedReader br = new BufferedReader(new InputStreamReader(is));
s=br.readLine();
NumberOfBits=Integer.parseInt(s);

CompactOutputStream cos= new CompactOutputStream(os,NumberOfBits);
s=br.readLine();
while(s!=null)
{
int num=Integer.parseInt(s);
cos.write(num);
s=br.readLine();
}
cos.close();
br.close();
}catch(IOException e)
{
System.err.println("an error occured");
}

פורסם

אולי יש לך newline בסוף הקובץ בטעות?

  • 2 שבועות מאוחר יותר...
פורסם

תנסה לשנות את התנאי של ה while ל:

while(s!=null && s.compareTo("")!=0)

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

ארכיון

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

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.