Just Some Bread
Just Some Bread
CC#
Created by Just Some Bread on 11/29/2023 in #help
Object reference set to an instance of an object.
Hi, just completing an assignment for uni that reads marks from a file and calculate the exam percentage and grade then outputs it to the console with the number of students who had that grade in the file. I came across a NullReferenceException and I'm not sure how to handle it.
using System;
using System.IO;

namespace Program{
class Program{
static void Main(string[] args){
string currFile = "5StudentsOneEachClassification.mark";
string[][] studentInfo = loadDataFromFile(currFile);
outputClassification(studentInfo);
}
static string[][] loadDataFromFile(string currFile){
string data = File.ReadAllText(System.IO.Directory.GetCurrentDirectory()+"/TestInputFiles/"+currFile);
string[] splitData = data.Split(new char[]{'\n'});
string[][] studentInfo = new string[splitData.Length-1][];
double capstoneTotal = 0, examTotal = 0, challTotal = 0, total = 0;

for(int i = 0; i<splitData.Length-1;i++){
string[] temp = splitData[i].Split(new char[]{':', '[', ']', ' ', ','});
for(int x = 0; x<temp.Length-1; x++){

if(temp[i] == "ID"){
studentInfo[i][0] = temp[i];
}
else if(temp[i] == "Challenges"){
challTotal = Convert.ToInt32(temp[i+2]) + Convert.ToInt32(temp[i+3]) + Convert.ToInt32(temp[i+4]) + Convert.ToInt32(temp[i+5]) + Convert.ToInt32(temp[i+6]) + Convert.ToInt32(temp[i+7]) + Convert.ToInt32(temp[i+8]) + Convert.ToInt32(temp[i+9]) + Convert.ToInt32(temp[i+10]) + Convert.ToInt32(temp[i+11]);
}
else if(temp[i] == "Exam"){
examTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "Capstone"){
capstoneTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "FirstName"){
studentInfo[i][0] = temp[i+1];
}
else if(temp[i] == "LastName"){
studentInfo[i][1] = temp[i+1];
}
}
challTotal = Math.Round((challTotal/35)*100, 2);
examTotal = Math.Round((examTotal/7)*100, 2);
capstoneTotal = Math.Round((capstoneTotal/100)*100, 2);
total = (challTotal*50/100) + (examTotal*25/100) + (capstoneTotal*25/100);

studentInfo[i][2] = Convert.ToString(total);
}
return studentInfo;
}

static void outputClassification(string[][] studentInfo){
int first = 0, two1 = 0, two2 = 0, third = 0, fail = 0;
for(int i = 0; i<studentInfo.Length-1; i++){
if(Convert.ToInt32(studentInfo[i][2]) >= 70){
first++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 60){
two1++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 50){
two2++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 40){
third++;
}
else{
fail++;
}
}
Console.WriteLine($"{first} - First");
Console.WriteLine($"{two1} - 2:1");
Console.WriteLine($"{first} - First");
Console.WriteLine($"{third} - Third");
Console.WriteLine($"{fail} - Fail");
}
}
}
using System;
using System.IO;

namespace Program{
class Program{
static void Main(string[] args){
string currFile = "5StudentsOneEachClassification.mark";
string[][] studentInfo = loadDataFromFile(currFile);
outputClassification(studentInfo);
}
static string[][] loadDataFromFile(string currFile){
string data = File.ReadAllText(System.IO.Directory.GetCurrentDirectory()+"/TestInputFiles/"+currFile);
string[] splitData = data.Split(new char[]{'\n'});
string[][] studentInfo = new string[splitData.Length-1][];
double capstoneTotal = 0, examTotal = 0, challTotal = 0, total = 0;

for(int i = 0; i<splitData.Length-1;i++){
string[] temp = splitData[i].Split(new char[]{':', '[', ']', ' ', ','});
for(int x = 0; x<temp.Length-1; x++){

if(temp[i] == "ID"){
studentInfo[i][0] = temp[i];
}
else if(temp[i] == "Challenges"){
challTotal = Convert.ToInt32(temp[i+2]) + Convert.ToInt32(temp[i+3]) + Convert.ToInt32(temp[i+4]) + Convert.ToInt32(temp[i+5]) + Convert.ToInt32(temp[i+6]) + Convert.ToInt32(temp[i+7]) + Convert.ToInt32(temp[i+8]) + Convert.ToInt32(temp[i+9]) + Convert.ToInt32(temp[i+10]) + Convert.ToInt32(temp[i+11]);
}
else if(temp[i] == "Exam"){
examTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "Capstone"){
capstoneTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "FirstName"){
studentInfo[i][0] = temp[i+1];
}
else if(temp[i] == "LastName"){
studentInfo[i][1] = temp[i+1];
}
}
challTotal = Math.Round((challTotal/35)*100, 2);
examTotal = Math.Round((examTotal/7)*100, 2);
capstoneTotal = Math.Round((capstoneTotal/100)*100, 2);
total = (challTotal*50/100) + (examTotal*25/100) + (capstoneTotal*25/100);

studentInfo[i][2] = Convert.ToString(total);
}
return studentInfo;
}

static void outputClassification(string[][] studentInfo){
int first = 0, two1 = 0, two2 = 0, third = 0, fail = 0;
for(int i = 0; i<studentInfo.Length-1; i++){
if(Convert.ToInt32(studentInfo[i][2]) >= 70){
first++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 60){
two1++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 50){
two2++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 40){
third++;
}
else{
fail++;
}
}
Console.WriteLine($"{first} - First");
Console.WriteLine($"{two1} - 2:1");
Console.WriteLine($"{first} - First");
Console.WriteLine($"{third} - Third");
Console.WriteLine($"{fail} - Fail");
}
}
}
48 replies
CC#
Created by Just Some Bread on 10/30/2023 in #help
❔ Most of the code in class throws error when working before.
Hello, I'm making a simple minesweeper game in the console using OOP. I created a class for the grid to be able to manage the grid for the minesweeper. I was building the code without any issues before, i added a new method and then when I try to build it now the code doesn't seem to work at all and throws an error on almost every line of code.
using System;

namespace Minesweeper{
class Minesweeper{
static void Main(string[] args){
Grid grid = new Grid();
string __playerName = "Alex";
Player player = new Player() {playerName = __playerName };
grid.bombCreation("Hard");

grid.printSeenGrid();
}
}
class Grid{
bool[,] seenGrid = new bool[20,20]; //Contains grid of what squares can be seen
char[,] bombGrid = new char[20,20];//Contains grid where the bombs are
public void printBombGrid{
for(int row = 0; row <= bombGrid.GetLength(0)-1; row++){
for(int col = 0; col<=bombGrid.GetLength(1)-1; col++){
if (bombGrid[row, col] == 'B'){
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" O");
}else{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" O");
}
Console.ForegroundColor = ConsoleColor.White;
}
Console.WriteLine();
}
}
public void setSeenGrid(bool set, int X, int Y){
seenGrid[X, Y] = set;
}

public void printSeenGrid(int[] playerSelection){
for(int row = 0; row<=seenGrid.GetLength(0)-1; row++){
for(int col = 0; col<=seenGrid.GetLength(1)-1; col++){
if(seenGrid[row, col] == true) {
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" O");
}
else if(seenGrid[row, col] == true && bombGrid[row, col] == 'B'){
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" O");
}
else{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" O");
}
}
Console.WriteLine();
}
}

public void bombCreation(string difficulty){
if(difficulty=="Hard"){
for(int i = 100; i>=0; i--){
Random rnd = new Random();
int bombX = rnd.Next(0, bombGrid.GetLength(0));
int bombY = rnd.Next(0, bombGrid.GetLength(1));
bombGrid[bombX, bombY] = 'B';
}
}
}
}
class Player : Grid{
public string playerName { get; set;}
}
}
using System;

namespace Minesweeper{
class Minesweeper{
static void Main(string[] args){
Grid grid = new Grid();
string __playerName = "Alex";
Player player = new Player() {playerName = __playerName };
grid.bombCreation("Hard");

grid.printSeenGrid();
}
}
class Grid{
bool[,] seenGrid = new bool[20,20]; //Contains grid of what squares can be seen
char[,] bombGrid = new char[20,20];//Contains grid where the bombs are
public void printBombGrid{
for(int row = 0; row <= bombGrid.GetLength(0)-1; row++){
for(int col = 0; col<=bombGrid.GetLength(1)-1; col++){
if (bombGrid[row, col] == 'B'){
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" O");
}else{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" O");
}
Console.ForegroundColor = ConsoleColor.White;
}
Console.WriteLine();
}
}
public void setSeenGrid(bool set, int X, int Y){
seenGrid[X, Y] = set;
}

public void printSeenGrid(int[] playerSelection){
for(int row = 0; row<=seenGrid.GetLength(0)-1; row++){
for(int col = 0; col<=seenGrid.GetLength(1)-1; col++){
if(seenGrid[row, col] == true) {
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" O");
}
else if(seenGrid[row, col] == true && bombGrid[row, col] == 'B'){
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" O");
}
else{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" O");
}
}
Console.WriteLine();
}
}

public void bombCreation(string difficulty){
if(difficulty=="Hard"){
for(int i = 100; i>=0; i--){
Random rnd = new Random();
int bombX = rnd.Next(0, bombGrid.GetLength(0));
int bombY = rnd.Next(0, bombGrid.GetLength(1));
bombGrid[bombX, bombY] = 'B';
}
}
}
}
class Player : Grid{
public string playerName { get; set;}
}
}
10 replies
CC#
Created by Just Some Bread on 10/28/2023 in #help
❔ For loop is being skipped after a do while loop.
No description
49 replies