Jump to content




Recent Status Updates

  • Photo
      15 Sep
    Error

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Help with while loop

while loop

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 azazo

azazo

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 06 March 2013 - 01:41 AM

Hi, i need help with this qustion

i solved ,but i dont know how i can get  full address  like this 11th street, nakheel



Problem


* Design and implement a Student class.


a. Each student has the following Data Fields:


* studentFirstName,

* studentLastName,

* studentAddress,

* studentBloodGroup

must be: A, A-, B, B-, AB, AB-, O or O-. Nothing else.

* studentCity.


b. What are the types of these Data?


* Write the following elements :


* Write a Constructor to initialize the values of the Data Fields (attributes).


* Write Accessor and Mutator methods for each data.


* Write a tostring() method to print all the student information’s to standard output.


* Create a main class called StudentDemo.java that uses the student class.


a. Read student info’s from an input file students.txt and create a student object.


* Sample file
: Students.txt

200912345 marya velardi 11th street, nakheel A+ soule


201023456 rechard sosemy zayed street, tawiya O sydeny


201124456 roby vela teniji matter road, lissali B+ lass vegas


 


b. Write a method that generates for a student a file named id.txt that contents same as the following example:


* 200912345.txt



Student Id: 200912345


Student name: marya velardi


Address: 11th street, nakheel


City: soule


BloodType: A+


thats my solution :

 

 

package student;public class Student {
  
    private String Name;
    private String FName;
    private String LName;
    private String StudentAddress;
    private String Sbloodtype;
    private String Studentcity;
    private String ID;
    public Student() {
    }
    public Student(String FtName, String LtName, String StudentAddress, String Sbloodtype, String Studentcity, String ID) {
        this.FName = FtName;
        this.LName = LtName;
        this.StudentAddress = StudentAddress;
        this.Sbloodtype = Sbloodtype;
        this.Studentcity = Studentcity;
        this.ID = ID;
    }
//use Accessor and Mutator methods
    public String getFtName() {
        return FName;
    }
    public void setFtName(String FtName) {
        this.FName = FtName;
    }
    public String getLtName() {
        return LName;
    }
    public void setLtName(String LtName) {
        this.LName = LtName;
    }
    public String getStudentAddress() {
        return StudentAddress;
    }
    public void setStudentAddress(String StudentAddress) {
        this.StudentAddress = StudentAddress;
    }
    public String getSbloodtype() {
        return Sbloodtype;
    }
    public void setSbloodtype( String Sbloodgroup) {
        this.Sbloodtype = Sbloodgroup;
    }
    public String getStudentcity() {
        return Studentcity.toString();
    }
    public void setStudentcity(String Studentcity) {
        this.Studentcity = Studentcity;
    }
    public String getID() {
        return ID;
    }
    public void setID(String ID) {
        this.ID = ID;
    }
    //tostring method to print all the student information’s
  
    public String toString() {
        return "Student{" + "FtName=" + FName + ",\n\t LtName=" + LName + ", StudentAddress=" + StudentAddress + ", Sbloodtype=" + Sbloodtype + ", Studentcity=" + Studentcity + ", ID=" + ID + '}';
    }
  
 
}
 

 



anthor class :

 

package student;
import java.io.*;
import java.util.Scanner;
public class StudentDemo {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner inFile = new Scanner(new FileReader("student.txt"));
        PrintWriter outFile = new PrintWriter("id.txt");
        //declare variables
        String ID;
        String FName;
        String LName;
        String StudentAddress;
        String SCity;
        String bloodtype;
        //read from student file        ID = inFile.next();
        FName = inFile.next();
        LName = inFile.next();        StudentAddress = inFile.next(); // how can i use while loop here in address
        SCity = inFile.next();
        bloodtype = inFile.next();
  
              outFile.println("Student ID :" + (ID));
        outFile.println("Student name:" + (FName) + " " + (LName));
        outFile.println("Address :" + StudentAddress);
        outFile.println("City :" + SCity);
        outFile.println("blood type :" + bloodtype);
        inFile.close();
        outFile.close();
    }
}
 

 


when i run the program i got this :

Student ID :200912345
Student name:marya velardi
Address :11th //when i run the program just get the first address and i wont print it all 
City :soule
blood type : A+

please explain how can I use while loop in address 


 


Edited by Roger, 06 March 2013 - 10:19 AM.
tags, formatting


#2 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 242 posts

Posted 06 March 2013 - 03:03 AM

Use nextLine() in place of next().


I can believe, but why should I?


#3 azazo

azazo

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 06 March 2013 - 07:02 AM

If iwrite nextLine() or next() both are same

 

it dose matter

 

the qustion is i dont understsand how i can get full address by using while loop



#4 Roman Y

Roman Y

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 196 posts

Posted 06 March 2013 - 08:25 AM

seems to me by the pattern that there is always a string containing comma (',') before the last string of the "address". What you could do is:
 

while(!(StudentAddress += inFile.next() + " ").contains(","));
StudentAddress += inFile.next();

 

That should solve the problem, but such solutions are not desired in practice.


Edited by Roman Y, 06 March 2013 - 08:30 AM.