• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading files  RSS feed

 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone I was wondering if someone could tell me if this is the correct way to load a file It tells me it has no errors except that when i try to run it it tells me it cant find the file

package csc123;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class CSC123
{


public static void main(String[] args) throws IOException
{
File inputFile=new File("CollegeTranscripts.txt");
Scanner input=new Scanner(inputFile);
double grade;
char c ;
double sum;
while(input.hasNext() )
{
String line;
line=input.nextLine();
c = line.charAt(line.length()-1);
switch(c)
{
case 'A': grade=4.00; break;
case 'B': grade=3.00; break;
case 'C': grade=2.00; break;
case 'D': grade=1.00; break;
default: grade=0.00; break;
}
sum = grade;
sum=sum+grade;
DecimalFormat formatter=new DecimalFormat("0.##");
double finalGrade =sum/3;
System.out.print(formatter.format(finalGrade));

}
}
}
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried rewriting the code but to no avail
//David Lopez lab 3 Feb-11-2014
package csc123;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class CSC123
{


public static void main(String[] args) throws IOException
{
String filename;
double points=0;
filename=getFileName();
points=getpoints(filename);
}
public static String getFileName(){
Scanner scanner=new Scanner(System.in);
String file;
System.out.println("Please enter a file name: ");
file=scanner.nextLine();
return file;
}
public static double getpoints(String filename)throws IOException
{
String line;
char c;
double grade=0, sum=0, avg0, cp=0;
int count=0;
File file=new File(filename);
if(! file.exists()){
System.out.println("File: "+ filename + " not found");
System.exit(0);
}

Scanner input=new Scanner(file);

while (input.hasNext())
{
line=input.nextLine();
System.out.println(line);

count++;

c = line.charAt(line.length()-1);
switch(c)
{
case 'A': grade=4.00; break;
case 'B': grade=3.00; break;
case 'C': grade=2.00; break;
case 'D': grade=1.00; break;
default: grade=0.00; break;
}
sum = grade;
sum=sum+grade;
}
DecimalFormat formatter=new DecimalFormat("0.00");
double finalGrade =sum/count;
System.out.print(formatter.format(finalGrade));
input.close();
return cp;



}


}
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah, I guess it's the correct way to load a file. Aside from the fact that there is no "The" way to do anything, that is. There's always different ways to do things.

But anyway your real question is "Why doesn't my code find the file?". And you've apparently found that it doesn't matter whether you use a constant value or a variable value -- why would it, anyway, they contain the same data?

The answer is that your code assumes your file is in your current working directory, whereas it's actually in some other directory. If you're going to use relative paths then you need to know about this concept, so have a look at the Wikipedia article: Working directory. It explains what it is and how to change it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!