I'm trying to write a program that read a file containing student data in this format -
studntId | Name | Major | GPA | YEAR |
-
---------|----------|----------------------|------|---------|
-
123456 | John Doe | Computer Science | 3.5 | Junior |
-
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 -
studntId | Name | Major | GPA | YEAR |
-
---------|----------|----------------------|-----|---------|
-
789012 | Mary Boe | Computer Information | 3.7 | Senior |
-
123456 | John Doe | Computer Science | 3.5 | Junior |
-
Here is the code: -
package lab13;
-
-
import java.util.*;
-
import java.io.*;
-
public class DataSorting {
-
-
public static void main(String[] args) throws FileNotFoundException {
-
int[]studentId = new int[25];
-
String[]firstName = new String[25];
-
String[]lastName = new String[25];
-
String[]major = new String[25];
-
double[]gpa = new double[25];
-
String[]year = new String[25];
-
-
Scanner input = new Scanner(new File("studentrecords.txt"));
-
int count = 0;
-
while(input.hasNextDouble() && count<25)
-
{
-
count++;
-
for(int i=0; i<25; i++)
-
{
-
studentId[i] = input.nextInt();
-
firstName[i] = input.next();
-
lastName[i] = input.next();
-
major[i] = input.next();
-
gpa[i] = input.nextDouble();
-
year[i] = input.next();
-
//System.out.printf("%10d %15s %15s %15s %15d %15s %n",studentId[i], firstName[i], lastName[i], major[i], gpa[i], year[i]);
-
}
-
}
-
-
bubbleSort(studentId, firstName, lastName, major, gpa, year);
-
System.out.println("after sorting: ");
-
System.out.println(" Exam Scores First Name Last name");
-
for(int i=0; i<studentId.length; i++)
-
System.out.printf("%10d %15s %15s %n", studentId[i], firstName[i], lastName[i]);
-
}
-
-
public static void bubbleSort (int[]dataId, String[]fname, String[]lname, String[]major, double[]gpa, String[]year)
-
{
-
boolean isSorted;
-
for (int R=0; R<dataId.length-1; R++)
-
{
-
do{
-
isSorted = true;
-
for(int i=1; i<dataId.length-R; i++)
-
{
-
if((lname[i].compareTo(lname[i-1])<0))
-
{
-
int temp1 = dataId[i];
-
String temp2 = fname[i];
-
String temp3 = lname[i];
-
String temp4 = major[i];
-
double temp5 = gpa[i];
-
String temp6 = year[i];
-
dataId[i] = dataId[i-1];
-
fname[i] = fname[i-1];
-
lname[i] = lname[i-1];
-
major[i] = major[i-1];
-
gpa[i] = gpa[i-1];
-
year[i] = year[i-1];
-
dataId[i-1] = temp1;
-
fname[i-1] = temp2;
-
lname[i-1] = temp3;
-
major[i-1] = temp4;
-
gpa[i-1] = temp5;
-
year[i-1] = temp6;
-
isSorted = false;
-
}
-
}
-
}
-
while(!isSorted);
-
}
-
}
-
}
-
7 2035
So what's the problem with it?
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
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: - 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 :)
It's very hard for me to read your code.
Have you considered implementing a class like I recommended earlier?
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. - package lab13;
-
import java.util.*;
-
import java.io.*;
-
public class DataSorting {
-
-
public static void main(String[] args) throws FileNotFoundException {
- //DECLARING THE ARRAYS THAT WOULD STORE VALUES READ FROM FILE
-
int[]studentId = new int[25];
-
String[]firstName = new String[25];
-
String[]lastName = new String[25];
-
String[]major = new String[25];
-
double[]gpa = new double[25];
-
String[]year = new String[25];
-
- //INITIALIZING THE SCANNER TO OPEN FILE
-
Scanner input = new Scanner(new File("studentrecords.txt"));
-
int count = 0;
-
- //USING A WHILE LOOP TO STORE THE NEXT READ VARIABLE IN THE FILE
-
while(input.hasNextDouble() && count<25)
-
{
-
count++;
-
for(int i=0; i<25; i++)
-
{
-
studentId[i] = input.nextInt(); //THIS STORES THE STUDENTS ID
-
firstName[i] = input.next(); //THIS STORES THE STUDENTS FIRST NAME
-
lastName[i] = input.next(); //THIS STORES THE STUDENTS LAST NAME
-
major[i] = input.next(); //THIS STORES THE STUDENTS MAJOR
-
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
-
year[i] = input.next();
-
System.out.printf("%10d %15s %15s %15s %15d %15s %n",studentId[i], firstName[i], lastName[i], major[i], gpa[i], year[i]);
-
}
-
}
-
}
-
Have you given us the actual format of the text file?
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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>...
|
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...
|
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...
|
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...
|
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();
|
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...
|
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. ...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |