7 Replies - 864 Views - Last Post: 27 May 2012 - 11:13 AM Rate Topic: -----

#1 maryam_sultan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 12

String and file in java

Posted 27 May 2012 - 01:45 AM

Hi, this my question, and i dont know how to use sting and file in my program.. can anyone help me plz..



Each student’s ID and test answers are stored in the file “students.txt”. The output will be written on another file “results.txt” that contains each student’s ID, number of correct answers, and grade, along the single highest score for the class.


------------------------------------------------------------

this is my program


   import java.util.Scanner;

   public class StudentGrade
   {
      static int correctScores[]={0,1,1,0,1,0,0,1,1,0};
      static int studentID[]= new int [5];
      static int noCorrectAnswers[]={0,0,0,0,0};
      static char grade[]=new char[5];
   
      static Scanner OM =new Scanner(System.in);
   
      public static void main(String args[])
      {
         for(int i=0;i<5;i++)
         {
            getStudentData(i);
         
            if(noCorrectAnswers[i]>=8)
               grade[i]='A';
            else if(noCorrectAnswers[i]>=5)
               grade[i]='B';
            else if(noCorrectAnswers[i]>=2)
               grade[i]='C';
            else
               grade[i]='F';
         }
      
      
         for(int k=0;k<5;k++)
         {
            System.out.println("Student ID: "+studentID[k]);
            System.out.println("Student correct answers no: "+noCorrectAnswers[k]);
            System.out.println("Student grade: "+grade[k]);
         }
      
         int max =0;
         for(int i=0;i<5;i++)
            if (noCorrectAnswers[i]>max)
            
               max=noCorrectAnswers[i];
         System.out.println("The highest score: " + max );
      
      }
   
   
      public static void getStudentData(int i)
      {
         while(true)
         {
            System.out.print("Enter student ID: ");
         
            int sid=OM.nextInt();
            if(checkID(sid))
            {
               studentID[i]=sid;
               break;
            }
            else
               System.out.println("This ID already exists");
         }
      
         System.out.println("Enter student scores:");
         for(int j=0;j<10;j++)
         {
            int scr=OM.nextInt();
            if(scr==0||scr==1)
            {
               if(scr==correctScores[j])
                  noCorrectAnswers[i]++;
            }
            else
            {
               System.out.println("The answer must be 0 or 1");
               j--;
            }
         }
      }
   
   
      public static boolean checkID(int sid)
      {
         for(int i=0;i<5;i++)
            if(studentID[i]==sid)
               return false;
         return true;
      }
   }



Is This A Good Question/Topic? 0
  • +

Replies To: String and file in java

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: String and file in java

Posted 27 May 2012 - 02:13 AM

Is your issue that you don't understand the concepts of Strings or File I/O in Java?

What's your issue? What lines of code aren't working? Please elaborate.

Unfortunately, we can't learn for you.

Strings tutorial
File I/O tutorial
Was This Post Helpful? 0
  • +
  • -

#3 maryam_sultan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 12

Re: String and file in java

Posted 27 May 2012 - 02:48 AM

aha ok i will post for u my code agine
Was This Post Helpful? 0
  • +
  • -

#4 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: String and file in java

Posted 27 May 2012 - 03:11 AM

I compiled your code and went through the paces with made up data. Here's the compiler output:

compiler said:

Student correct answers no: 5
Student grade: B
Student ID: 16789
Student correct answers no: 5
Student grade: B
Student ID: 19876
Student correct answers no: 5
Student grade: B
The highest score: 10


I don't see the issue.

What's the purpose of the program? What's it not doing that you want it to do?
Was This Post Helpful? 0
  • +
  • -

#5 maryam_sultan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 12

Re: String and file in java

