HacruLeian

thank you very much everyone! i figure out how to obtain this output:

WS
cc
dC

i reset the Initial = "" after System.out.println (Initial).

thank you very much for helping me to solve this problem :D

HacruLeian

BufferedReader reader = null;
    String Fullname= "", Initials = "" ;
    String[] name;  
    try {

        File csvfile = new File ("sample3.csv");
        reader = new BufferedReader(new FileReader(csvfile));
        String line = "";   
        while ((line = reader.readLine()) != null) {
             name = line.split(" ");    
            //Fullname = name[i];
            for(int i = 0; i < name.length(); ++i){ 

                 Initials += name[i].charAt(0);
            }
            System.out.println(Initials);
        }

as for this one, i don't get why it gave me this error:

cannot find symbol: for(int i = 0; i < name.length(); ++i){
symbol:   method length()
location: variable name of type String[]
1 error

then i declared String[] name ="";
it gave empty output. is it wrong with this code?

HacruLeian

while (filescan.hasNext())
            {
                String line = "";
                String[] name = line.split(" ");
                String Name = filescan.next(); 

                    for (int i =0; i <Name.length(); i++)
                    {       
                        String[] name2 = line.split(" ");
                        ShortenedName +=  Name.charAt(0);   
                    }

            }
            System.out.println(ShortenedName);

uhm, if i do this then i get: WWWWWWWSSSSSSSSSSSccccccccccccccdddddCCCCCCCCCCC

and if it's this:

while (filescan.hasNext())
            {
                String line = "";
                String[] name = line.split(" ");
                String Name = filescan.next(); 

                    for (int i =0; i <ShortenedName.length(); i++)
                    {       
                        String[] name2 = line.split(" ");

                    }
                ShortenedName +=  Name.charAt(0);   
            }
            System.out.println(ShortenedName);

then the output is : WSccdC

is it the way i do is wrong? i still couldn't get the output that i want

thank you

HacruLeian

this is is the newest way i tried:

String ShortenedName = "";
        Scanner filescan = null;
        char NameAtFirst;

            filescan= new Scanner (new File("sample2.csv"));

            while (filescan.hasNext())
            {
                String line = "";
                String[] name = line.split(" ");
                String Name = filescan.next(); 
                ShortenedName +=  Name.charAt(0);   
                System.out.println(ShortenedName);

but the output that i got is:
W
WS
WSc
WScc
WSccd
WSccdC

is there anyway to fix it so that it become:

WS
cc
dC

thank you

HacruLeian

if split and get only the first char of the word is it like this?

while ((line = reader.readLine()) != null) {
            String[] name = line.split(" ");    
            //Fullname = name[0];
            for(int i = 0; i < name.length(); ++i){ 
                     Initials += Fullname.charAt(0);
            }

HacruLeian

i've never try coding using the current java before, but i've seen it once when i was searching on the google for this problem. may i know what does this ":" notation mean?

HacruLeian

i've just eliminate some of the unused variable and some stuff hopefully it look more presentable. about the split with the default separator of space i used it this way String[] name = line.split("\n"); is it wrong?

public static void main(String[] args) {

    tryname obj = new tryname();
    obj.run();

  }

  public void run() {

    BufferedReader reader = null;
    String Fullname= "", Initials = "" ;    
    try {

        File csvfile = new File ("Sample2.csv");
        reader = new BufferedReader(new FileReader(csvfile));
        String line = "";   
        while ((line = reader.readLine()) != null) {
            String[] name = line.split("\n");   
            Fullname = name[0];
            for(int i = 0; i < Fullname.length(); ++i){ 
                if(Fullname.charAt(i) == ' ')
                {
                     Initials += Fullname.charAt(i+1);
                }
            }
            System.out.println(Initials);
        }

HacruLeian

sorry,i forgot to mention about that.it compile and there's no exception error. the output is wrong. it gave this :

S
Sc
ScC

instead of the one i mentioned above. i thought of looping it char by char to check whether the next char is a '' or not. if it's a ' ' then the next char will be selected and assigned to a variable and the next char selected will be concatenated with the char selected earlier. that's is what i thought. i'm not very good in programming so i refer to internet when i'm doing this. please guide me or share with me your thought for this problem. thank you.

HacruLeian Newbie Poster

i have a .csv file contain some name as below:

William Shakespeare
charlie chaplin
david Copperfield

and i'm supposed to write a java program that will turn the input above into the output below:

WS
cc
dC

but i still couldn't get the output that i want, i wanna ask is there anything wrong with my code below?

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.io.*;

public class tryname {

  public static void main(String[] args) {

    tryname obj = new tryname();
    obj.run();

  }

  public void run() {

    BufferedReader reader = null;
    char tempname;
    //int point =0;
    String Fullname= "", Initials = "" ;
    String cvsSplitBy = "\n";

    try {

        File csvfile = new File ("Sample2.csv");
        reader = new BufferedReader(new FileReader(csvfile));
        String line = "";   
        //String s;
        while ((line = reader.readLine()) != null) {

            // use comma as separator
            String[] name = line.split(cvsSplitBy); 
            Fullname = name[0];
            for(int i = 0; i < Fullname.length(); i++){ 
                //Initials = name [i];
                tempname= Fullname.charAt(i);
                if(tempname == ' ')
                {
                     //Fullname += name.charAt(i);
                     Initials += Fullname.charAt(i+1);
                     //point= i+1;
                }
            }
            System.out.println(Initials);

        }

HacruLeian

for the vector in the superclass i do something like this:
Inline Code Example Here

//superclass
class Person {
protected:
    string name;
    string id;
    vector <Person> info;
public:
    Person (string name, string id): name(name),id(id) {}// constructor initializer list

    string getName() {return name;}
    string getID() {return id;}
};

//subclass -> derived from superclass
class Student : virtual public Person {
protected:
    string Student_year;
public:
    Student (string name, string id, string Student_year): Person(name,id),Student_year(Student_year) {}

    string getStud_year() {return Student_year;}

then in the int main() when i want to create a student, do i do something like this in the int main?

 case '1' : cout<<"insert name"<<endl;
                        info.push_back(name);
                        cout<<"insert ID:"<<endl;
                        info.push_back(ID);
                        cout<<"insert year: 1. gamma , 2. delta"<<endl;
                        Student::info.push_back(Student_year);
                        break;

HacruLeian

what is it mean by the error called: function definition does not declare parameters, multiple initilization given for the base class and no matching function call to the superclass?

HacruLeian

may i know why i can't access the private member data even though i've declared public..

class Person {
private:
    string name;
    string id;
public:
    Person (string name, string id): name(name),id(id) {}
    string getName() {return name;}
    string getID() {return id;}
};

class Student : public Person {
private:
    string Student_year;
public:
    Student (string name, string id, string Student_year): Person(name), Person(id),Student_year(Student_year) {}
    string getStudent_year {return Student_year;}
    void print();

};

class Lecturer :  public Person {
private:
    string Room_number;
public:
    Lecturer (string name, string id, string Room_number): Person(name),Person(id), Room_number(Room_number) {}
    string getRoom_number {return Room_number;}
    void print();
};

class Project :  public Student,  public Lecturer {
public:
    Project (string name, string id) : Person(name), Person(id), Student( Student_year), Lecturer(Room_number) {}
    void print();
};

how do i eliminate that problem?

HacruLeian

Problem Statement
You are hired to develop a Final Year Project (FYP) management system for a university. The business rules are listed below:
•        A student may register a project with a supervisor.
•        A project may have up to 2 students and must have 1 supervisor only.

The system must be able to perform the following functions:
1.      Create lecturers. [0.5m]
2.      Delete lecturers. [1m]
3.      Create students. [0.5m]
4.      Delete students. [1m]
5.      Create project. [0.5m]
6.      Delete project. [1m]
7.      Register a student to a supervisor for a project. [0.5m]
8.      Register project according to the quota. [1m]
9.      Unregister a student from a project. [1m]
10.  List all lecturers (all attributes, student, and project must be shown) [1m]
11.  List all students (all attributes, project, and supervisor must be shown) [1m]
12.  List all projects (all attributes, student, and supervisor must be shown) [1m]
13.  Read data from file and save data to file. [1m] (You may provide some initial data to facilitate testing during interview.)

HacruLeian

yeah, but i have 3 class to create" lecturer, studenta and project.

how do i link them together? because i seen something like this:

class Player {
  int id;
  string name;
  string team;
 public:
  Player (int id, string name) : id(id), name(name), team("-") { }
  void print() const { cout << id << ", " << name << ", " << team << endl; }
  void setTeam (string team) { this->team = team; } 
};

class Team {
  string name;
  vector <Player> players;
 public:
  Team (string name) : name(name) { }
  void add (Player& player) {
    player.setTeam (name);
    players.push_back (player);
  }

and i have this:

class Project {
private: 
    string lec_name, stu_name, stu_id , project_name, Pro_startdate, Pro_duedate;

public:
    Project() {} // default constructor
    Project(string lec_name= "unknown", stu_name="unknown", stu_id="0" , project_name="no title",Pro_startdate="Null", Pro_duedate= "Null") {}; // overload constructor
    string getLecName(){return lec_name;} // get from class lecturer
    string getStuName(){return stu_name;}//get from student class
    string getStuID(){return stu_id;}//get from student class
    string getProName(){return project_name;}
    string getProStaDate(){return Pro_startdate;}
    string getProDueDate(){return Pro_duedate;}

}

class supervisor {
private:
    string lec_name, lec_ID, stu_name, stu_id, project_name; 

public: 
    supervisor() {} // default constructor
    supervisor(string lec_name= "unknown", lec_ID= "0", stu_name="unknown", stu_id="0" , project_name="no title") {}; // overload constructor
    string getLecName(){return lec_name;} 
    string getLecID(){return lec_ID;}
    string getStuName(){return stu_name;}//get from student class
    string getStuID(){return stu_id;}//get from student class
    string getProName(){return project_name;}//get from project class
}

class student {
private:
    string stu_name, stu_id, stu_faculty, stu_year, project_name, lec_name; 
public:
    student() {}//default constructor
    student(string stu_name= "unknown", stu_id= "0", stu_faculty= "Null",  lec_name="unknown", project_name="no title") {}; // overload ...

HacruLeian

class Project {
private: 
    string lec_name, stu_name, stu_id , project_name, Pro_startdate, Pro_duedate;

public:
    Project() {} // default constructor
    Project(string lec_name= "unknown", stu_name="unknown", stu_id="0" , project_name="no title",Pro_startdate="Null", Pro_duedate= "Null") {}; // overload constructor
    string getLecName(){return lec_name;} // get from class lecturer
    string getStuName(){return stu_name;}//get from student class
    string getStuID(){return stu_id;}//get from student class
    string getProName(){return project_name;}
    string getProStaDate(){return Pro_startdate;}
    string getProDueDate(){return Pro_duedate;}

}

this is how i implement the class. because the whole assignment need to have lecturer, student and project, so i need to implement three class right?

uh, can anyone explain about the constructor initializer list? well, i don't really know how to implement it.

thank you.

HacruLeian Newbie Poster

hello, i'm confused here. i have an assignment that required me to create a project registration program that have student, project and supervisor. i haven't start to do it yet. but i've gone through the question. there's a part of the program that need to list out the student's, supervisor's and project's attributes that the user had enter. i thought of using vector to store all the attributes. but i don't think vector can do/store the attribute and list it out with something like this:

project's name: Computer
Supervisor: DR Stanley
Student Name: Robbinson
Student ID: 12345532

so, i'm not sure how to do for this part. can anyone explained to me what other method can i use? well, i know for this assignment, i need to use class and inheritance so far...

thank you

HacruLeian

hello,

for the checking part i used if else statement, because i couldn't understand the loop, i want to ask a few things here, if i declare array[20] means my value supposed from 0 to 19 right? but why when generate the number and print it, there's a value 0, whereas in my array, i don't have 0 in there.this is my array that store the value:

[CODE]int boxvalue[20]={1,5,50,100,200,500,1000,2000,5000,20000,30000,50000,100000,120000,150000,200000,220000,250000,300000};[/CODE]

when the game started, user must select a box and in the game, if user input no deal, user have to select certain number of boxes available right? so, how do we do this? we implement loop for this right?

then each round the number of boxes that user need to choose will be decreasing am i right? so for the round part i think i want to implement function,but how about number of boxes can be select during that round? should i implement different loop or use back the same and reset the number of boxes to be chosen?this is the loop to eliminate the boxes, and i want to set the box selected to be 'X' or something so that user cannot choose that particular box again. but it seems like i failed. i tried to assign 'X' to box[num] then print the whole box again, it still appeared the same, and i tried to change the text color of the box selected to black, it failed as well, so i got no idea what can i do anymore. ...

HacruLeian

hello, how to set color using c++? i've searched through the google and i still can't get it, they use void setcolor() and some kind of function inside, which i don't understand....

HacruLeian

hello, why i got the output all 0 when i do this:

[CODE]for (int i=0; i<20; i++)
{
    for (int j=i+1;j<20; j++)
    {
        if (cboxvalue[j]==cboxvalue[i])
        {
            cboxvalue[j]=0;
        }
    }
}
for ( int i=0; i <20; i++)
    {
        if ( cboxvalue[i]=0)
        {
            RandIndex=rand()% 20;
            cboxvalue[i]=boxvalue[RandIndex];
        }
    }[/CODE]

i thought we use search array to do it? aren't way of doing search array is using loop and if else statement?

HacruLeian

but arent both i and j have the same value? because both start from zero and we're looking at the same array,

[CODE]for(int i=0;i<26;++i)
{ for( int j=0;j<26;++j)
{ if(cboxvalue[i]==cboxvalue[j])[/CODE]

HacruLeian

hello,

i wanted to do error checking to ensure that there's no repeated value in the boxes. so i thought of using array searching to do it, this is what i have in mind:

[CODE]int main()
{
int box[26]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
double boxvalue[26]={.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,75000,100000,200000,300000,400000,500000,750000,1000000};
double cboxvalue[26];
int RandIndex; int input;

srand( time(NULL) );
for( int j=0; j<26; j++)
{
   RandIndex=rand()%26;
       cboxvalue[j]=boxvalue[RandIndex];

       do{

          if( cboxvalue[1]==1||cboxvalue[1]==5||cboxvalue[1]==75||.....)//checking
           {
              RandIndex=rand()%26;
      cboxvalue[j]=boxvalue[RandIndex];
            }while( cboxvalue[1]==true)//not sure here

     }[/CODE]

i thought of another way is that, to do this:

[CODE]if(cboxvalue[1]==cboxvalue[2]&&cboxvalue[1]==cboxvalue[3]&&.....)
{

RandIndex=rand()%26;
cboxvalue[1]=boxvalue[RandIndex];

}
else if ( cboxvalue[2]==cboxvalue[1]....)
{
RandIndex=rand()%26;
cboxvalue[2]=boxvalue[RandIndex];
}[/CODE]

i think i'll need loop for this, but i not sure it need to loop how many times, or i should use do while to do it?

HacruLeian

there are a few things i need to do: print all the value in deal or no deal, calculate the banker's offer, user's selection, error handling. i want to made sure the value generated for each boxes is not the same, means there's no same number generated on two or more boxes, so i have to do checking for this, i wanted to use array searching, but the main things is that i don't even know what's the value in the boxes, so the condition will be a problem, i wanted to use:

if (cbox[j]==.....)

the condition i not sure how to write, but i do know we use "&&" in the condition...

HacruLeian

hello adityatandon,

i see, thank you for explaining the %5! XD

HacruLeian

hello, may i know what is the meaning of invalid type double[26][double] for array subscript?

i've tried to put all the thing inside the main, once correct, i will implement it in function, but when i run the program, it print "invalid type double[26][double] for array subscript". i wonder why we can't do this?

[CODE]#include

include
include

using namespace std;

int main()
{
int box[26]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
double boxvalue[26]={.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,75000,100000,200000,300000,400000,500000,750000,1000000};
double cboxvalue[26];
double RandIndex; int input;// the RandIndex cannot be declared as double.Why?

srand( time(NULL) );
for( int j=0; j<26; j++)
{
    RandIndex=rand()%26;
    cboxvalue[j]=boxvalue[RandIndex];

}

for( int i=0; i<26; i++)
{
        cout<<" "<<"|"<<box[i]<<"|";
}
cout<<endl;

cout<<"Please enter a number:"<<endl;
cin>>input;

if( input==1)
{
    cout<<cboxvalue[1];
}
else
{
    cout<<"Bad input"<<endl;
}

}
[/CODE]

HacruLeian

hello adityatandon,

may i know why you put the %5?

[CODE]int j=0;
for( int i=0; i<26; i++)
{
cout<<" "<<"|"<<box[i]<<"|";
++j;
if(j%5==)
cout<<"\n";
}[/CODE]

HacruLeian

hello, um, you meant, the data type must be the same?

HacruLeian

hello, um, may i know what's the meaning of "double amount_in_box[26] shadows a parameter? um, the reason i declare as double is because the value inside the box contain decimal, when i type double RandIndex as the declaration for the RandIndex, it gives error when i compile, but when i typed int, it works, so i'm a little not sure here. i used MinGW and notepad++ for programming

HacruLeian Newbie Poster

hello, i want to get some guidance from the reader who read my post. I'm doing a game called deal or no deal using c++ and i can't think of other way to print the box's number instead of printing the value inside. what i want to do is, user input the number of the box he/she select and print the value inside. what i can think of is use pass by reference by calling the function that store the value inside the box once the user input the number. but it came out error. i wonder that is my way of using pass by reference is wrong? and my i know how to calculate the banker's offer? i had browsed through google on how to calculate the offer, but i still don't understand the formula.

[CODE]#include

include
include

using namespace std;

void generate(double amount_in_box[], int RandIndex, double array[])
{

srand( time(NULL) );
double amount_in_box[26]={.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,75000,100000,200000,300000,400000,500000,750000,1000000};
for(int i=0; i<26; i++)
{
    RandIndex=rand() % 26;
    array[i]==amount_in_box[RandIndex];
}   

}

void showbox(int box[])
{
box[26]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,

            14,15,16,17,18,19,20,21,22,23,24,25,26};

for( int i=0; i<26; i++)
{
        cout<<" "<<"|"<<box[i]<<"|";
}

}
void GetValue(double& array[],int input)
{

cout<<"Please choose a number:"<<endl;
cin>>input;

if( input==1)// testing purpose
{
    cout<<array[1]<<endl;
}
else
{
    cout<<"bad input"<<endl;
}

}
int main()
{
double amount_in_box[26];double array[];int box[26],
int RandIndex,input;
generate(amount_in_box[],RandIndex);
showbox( box[]);
GetValue(array[],input);

}[/CODE]

HacruLeian

sorry, i will post in a new thread

HacruLeian

um, i want to print the value inside the box i choose, but i don't have any idea to get the value, if i use assign then when output i think it will be the value inside the box instead of the box's number, as for the second part, i want to arrange it something like this:

1 2 3 4 5

6 7 8 9 10

but i got no idea how to do it.