How Would I Separate Two Separate tokens into Two Separate Arrays?

• (2 Pages)
• 1
• 2

21 Replies - 1300 Views - Last Post: 13 January 2013 - 08:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307106&amp;s=73072e6e358b50bd6041a169abd36bc6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 2pac

Reputation: 0
• Posts: 13
• Joined: 13-January 13

How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 05:55 PM

Hi all, I just joined this site. It looks like a nice place to learn new languages />

I am working on a java program as I am trying to learn Java Programming by myself.

I am working on a GPA Calculator program where you read a file that has information such as letter grades and grade numbers.
For example the first line in the text file would be like: A+ 9
Second line: B 6

I need help on separating the letter grades and the grade numbers into separate arrays and then finding the grade point average from the given information from the file.

Here is what I have so far:
```import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class GPACalculator {
public static Scanner console = new Scanner(System.in);
public static int [] gradePointValue = {9, 8, 7, 6, 5, 4, 3, 2, 1};
public static String[] letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"};
public static void main(String args[]) throws IOException {
System.out.print("What is the file name? ");
try {
FileInputStream file = new FileInputStream(console.nextLine());
String line;
System.out.println(line);
}
}
catch(IOException e) {
System.out.println(e);
}
String[] letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"};
int [] gradePointValue = {9, 8, 7, 6, 5, 4, 3, 2, 1};

}

}
}

}

```

I am still learning Java, so any help would be greatly appreciated. Thank you guys so much!

Is This A Good Question/Topic? 0

Replies To: How Would I Separate Two Separate tokens into Two Separate Arrays?

#2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 05:59 PM

String[] token = line.split(" ");

token[0] will contain "A+" or "B"
tokjen[1] will contain "9" or "6"

#3 natecat

Reputation: 57
• Posts: 233
• Joined: 19-December 11

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:00 PM

This is quite easy, simply use stringVariable.split(" "); which will give you an array with the first element being the letter grade and the second element being the number grade.
EDIT: Ninja'ed

This post has been edited by natecat: 13 January 2013 - 06:00 PM

#4 2pac

Reputation: 0
• Posts: 13
• Joined: 13-January 13

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:03 PM

Where does String[] token = line.split(" "); go?

Does it go inside the try statement? Thanks for the quick replies guys, it really means alot.

#5 2pac

Reputation: 0
• Posts: 13
• Joined: 13-January 13

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:25 PM

I also have to use a certain grade value which is 1.5 and that is multiplied by every grade number

For example: A+ 9
(The 9 would by multiplied by 1.5 to find out the grade point value.)

And then all the 1.5 values are added together. Then the total grade point value should be divided by the total grade sum (all the 1.5 values added together )

#6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:28 PM

When you have access to the line variable and when it changes
``` while((line = br.readLine())!= null) {
String[] token = line,split(" ");

```

#7 2pac

Reputation: 0
• Posts: 13
• Joined: 13-January 13

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:31 PM

My program runs but still shows the same output as the text file has.

```import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class GPACalculator {
public static Scanner console = new Scanner(System.in);
public static int [] gradePointValue = {9, 8, 7, 6, 5, 4, 3, 2, 1};
public static String[] letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"};
public static void main(String args[]) throws IOException {
System.out.print("What is the file name? ");
try {
FileInputStream file = new FileInputStream(console.nextLine());
String line;
String[] token = line.split(" ");
System.out.println(line);
}
}
catch(IOException e) {
System.out.println(e);
}
String[] letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"};
int [] gradePointValue = {9, 8, 7, 6, 5, 4, 3, 2, 1};

}

}
}

}

```

#8 natecat

Reputation: 57
• Posts: 233
• Joined: 19-December 11

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:37 PM

The String[] token is the thing that has the grade and the number.

#9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:39 PM

and you do nothing with your array token[]

#10 2pac

Reputation: 0
• Posts: 13
• Joined: 13-January 13

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:40 PM

I'm completely lost now. I'm sorry, I just don't understand what to do after that. I'm trying to find the GPA so I have to find the total gradeWeight (all the 1.5 values added together for every line in the text file).

I also have to find the gradePointValue sum which is (1.5*9) + (1.5*6)..etc

#11 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:48 PM

Quote

For example the first line in the text file would be like: A+ 9
Second line: B 6

Now you are telling us that you are reading floating numbers from the file ?!?!?!?!?!

#12 2pac

Reputation: 0
• Posts: 13
• Joined: 13-January 13

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 06:52 PM

No.

What I mean is, in the text file there are numbers and letters.

Example:
A+ 9
B 6
B+ 7
A 8

etc etc

I need those letter grades and the number grades to be separated into the separate arrays I made.

Then I need to figure out the gpa by multiplying 1.5 to every grade number in the text file and adding all those new grade number values together. Also have to add all the 1.5's together (based on how many lines there are. If there are 5 lines then it's 5*1.5, if there are 10 lines then it's 10*1.5.. etc)

Then to find gpa, it's just the total grade values (after the 1.5 value is multipled to the numbers and all those new numbers are added together) / (all the 1.5 values added together).

Sorry for all the confusion!

The numbers in the text file are ALL ints, no doubles or floating numbers.

#13 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 07:04 PM

SO you need something like this

```   int nbInUse = 0;

Scanner scan = new Scanned(new File(file));
while(scan.hasNextLine()) {
String line = scan.nextLine();
String[] token = lien,split(" ");
nbInUse++;
}

```

#14 2pac

Reputation: 0
• Posts: 13
• Joined: 13-January 13

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 07:10 PM

I have this now, and I'm getting errors.

```import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class GPACalculator {
public static Scanner console = new Scanner(System.in);
public static int [] gradePointValue = {9, 8, 7, 6, 5, 4, 3, 2, 1};
public static String[] letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"};
public static void main(String args[]) throws IOException {
System.out.print("What is the file name? ");
try {
FileInputStream file = new FileInputStream(console.nextLine());
String line;
String[] token = line.split(" ");
System.out.println(line);
}
int nbInUse = 0;

Scanner console = new Scanner(new file(file));
while(console.hasNextLine()) {
String line = console.nextLine();
String[] token = line.split(" ");
nbInUse++;
}
}
catch(IOException e) {
System.out.println(e);
}
String[] letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"};
int [] gradePointValue = {9, 8, 7, 6, 5, 4, 3, 2, 1};

}

}
}

}

```

#15 natecat

Reputation: 57
• Posts: 233
• Joined: 19-December 11

Re: How Would I Separate Two Separate tokens into Two Separate Arrays?

Posted 13 January 2013 - 07:10 PM

pbl, on 14 January 2013 - 02:04 AM, said:

SO you need something like this

```   int nbInUse = 0;

Scanner scan = new Scanned(new File(file));
while(scan.hasNextLine()) {
String line = scan.nextLine();
String[] token = lien,split(" ");