473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Please help me fix this code

I'm trying to write a program that read a file containing student data in this format
Expand|Select|Wrap|Line Numbers
  1. studntId | Name     | Major                | GPA  | YEAR    |
  2. ---------|----------|----------------------|------|---------|
  3. 123456   | John Doe | Computer Science     | 3.5  | Junior  |
  4. 789012   | Mary Boe | Computer Information | 3.7  | Senior  |

the program is supposed to read the file and organize the data according to the students last name and look like this
Expand|Select|Wrap|Line Numbers
  1. studntId | Name     | Major                | GPA | YEAR    |
  2. ---------|----------|----------------------|-----|---------|
  3. 789012   | Mary Boe | Computer Information | 3.7 | Senior  |
  4. 123456   | John Doe | Computer Science     | 3.5 | Junior  |
  5.  
Here is the code:


Expand|Select|Wrap|Line Numbers
  1. package lab13;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5. public class DataSorting {
  6.  
  7.     public static void main(String[] args) throws FileNotFoundException {
  8.         int[]studentId = new int[25];
  9.         String[]firstName = new String[25];
  10.         String[]lastName = new String[25];
  11.         String[]major = new String[25];
  12.         double[]gpa = new double[25];
  13.         String[]year = new String[25];
  14.  
  15.         Scanner input = new Scanner(new File("studentrecords.txt"));
  16.         int count = 0;
  17.         while(input.hasNextDouble() && count<25)
  18.         {
  19.             count++;
  20.             for(int i=0; i<25; i++)
  21.             {
  22.                 studentId[i] = input.nextInt();
  23.                 firstName[i] = input.next();
  24.                 lastName[i] = input.next();
  25.                 major[i] = input.next();
  26.                 gpa[i] = input.nextDouble();
  27.                 year[i] = input.next();
  28.                 //System.out.printf("%10d %15s %15s %15s %15d %15s %n",studentId[i], firstName[i], lastName[i], major[i], gpa[i], year[i]);
  29.             }
  30.         }
  31.  
  32.         bubbleSort(studentId, firstName, lastName, major, gpa, year);
  33.         System.out.println("after sorting: ");
  34.         System.out.println("    Exam Scores     First Name   Last name");
  35.         for(int i=0; i<studentId.length; i++)
  36.             System.out.printf("%10d %15s %15s %n", studentId[i], firstName[i], lastName[i]);
  37.     }
  38.  
  39.     public static void bubbleSort (int[]dataId, String[]fname, String[]lname, String[]major, double[]gpa, String[]year)
  40.     {
  41.         boolean isSorted;
  42.         for (int R=0; R<dataId.length-1; R++)
  43.         {
  44.             do{
  45.                 isSorted = true;
  46.                 for(int i=1; i<dataId.length-R; i++)
  47.                 {
  48.                     if((lname[i].compareTo(lname[i-1])<0))
  49.                     {
  50.                         int temp1 = dataId[i];
  51.                         String temp2 = fname[i];
  52.                         String temp3 = lname[i];
  53.                         String temp4 = major[i];
  54.                         double temp5 = gpa[i];
  55.                         String temp6 = year[i];
  56.                         dataId[i] = dataId[i-1];
  57.                         fname[i] = fname[i-1];
  58.                         lname[i] = lname[i-1];
  59.                         major[i] = major[i-1];
  60.                         gpa[i] = gpa[i-1];
  61.                         year[i] = year[i-1];
  62.                         dataId[i-1] = temp1;
  63.                         fname[i-1] = temp2;
  64.                         lname[i-1] = temp3;
  65.                         major[i-1] = temp4;
  66.                         gpa[i-1] = temp5;
  67.                         year[i-1] = temp6;
  68.                         isSorted = false;
  69.                     }
  70.                 }
  71.             }
  72.             while(!isSorted);
  73.             }
  74.         }
  75.     }
  76.  
