i am having a problem to store the item and call from it.It is like you already created a text file,then you need to assess item from it.the item is in disorder. :sad:

Dani AI

Generated

Brief diagnosis (what's actually breaking)

  • The stray semicolon after the token-check turns the if into a no-op; code then always calls nextToken() even when no token exists. was right to flag that and to recommend trim().
  • The types/order passed into the Assessment constructor don’t match its signature: numeric fields (student id, mark) must be parsed from their String tokens first.
  • result is declared as an Assessment[] but your code assigns a single Assessment to the array variable; you need to store each new object into an element (result[size] = ...) or use a List<Assessment> instead. Also check the filename — the sample shows "ResultFile.txt." (trailing dot) which will prevent opening the file.

A clear, safe approach

  • Use a dynamic list so you don't hit fixed-size problems. Split the CSV line, trim each column, check length, parse numbers with Integer.parseInt/Double.parseDouble, then construct Assessment with correct types/order. If you must keep an array, write into result[size] then size++. Add a small catch for NumberFormatException and skip malformed lines.

Example (different style from the original posts):

List<Assessment> list = new ArrayList<>();
try (BufferedReader r = new BufferedReader(new FileReader("ResultFile.txt"))) {
    r.readLine(); // skip header/subject if present
    String row;
    while ((row = r.readLine()) != null) {
        String[] cols = row.split(",");
        if (cols.length < 4) continue;
        int id = Integer.parseInt(cols[0].trim());
        String student = cols[1].trim();
        String item = cols[2].trim();
        double mark = Double.parseDouble(cols[3].trim());
        list.add(new Assessment(student, item, id, mark));
    }
}

Quick checklist before re-running

  1. Remove the stray semicolon and use trim().
  2. Parse numeric tokens to int/double before calling the constructor.
  3. Store into result[index] or use a List.
  4. Guard prints with a check on size/list.size() to avoid NPE/IndexOutOfBounds.
  5. Verify the exact filename (no extra dot) and test with a tiny sample file.

This ties together 's pointer about the if and trimming, and gives the concrete fixes for the constructor/array mistakes.

Recommended Answers

All 4 Replies

I don't know what you are asking for. Can you clarify?

Can you show an example of what you are trying to do and what problem you are having with getting it to work?

It also helps if you ask specific questions.

while (line!=null)
{



tokens = new StringTokenizer(line, ",",false);
String assessNameToken = tokens.nextToken();
if((tokens.hasMoreTokens()) );
int maxScoreToken = Integer.parseInt(tokens.nextToken());
int scaleScoreToken = Integer.parseInt(tokens.nextToken());


descript = new Assessment(assessNameToken, maxScoreToken, scaleScoreToken);
size++;
line = readDescription.readLine();


}

is almost like tat..i wanted to read the string file...but i dont think it can uses the parseString..i wanted to store into an array and called it afterward for further use...i dunno whether i make the question clear o not??

Firstly,

The line with the code

if((tokens.hasMoreTokens()) );

is basically useless. This checks if there are tokens and does nothing. What you want is

if((tokens.hasMoreTokens()) )
{
	//put code here
}

Also, you might want to use the trim() function when getting tokens. This cleans out the string of weird spaces at the beginning and end:

String someTokenHere = tokens.nextToken().trim();

For more help,

import java.io.*;
import java.util.*;


public class try2
{
public static void main(String[] args)
{
Assessment[] result= new Assessment[10];


String line,line1, subjectName,token;
int lineNum=0;
int size=0;
int time=0;


try
{


BufferedReader readResult= new BufferedReader(new FileReader("ResultFile.txt."));



subjectName = readResult.readLine();



line = readResult.readLine();



StringTokenizer tokens;


while (line!=null)
{



tokens = new StringTokenizer(line, ",",false);
String studentIdToken = tokens.nextToken();
if(tokens.hasMoreTokens() );
String studentToken = tokens.nextToken();
String assessmentItemToken = tokens.nextToken();
double assessmentMarkToken = Double.parseDouble(tokens.nextToken());


result = new Assessment(studentIdToken, studentToken, assessmentItemToken,assessmentMarkToken);
size++;
line = readResult.readLine();


}
System.out.println(subjectName);  // check whether the infomation have been stored
System.out.println(result[2].getStudentId());
System.out.println(result[3].getStudent());
System.out.println(result[4].getAssessmentItem());
System.out.println(result[4].getAssessmentMark());



}



catch(FileNotFoundException e)
{
System.err.println("file not found");
}


catch(IOException e)
{
System.err.println(e);
}
}


}


class Assessment
{
private String student, assessmentItem;
private int studentId;
private double assessmentMark;


public Assessment(String name,String item, int id, double mark)
{
student=name;
assessmentItem=item;
studentId=id;
assessmentMark=mark;
}



public double getAssessmentMark()
{
return assessmentMark;
}


public int getStudentId()
{
return studentId;
}


public String getAssessmentItem()
{
return assessmentItem;
}


public String getStudent()
{
return student;
}


}

i still dunno wat happen to my program..it says tat cannot find the symbol constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.