Posted 27 May 2012 - 05:12 AM

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

   public class StudentGrade
   {
      static int correctScores[]={0,1,1,0,1,0,0,1,1,0};
      static int studentID[]= new int [5];
      static int noCorrectAnswers[]={0,0,0,0,0};
      static char grade[]=new char[5];
   
      static Scanner OM =new Scanner(System.in);
   
      public static void main(String args[])throws Exception
      {
         for(int i=0;i<5;i++)
         {
            getStudentData(i);
         
            if(noCorrectAnswers[i]>=8)
               grade[i]='A';
            else if(noCorrectAnswers[i]>=5)
               grade[i]='B';
            else if(noCorrectAnswers[i]>=2)
               grade[i]='C';
            else
               grade[i]='F';
         }
      
      
         for(int k=0;k<5;k++)
         {
            System.out.println("Student ID: "+studentID[k]);
            System.out.println("Student correct answers no: "+noCorrectAnswers[k]);
            System.out.println("Student grade: "+grade[k]);
         }
      
         int max =0;
         for(int i=0;i<5;i++)
            if (noCorrectAnswers[i]>max)
            
               max=noCorrectAnswers[i];
         System.out.println("The highest score: " + max );
      
      }
   
   
      public static void getStudentData(int i)throws Exception
      {
      
         Scanner inFile=new Scanner (new FileReader("Student.txt"));
         PrintWriter outFile=(new PrintWriter("Result.txt"));
         while(inFile.hasNext())
         
         {
         
            while(true)
            {
               System.out.print("Enter student ID: ");
            
               int sid=inFile.nextInt();
               if(checkID(sid))
               {
                  studentID[i]=sid;
                  break;
               }
               else
                  System.out.println("This ID already exists");
            }
         
            System.out.println("Enter student scores:");
            for(int j=0;j<10;j++)
            {
               int scr=OM.nextInt();
               if(scr==0||scr==1)
               {
                  if(scr==correctScores[j])
                     noCorrectAnswers[i]++;
               }
               else
               {
                  System.out.println("The answer must be 0 or 1");
                  j--;
               
               }
               inFile.close();
               outFile.close();
            }
         }
      
      }
   
      public static boolean checkID(int sid)
      {
         for(int i=0;i<5;i++)
            if(studentID[i]==sid)
               return false;
         return true;
      }
   }




the issue i create a file named Student.txt that has this data
{
Student ID:2010

student scores:
0
1
1
0
1
0
0
1
1
0


Student ID:2011

student scores:
1
0
0
1
0
1
0
1
1
0

Student ID:2012

student scores:
1
0
0
1
0
1
1
0
0
1


Student ID:2014

student scores:
0
1
0
0
0
0
0
0
1
0

Student ID:2015

student scores:
0
1
1
0
1
1
1
0
1
0
}

so i want another file which named result to show the output .. you get my issue
Was This Post Helpful? 0
  • +
  • -

#6 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: String and file in java

Posted 27 May 2012 - 05:34 AM

Honestly... I'd suggest a tutorial. The issue is how you're reading and writing to your files. Sorry that I can't be of more help. :P

Oracle - Reading, Writing, and Creating Files

After that, if you have any more problems, then I'd be glad to further assist you.
Was This Post Helpful? 0
  • +
  • -

#7 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: String and file in java

Posted 27 May 2012 - 09:04 AM

Source

View Postx68zeppelin80x, on 27 May 2012 - 03:42 AM, said:

Why don't you just save the query as a text file i.e. test.sql and read it in using the java.util.Scanner class?

Scanner Read File

Quote

import java.io.File;
import java.util.Scanner;

public class Exercise {
    public static void main(String[] args)  throws Exception {
	// Indicate that you are planning to opena file
	File fleExample = new File("Example.xpl");
        // Prepare a Scanner that will "scan" the document
        Scanner opnScanner = new Scanner(fleExample);

	// Read each line in the file
        while( opnScanner.hasNext() ) {
            // Read each line and display its value
	    System.out.println("First Name:    " + opnScanner.nextLine());
	    System.out.println("Last Name:     " + opnScanner.nextLine());
	    System.out.println("Hourly Salary: " + opnScanner.nextLine());
	    System.out.println("Is Full Time?: " + opnScanner.nextLine());
	}
            
    	// De-allocate the memory that was used by the scanner
        opnScanner.close();
    }
}



Instead of printing each line in the while... just:
String sql = "";
while( opnScanner.hasNext() ) {
  sql += opnScanner.nextLine();
}


