Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What's wrong with the code???  RSS feed

 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need help getting this thing to compile. The compiler does not like the Catch in the StudentDirectory method. It also does not like my code for the lookupRecord method, but I am not sure why.

Thanks!!!

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

public class StudentDirectory {

private StudentRecord [] directory;
private int size = 0;

StudentDirectory(String inFileName){

try{

File inFile = new File("c:/in.txt");
Scanner inStream = new Scanner (inFileName);
directory = new StudentRecord [20];
int grades;
String names;

while (inStream.hasNext()){

names = inStream.next();
grades = inStream.nextInt();
directory [size] = new StudentRecord(names, grades);
size++;}}

catch (IOException e){

System.out.println ("I/O ERROR");
System.exit (1);}}

int lookupRecord(String name){

for(int i=0;i<directory.length;i++){

if(directory[i].equals(name)){
return (directory[i].getGrade ());}

else {

return -1;}}}

void addOrChangeRecord(String name, int grade){

for(int i=0;i<directory.length;i++){

int oldNumber = 0;

if(directory[i].equals(name)){

oldNumber = directory[i].getGrade ();
directory[i].setGrade(grade);}

if (size == directory.length);{

StudentRecord[] temp = new StudentRecord[directory.length * 2];
System.arraycopy(directory, 0, temp, 0, directory.length);
directory = temp;}}}

void removeRecord(String name){

for(int i=0;i<directory.length;i++){

if(directory[i].equals(name)){

directory [i -1] = null;
size--;}}}

void display (){

for (int i = 0; i < size; i++){

System.out.print(directory[i].getName() + ' ');
System.out.println(directory[i].getGrade());}}

void save(String outFileName){

try{

PrintWriter outfile = new PrintWriter ( new FileWriter (outFileName));
for (int i = 0; i < size; i++){

outfile.print(directory[i].getName() + ' ');
outfile.println(directory[i].getGrade());}

outfile.close ();}

catch (IOException e){

System.out.println ("I/O ERROR");
System.exit (1);}}}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the StudentRecord class?
 
Craig Tyler
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with the catch is that you are constructing the Scanner from a string, not a File, so it doesn't throw the expected FileNotFoundException.

As for the problem in your lookupRecord method, I think you'll find that if you look at the logic, your problem would be solved. If you find a match within the for loop, by all means return from there, but that else clause shouldn't really be there. Just return -1 after the for loop.

Last things.

Too much code. No formatting. No output from the compiler. I just copied this, wrote a StudentRecord class, and ran it rather than wade through all the code. Please be kind to us
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class StudentRecord{

private String name;
private int grade;

StudentRecord (String name, int grade){

this.name = name; //Initializes name.
this.grade = grade; //Initializes grade.}

public StudentRecord (){

grade = 0; // Sets grade equal to 0.
name = null; // Gives name a null value.}

public String getName (){

return name; //Returns name.}

public int getGrade (){

return grade; //Returns grade.}

public void setGrade (int aGrade){

grade = aGrade;}}
 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help!!!

PS. On the formatting, this is not exactly how my code looks. I took out a lot of spaces when I posted it here to keep the code as short as possible.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that there are Code Tags to keep your formatting intact.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!