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 PM Rate Topic: -----

#1 2pac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.InputStreamReader;
import java.io.BufferedReader;
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());
		BufferedReader br = new BufferedReader(new InputStreamReader(file));
		String line;
		while((line = br.readLine())!= null) {
			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};
		
	}

	public static double getGrade(String[]letterGrades) {
		double grade = 0;
		if(letterGrades.equals("A+")) {
			grade = 9;
		}else if(letterGrades.equals("A")) {
			grade = 8;
		}else if(letterGrades.equals("A-")) {
			grade = 7;
		}else if(letterGrades.equals("B+")) {
			grade = 6;
		}else if(letterGrades.equals("B")) {
			grade = 5;
		}else if(letterGrades.equals("B-")) {
			grade = 4;
		}else if(letterGrades.equals("C+")) {
			grade = 3;
		}else if(letterGrades.equals("C")) {
			grade = 2;
		}else if(letterGrades.equals("D")) {
			grade = 1;
		} else { letterGrades.equals("F"); 
			grade = 0;
		}
		return grade;
	}

}



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  Icon User is offline

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

Reputation: 8378
  • View blog
  • 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"
Was This Post Helpful? 1
  • +
  • -

#3 natecat  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 2pac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 2pac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 )
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8378
  • View blog
  • 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(" ");


Was This Post Helpful? 0
  • +
  • -

#7 2pac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.InputStreamReader;
import java.io.BufferedReader;
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());
		BufferedReader br = new BufferedReader(new InputStreamReader(file));
		String line;
		while((line = br.readLine())!= null) {
			String[] token = line.split(" ");
			System.out.println(line);
			}
		}
		catch(IOException e) {
			System.out.println(e);	
		}
		double gradeWeight = 1.5;
		//double gradeWeightSum = gradeWeight;
		String[] letterGrades = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"};
		int [] gradePointValue = {9, 8, 7, 6, 5, 4, 3, 2, 1};
		
	}

	public static double getGrade(String[]letterGrades) {
		double grade = 0;
		if(letterGrades.equals("A+")) {
			grade = 9;
		}else if(letterGrades.equals("A")) {
			grade = 8;
		}else if(letterGrades.equals("A-")) {
			grade = 7;
		}else if(letterGrades.equals("B+")) {
			grade = 6;
		}else if(letterGrades.equals("B")) {
			grade = 5;
		}else if(letterGrades.equals("B-")) {
			grade = 4;
		}else if(letterGrades.equals("C+")) {
			grade = 3;
		}else if(letterGrades.equals("C")) {
			grade = 2;
		}else if(letterGrades.equals("D")) {
			grade = 1;
		} else { letterGrades.equals("F"); 
			grade = 0;
		}
		return grade;
	}

}


Was This Post Helpful? 0
  • +
  • -

#8 natecat  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8378
  • View blog
  • 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[]
Was This Post Helpful? 0
  • +
  • -

#10 2pac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8378
  • View blog
  • 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

Based on your first post

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 ?!?!?!?!?!
Was This Post Helpful? 0
  • +
  • -

#12 2pac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8378
  • View blog
  • 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;
   String[] letterGrades = new String[100];
   int[] gradeValues = new int[100];


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


Was This Post Helpful? 1
  • +
  • -

#14 2pac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.InputStreamReader;
import java.io.BufferedReader;
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());
		BufferedReader br = new BufferedReader(new InputStreamReader(file));
		String line;
		while((line = br.readLine())!= null) {
			String[] token = line.split(" ");
			System.out.println(line);
			}
			int nbInUse = 0;
			String[] letterGrades = new String[100];
			int[] gradePointValue = new int[100];
			
			Scanner console = new Scanner(new file(file));
			while(console.hasNextLine()) {
				String line = console.nextLine();
				String[] token = line.split(" ");
				letterGrades[nbInUse] = token[0];
				gradePointValue[nbInUse] = Integer.parseInt(token[1]);
				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};
		
	}

	public static double getGrade(String[]letterGrades) {
		double grade = 0;
		if(letterGrades.equals("A+")) {
			grade = 9;
		}else if(letterGrades.equals("A")) {
			grade = 8;
		}else if(letterGrades.equals("A-")) {
			grade = 7;
		}else if(letterGrades.equals("B+")) {
			grade = 6;
		}else if(letterGrades.equals("B")) {
			grade = 5;
		}else if(letterGrades.equals("B-")) {
			grade = 4;
		}else if(letterGrades.equals("C+")) {
			grade = 3;
		}else if(letterGrades.equals("C")) {
			grade = 2;
		}else if(letterGrades.equals("D")) {
			grade = 1;
		} else { letterGrades.equals("F"); 
			grade = 0;
		}
		return grade;
	}

}


Was This Post Helpful? 0
  • +
  • -

#15 natecat  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • 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

View Postpbl, on 14 January 2013 - 02:04 AM, said:

SO you need something like this

   int nbInUse = 0;
   String[] letterGrades = new String[100];
   int[] gradeValues = new int[100];


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


Just wanted to say it would be better to make a data type instead of the two arrays. Here is a good tutorial on that: http://www.dreaminco...arallel-arrays/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2