Dec 2 '09 #1
7 2035
mrjohn
32
So what's the problem with it?
Dec 2 '09 #2
Frinavale
9,735 Expert Mod 8TB
This is not bubble sort.

Please research the topic of Bubble Sort so that you can have a concrete understanding of how this sort algorithm works before you attempt to implement anything.

Once you have an understand of how bubble sort works, you should consider making use of the Object Oriented Programming model that Java has to offer.

Create a class (called "Student" to keep things clear).

Then Create a List/Collection of Student Objects based on what you read from the file.

Once you have a list /collection of Student Objects preforming the bubble sort will be a lot easier.

-Frinny
Dec 2 '09 #3
the problem with the code is that I cannot get the arrays to store the value/strings read in the file.
I've been going over the code all day and still don't see a problem with it.
I keep getting this error:
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
I'm sorry, i'm a beginner at this...

thanks Frinavale for fixing up my earlier post.. new to the site..didn't know i could do that :)
Dec 2 '09 #4
Frinavale
9,735 Expert Mod 8TB
It's very hard for me to read your code.
Have you considered implementing a class like I recommended earlier?
Dec 3 '09 #5
I do not know how to implement a class?
Here is what I think is giving me the error but I'm not sure how to fix it.
Please let me know what you think about it.
Thank you.

Expand|Select|Wrap|Line Numbers
  1. package lab13;
  2. import java.util.*;
  3. import java.io.*;
  4. public class DataSorting {
  5.  
  6. public static void main(String[] args) throws FileNotFoundException {
  7. //DECLARING THE ARRAYS THAT WOULD STORE VALUES READ    FROM FILE
  8. int[]studentId = new int[25];
  9. String[]firstName = new String[25];
  10. String[]lastName = new String[25];
  11. String[]major = new String[25];
  12. double[]gpa = new double[25];
  13. String[]year = new String[25];
  14.  
  15. //INITIALIZING THE SCANNER TO OPEN FILE
  16. Scanner input = new Scanner(new File("studentrecords.txt"));
  17. int count = 0;
  18.  
  19. //USING A WHILE LOOP TO STORE THE NEXT READ VARIABLE IN THE FILE
  20. while(input.hasNextDouble() && count<25)
  21. {
  22. count++;
  23. for(int i=0; i<25; i++)
  24. {
  25. studentId[i] = input.nextInt(); //THIS STORES THE STUDENTS ID
  26. firstName[i] = input.next(); //THIS STORES THE STUDENTS FIRST NAME
  27. lastName[i] = input.next(); //THIS STORES THE STUDENTS LAST NAME
  28. major[i] = input.next(); //THIS STORES THE STUDENTS MAJOR
  29. gpa[i] = input.nextDouble();//THIS IS WHERE I THINK THE PROBLEM STARTS, FOR SOME REASON IT WON'T STORE THE NEXT READ VARIABLE WHICH IS SUPPOSED TO BE THE GPA
  30. year[i] = input.next();
  31. System.out.printf("%10d %15s %15s %15s %15d %15s %n",studentId[i], firstName[i], lastName[i], major[i], gpa[i], year[i]);
  32. }
  33. }      
  34. }
  35.  
Dec 3 '09 #6
ChipR
1,287 Expert 1GB
Have you given us the actual format of the text file?
Dec 3 '09 #7
RedSon
5,000 Expert 4TB
Do you have a debugger attached to your main method when you run this? Instead of throwing your file not found exception why don't you try catching all exceptions and print out what they are so you have a better idea?

There must be more details around your exception in thread main that will lead you to a better answer.

This probably doesnt matter but does nextDouble return a double or a Double?
Dec 3 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
0
by: s_erez | last post by:
Hi, This is a realy tricky one. I have an ASP.NET application where some pages are reading data from a DB and presenting reports. In order for the user to wait while the page is reading data from...
2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
by: settyv | last post by:
Hi, Below is the Javascript function that am trying to call from asp:Button control. <script language="javascript"> function ValidateDate(fromDate,toDate) { var fromDate=new Date();
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
4
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.