This post has been edited by x68zeppelin80x: 27 May 2012 - 09:05 AM

Was This Post Helpful? 0
  • +
  • -

#8 maryam_sultan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 12

Re: String and file in java

Posted 27 May 2012 - 11:13 AM

I rewrite the code agine but ill i have problem with the output the file result created but with out output.. i can't get the problem when i compiler it I don't have problem but when run i have

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

   public class StudentGrade
   {
      static int correctScores[]={0,1,1,0,1,0,0,1,1,0};
      static int studentID[]= new int [5];
      static int noCorrectAnswers[]={0,0,0,0,0};
      static char grade[]=new char[5];
   
      static Scanner OM =new Scanner(System.in);
   
      public static void main(String args[])throws Exception
      
      {
      
                  
         for(int i=0;i<5;i++)
         {
            getStudentData(i);
            
            if(noCorrectAnswers[i]>=8)
               grade[i]='A';
            else if(noCorrectAnswers[i]>=5)
               grade[i]='B';
            else if(noCorrectAnswers[i]>=2)
               grade[i]='C';
            else
               grade[i]='F';
         }
         
         
         for(int k=0;k<5;k++)
         {
            System.out.println("Student ID: "+studentID[k]);
            System.out.println("Student correct answers no: "+noCorrectAnswers[k]);
            System.out.println("Student grade: "+grade[k]);
         }
         
         int max =0;
         for(int i=0;i<5;i++)
            if (noCorrectAnswers[i]>max)
               
               max=noCorrectAnswers[i];
         System.out.println("The highest score: " + max );
         
      }
           
      public static void getStudentData(int i)throws FileNotFoundException
      {
      
         {
            Scanner inFile=new Scanner (new FileReader("Student.txt"));
            PrintWriter outFile=(new PrintWriter("Result.txt"));
         
            while(inFile.hasNext())
            
            {
            
            
               while(true)
               {
                  System.out.print("Enter student ID: ");
               
                  int sid=inFile.nextInt();
                  if(checkID(sid))
                  {
                     studentID[i]=sid;
                     break;
                  }
                  else
                     System.out.println("This ID already exists");
               }
            
               System.out.println("Enter student scores:");
               for(int j=0;j<10;j++)
               {
                  int scr=inFile.nextInt();
                  if(scr==0||scr==1)
                  {
                     if(scr==correctScores[j])
                        noCorrectAnswers[i]++;
                  }
                  else
                  {
                     System.out.println("The answer must be 0 or 1");
                     j--;
                  
                  }
                  inFile.close();
               
                  outFile.close();
               }
            
            }
         
         }
      }
   
      public static boolean checkID(int sid)throws FileNotFoundException
      {
         for(int i=0;i<5;i++)
            if(studentID[i]==sid)
               return false;
         return true;
      }
   }




each time i run the program i get this

Enter student ID: Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at StudentGrade.getStudentData(StudentGrade.java:65)
at StudentGrade.main(StudentGrade.java:20)

What exactly does "Exception in thread "main" java.util.InputMismatchException" mean and how can I fix it so my program runs properly?


and this is the question can anyone told me my work until now was correct or not and help me plz to complet it


Mr ahmed always gives True/False tests to her class. Her tests have always 10 questions. The maximum class size is 5. She needs a program that will calculate the students’ grades based on the best grade.

Grade

A will range from the best score, to the best score minus 2.

B will range from the best score minus 3, to the best score minus 5.

C will range from the best score minus 6, to the best score minus 8.

F will be anything below the best score minus 8.

Each student’s ID and test answers are stored in the file “students.txt”. The output will be written on another file “results.txt” that contains each student’s ID, number of correct answers, and grade, along the single highest score for the class.

Develop a Java program for the above problem.

· Use two files, one for input and another one for output.

· Use four one-dimensional arrays – one for the correct scores and the other three for the needed output.

· Test if a student’ ID number has been entered previously before storing it in the array: each ID number cannot appear more than one time in the array.

· Check if the answer given for each question is a value equal to 0 or 1. If it is not the case, the user must re-enter the value again.

· Use methods in writing your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1