Results 1 to 5 of 5

Thread: Java code question     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Java code question

    I have an assignment where i have to read int's in from a txt file and put them into categories and then output them. Getting a lump of errors can anyone tell me what im doing wrong? Also i need to ignore the first number in the file, how would i do this? Am i going about entirely the wrong way?

    Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class ClassGrades{
    
    	public static void main (int[] args){
    		int one = 0;
    		int two = 0;
    		int three = 0;
    		int four = 0;
    		int five = 0;
    		int six = 0;
    		int seven = 0;
    		int eight = 0;
    	
    		File file = new File("grades.txt");
    		try{
    			Scanner scanner = new Scanner(file);
    			while(scanner.nextInt()) {
    				if(scanner.nextInt < 25) { one = one + 1};
    				else if(scanner.nextInt() > 24 && < 50) { two = two + 1};
    				else if(scanner.nextInt() > 49 && < 75) { three = three + 1};
    				else if(scanner.nextInt() > 74 && < 100) { four = four + 1};
    				else if(scanner.nextInt() > 99 && < 125) { five = five + 1};
    				else if(scanner.nextInt() > 124 && < 150) { six = six + 1};
    				else if(scanner.nextInt() > 149 && < 175) { seven = seven + 1};
    				else { eight = eight + 1};
    				System.out.println("[0 - 24]: "one"");
    				System.out.println("[25 - 49]: "two"");
    				System.out.println("[50 - 74]: "three"");
    				System.out.println("[75 - 99]: "four"");
    				System.out.println("[100 - 124]: "five"");
    				System.out.println("[125 - 149]: "six"");
    				System.out.println("[150 - 174]: "seven"");
    				System.out.println("[175 - 199]: "eight"");
    			}
    			scanner.close();
    		}
    		catch(FileNotFoundException e) {}
    	}
    }

  2. #2
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Actually doesn't matter tech just said we gotta do it in c++ >< been 3 years scene i've had c++

    i found basics of opening/closing txt's in c++

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    void main () {
    
    	int one = 0;
    	int two = 0;
    	int three = 0;
    	int four = 0;
    	int five = 0;
    	int six = 0;
    	int seven = 0;
    	int eight = 0;
    	
        int INT;
    	ifstream infile;
    	infile.open ("grades.txt");
            while(!infile.eof) // To get you all the lines.
            {
    	        getline(infile,INT); // Saves the line in STRING.
    	        cout<<INT; // Prints our STRING.
            }
    	infile.close();
    	system ("pause");
    }
    No clue how to get them one by one though; i know i can just do a if else statement to put them like i was in the java code.

  3. #3
    Nidhogg
    Join Date
    Apr 2006
    Posts
    3,562
    BG Level
    7

    Quote Originally Posted by Skie View Post
    a lump of errors
    pasting these would be helpful, when you decide to ask again in c++

  4. #4
    Nidhogg
    Join Date
    Apr 2006
    Posts
    3,562
    BG Level
    7

    working perl example

    Code:
    use POSIX qw(ceil floor);
    my $g =();
    if (open(my $fh, "grades.txt") ) {
       while ( <$fh> ) {
          my $key = ceil($_/25);	   
          $g->{$key}++;
       }
    } else { die $!; }
    
    foreach my $slot (keys %$g) {
       print "$slot: $g->{$slot}\n";
    }

  5. #5
    Banned.

    Join Date
    Jul 2005
    Posts
    5,821
    BG Level
    8
    FFXI Server
    Sylph
    WoW Realm
    Arthas

    Crappy, but easy to understand solution:
    Add an arraylist/list for each of your categories.

    Putting things into categories:

    (Pseudo code)

    If you want to get fancy, write a struct like this:
    Code:
    Struct grades{
                  List<Int> under10;
                  List<Int> under50;
                  List<Int> under100;
                  List<Int> perfect;
    }
    Write a function like this:
    Code:
    private void lolCoding(int a){
                  if      ((a >= 1) && (a <= 9)) grades.under10.Add(a);
                  else if ((a >= 10) && (a <= 49)) grades.under50.Add(a);
                  else if (a >= 50 && (a <= 99)) grades.under100.Add(a);
                  else grades.perfect.Add(a);
    }
    This will give you everything in an organized fashion. If you just want to output the values, just get rid of the Struct and make the function output your stuff.

Similar Threads

  1. Web Coding Question
    By SaviorSelf in forum Tech
    Replies: 12
    Last Post: 2011-04-17, 02:02
  2. Java code
    By Skie in forum Tech
    Replies: 14
    Last Post: 2011-02-03, 11:02
  3. Java Test question review help.
    By Skie in forum Tech
    Replies: 6
    Last Post: 2010-11-21, 20:32
  4. Java Code help
    By Skie in forum Tech
    Replies: 11
    Last Post: 2010-10-11, 14:37
  5. more java code
    By Skie in forum Tech
    Replies: 0
    Last Post: 2010-09-24, 20:14
  6. noob coding question
    By Effrenat in forum Tech
    Replies: 9
    Last Post: 2007-02-06, 19:16