Results 1 to 7 of 7
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default Concatinating strings help

    Hi,

    I'm doing a college question however I'm not quite getting the output I need. What the end output when running this should be "pass Error fail"

    but instead I'm getting

    pass
    passError
    passErrorfail

    The actual method is here:

    Java Code:
    import java.util.*;
    public class Ex7{
    
    public static void printPassList(Vector v){
    
    Vector vec = new Vector();
    vec = v;
    
    String str ="";
    
    int vecSize = vec.size();
    
    for(int i=0;i<vecSize;i++){
    
        int mark;
            if(vec.elementAt(i) instanceof Student){
    
        Student key = (Student) vec.elementAt(i);
    
        mark = key.getMark();
    
            if(mark <50){
                str += "fail";
            }
            else if((mark >=50) && (mark <70)){
                str += "pass";
            }
            else if(mark >= 70) {
                str += "distinction";
            }
        }
        else{
            str+= "Error";
        }
    
       System.out.println(str);
    
    
    }//END for loop
    
    
    
    }//END method
    }//END class
    The main method I made to test this is here:

    Java Code:
    import java.util.*;
    public class Test
    {
    public static void main(String[] args) {
    
    Vector ClassList = new Vector();
    
    Student John = new Student("John","220289","7",54);
    Student Jack = new Student("Jack","220190","8",33);
    String s = "www";
    
    ClassList.addElement(John);
    ClassList.addElement(s);
    ClassList.addElement(Jack);
    
    Ex7.printPassList(ClassList);
    }
    }
    Can someone please help

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default

    Nowhere in your code is a String catenated to another String that has a space character in it so the final result will not have a space in it.

    kind regards,

    Jos

  3. #3
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    My mistake I didnt realise I put the print message in the for loop by accident, ive had a read on the net and fixed it, thanks

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by counterfox View Post
    My mistake I didnt realise I put the print message in the for loop by accident, ive had a read on the net and fixed it, thanks
    We are talking about different things here: indeed, that System.out.println( ... ) call inside the body of the loop shows you the progression so far; I was talking about those spaces (which is futile) and the end result is as you wanted it to be.

    kind regards,

    Jos

  5. #5
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    Yeah I understand, its just for the question the spaces are needed.
    This is what I done to get the result I wanted

    Java Code:
    import java.util.*;
    public class Ex7{
    
    public static void printPassList(Vector v){
    
    Vector vec = new Vector();
    vec = v;
    
    String str ="";
    String space =" ";
    
    int vecSize = vec.size();
    
    for(int i=0;i<vecSize;i++){
    
        int mark;
            if(vec.elementAt(i) instanceof Student){
    
        Student key = (Student) vec.elementAt(i);
    
        mark = key.getMark();
    
            if(mark <50){
                str = str + "fail";
            }
            else if((mark >=50) && (mark <70)){
                str = str + "pass";
            }
            else if(mark >= 70) {
                str = str + "distinction";
            }
        }
        else{
            str = str + "Error";
        }
    str = str+space;
    }//END for loop
    
    System.out.println(str);
    
    }//END method
    }//END class
    I'm not sure if thats the best way to join strings with spaces but all I could think of

  6. #6
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by counterfox View Post
    Yeah I understand, its just for the question the spaces are needed.
    This is what I done to get the result I wanted

    Java Code:
    import java.util.*;
    public class Ex7{
    
    public static void printPassList(Vector v){
    
    Vector vec = new Vector();
    vec = v;
    
    String str ="";
    String space =" ";
    
    int vecSize = vec.size();
    
    for(int i=0;i<vecSize;i++){
    
        int mark;
            if(vec.elementAt(i) instanceof Student){
    
        Student key = (Student) vec.elementAt(i);
    
        mark = key.getMark();
    
            if(mark <50){
                str = str + "fail";
            }
            else if((mark >=50) && (mark <70)){
                str = str + "pass";
            }
            else if(mark >= 70) {
                str = str + "distinction";
            }
        }
        else{
            str = str + "Error";
        }
    str = str+space;
    }//END for loop
    
    System.out.println(str);
    
    }//END method
    }//END class
    I'm not sure if thats the best way to join strings with spaces but all I could think of
    it's even easier than that, if you just put spaces inside your quotes.

    likes "Error " instead of "Error"

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Also, there's no reason to create a new Vector
    Java Code:
    Vector vec = new Vector();
    and then immediately overwrite it with a passed parameter.
    Java Code:
    vec = v;
    Just declare your variable and do the assignment in one statement instead.

    -Gary-

Similar Threads

  1. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 05:30 AM
  2. Concatinating contents of a string array based on condition
    By gangsterooseven in forum Advanced Java
    Replies: 2
    Last Post: 10-07-2009, 07:35 AM
  3. Concatinating a String
    By gwithey in forum New To Java
    Replies: 4
    Last Post: 03-30-2009, 01:34 PM
  4. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •