Can`t this be done with the appropriate RegEX?
Printable View
Can`t this be done with the appropriate RegEX?
Where does regex come into it?
db
I have the code in two separate classes, which are in two different files. Here is the class with the main method:
And the class with all the methods that do all the hard work:Code:public class MainBinaryInsertionSort {
public static void main(String[] args) {
BinaryInsertionSort z = new BinaryInsertionSort();
z.openTextFile();
z.readTextFile();
z.closeTextFile();
}
}
So far I have managed to get all the ID #s into the ArrayList AND I have converted the ArrayList into an array. However, I am getting an error that is supposedly hinting at a mistake with printing out the converted array. This is the error I get:Code:import java.io.*;
import java.util.*;
public class BinaryInsertionSort {
private Scanner x;
ArrayList<String> studentIDS = new ArrayList<String>();
public void openTextFile() {
try {
x = new Scanner(new File("StudentInformation.txt"));
}
catch (Exception e) {
System.out.println("Error! Could not find file!");
}
}
public void readTextFile() {
while(x.hasNext()) {
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
studentIDS.add(d);
System.out.println(d);
}
}
public void closeTextFile() {
x.close();
}
String[] convertedStudentIDS = studentIDS.toArray(new String[studentIDS.size()]);
System.out.println(convertedStudentIDS);
}
I am getting those errors when I try to compile. Please help!Code:.\BinaryInsertionSort.java:38: error: <identifier> expected
System.out.println(convertedStudentIDS);
^
.\BinaryInsertionSort.java:38: error: <identifier> expected
System.out.println(convertedStudentIDS);
^
.\BinaryInsertionSort.java:38: error: cannot find symbol
System.out.println(convertedStudentIDS);
^
symbol: class convertedStudentIDS
location: class BinaryInsertionSort
.\BinaryInsertionSort.java:38: error: cannot find symbol
System.out.println(convertedStudentIDS);
^
symbol: class out
location: class System
4 errors
Press any key to continue . . .
Please look very carefully. Is that line of code in a method? Is it in a constructor? Where exactly is that line of code located?
Okey first of all, i had to change a bit on your .txt file since i couldnt make the double work with "." but only with ",".
My english is not so good, so sorry for mistakes. And am very new at java myself and i am having a similare problem. So here is my try.
Code:/* How the .txt file looks for me to make this work
Mark;25;3,5;0789126
Jessica;21;3,9;0769821
Ashley;19;3,1;0791582
Tom;18;3,7;0698751
Brittney;22;3,2;0581943
Kevin;25;3,4;0968242
Joseph;25;3,1;0181793
John;19;2,8;0908716
Dimitri;23;3,0;0891769
Kimberly;24;3,8;0981913
Name;Age;GPA(use , not .);ID
*/
import java.util.*;
import java.io.*;
/* Okey first compile it javac index.java
* then to run it use java index yourtextfilename.txt
* very important to add your .txt file name after index (or whatever u name ur mainclass)
* and im very new to java so its probably easyer / faster / better ways to make this work
* but this is what i know so i hope it helps you out somehow
*/
class index {
public static void main(String[] args) {
// if we find 1 argument in the line then okey
if(args.length == 1) {
readFile s = new readFile(args[0]);
s.meny();
}
else {
// else big problem
System.out.println("Not correct amount of files!");
}
}
}
// The data class that recives the data later.
// with this class you will be able to do the rest of your homework. you just need to
// add the methods you will need!
class Data {
String name;
int age;
double gpa;
String id;
Data(String name, int age, double gpa, String id) {
this.name = name;
this.age = age;
this.gpa = gpa;
this.id = id;
}
}
/*
* First of all your .txt file doubles cant be like 10.5 it has to be 10,5
* so what i did is use something else to space the data ";" and not ",".
*/
class readFile {
// make a hashmap to store the textdata in
HashMap <String, Data> hmap = new HashMap <String,Data>();
File fil1;
// lets take the argument string "java index ..." and check if the
readFile(String ffil1) {
fil1 = new File(ffil1);
// file exists
if(fil1.exists()) {
try {
Scanner sc = new Scanner(fil1);
// use ";" "enter" and "return" as splitter
sc.useDelimiter("[;\n\r]");
// check the textfile
while(sc.hasNext()) {
String name = sc.next();
int age = sc.nextInt();
double gpa = sc.nextDouble();
// i made the id to a string, so that the hashmap can use it as key
// you could also recive the id as int, and use hmap.put(""+id,...
// but i picked this 1, and the ID will only be used as a key so why not
String id = sc.next();
// put the data we read from the .txtfile into your hashmap.
hmap.put(id, new Data(name, age, gpa, id));
// (key, new Data(variables,variables,variables,variables))
}
sc.close();
}
catch (Exception e) {
System.out.println("Error: "+e.getMessage());
}
}
}
void allName() {
Iterator<Data> it = hmap.values().iterator();
// we use the Iterator to read the valueobjects (correct me if im wrong, new into java)
int a = hmap.size();
// use the hashmap data size as a (to make our array that will sort and print out the ids)
// in our example we used 10 names/lines of data, so a would be 10.
String[] sortid = new String[a];
//the array sortid is made, and its a int big [a]/[10]
int t = 0;
// we make a counter
while (it.hasNext()) {
//make connection and start counting and putting correct id to the array
Data d = (Data) it.next();
sortid[t] = d.id;
t++;
}
java.util.Arrays.sort(sortid);
// we use the sorter.
System.out.println("Name - Age - GPA - ID");
for(int j = 0; j < t; j++) {
//for-loop to print out the data
Data d = (Data) hmap.get(sortid[j]);
System.out.print(d.name+" - ");
System.out.print(d.age+" - ");
System.out.print(d.gpa+" - ");
System.out.println(d.id);
}
}
void meny() {
System.out.println("#### MENY ####");
System.out.println("(1) All name");
System.out.println("(9) Exit");
menyValg();
}
void menyValg() {
Scanner sc = new Scanner(System.in);
int valg = sc.nextInt();
switch (valg) {
case 1 : allName();
meny();
break;
case 10 : break ;
default :
System.out.println("Error: Pick a number 1-x // 9 = Exit!");
meny();
}
sc.close();
}
}
I got the compile-time error to disappear by altering the code a little. This is what I have now:
And:Code:public class MainBinaryInsertionSort {
public static void main(String[] args) {
BinaryInsertionSort z = new BinaryInsertionSort();
z.openTextFile();
z.readTextFile();
z.closeTextFile();
}
}
When I run it, however, I get gibberish:Code:import java.io.*;
import java.util.*;
public class BinaryInsertionSort {
private Scanner x;
public void openTextFile() {
try {
x = new Scanner(new File("StudentInformation.txt"));
}
catch (Exception e) {
System.out.println("Error! Could not find file!");
}
}
public void readTextFile() {
ArrayList<String> studentIDS = new ArrayList<String>();
while(x.hasNext()) {
String a = x.next();
String b = x.next();
String c = x.next();
String d = x.next();
studentIDS.add(d);
System.out.println(d);
String[] convertedStudentIDS = studentIDS.toArray(new String[studentIDS.size()]);
System.out.println(convertedStudentIDS);
}
}
public void closeTextFile() {
x.close();
}
}
I'm guessing the program fails to successfully convert the ArrayList to an array. Help?Code:0789126
[Ljava.lang.String;@6d172f8f
0769821
[Ljava.lang.String;@d338d3d
0791582
[Ljava.lang.String;@337da690
0698751
[Ljava.lang.String;@7e425258
0581943
[Ljava.lang.String;@342c502a
0968242
[Ljava.lang.String;@49431028
0181793
[Ljava.lang.String;@51d9d7ab
0908716
[Ljava.lang.String;@34de4588
0891769
[Ljava.lang.String;@6ca6fed5
0981913
[Ljava.lang.String;@47a489ad
Press any key to continue . . .
You're seeing the default toString() representation of an Array of String. To see the contents of the array, you could always use the Arrays class toString(...) method:
Code:System.out.println(java.util.Arrays.toString(convertedStudentIDS));
Awesome! It works perfectly! Thanks everyone for all your help! :)