CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: ostringstream

  1. #1
    Join Date
    Mar 2008
    Posts
    78

    ostringstream

    so i have this code that i had used in c++, but i don't know what i should i use in java...
    Code:
    	public String getStringMessageID()
    	{
    		ostringstream temp;
    		temp << messageId;
    		return temp.str();
    	}
    These are the errors for the above:
    ostringstream cannot be resolved to a type Assignment 10/src Messages.java line 114 1209617205928 8023
    Syntax error on token "<<", invalid AssignmentOperator Assignment 10/src Messages.java line 115 1209617205929 8024


    I also have another error which i dont know what to do about, but would love some help on
    Code:
    	static void sortArray(Message msg[], int size)
    	{
    		Message temp;
    		boolean swapped = true;
    		int count = 0;
    		for(int index = 0; index < size && swapped; index ++) 
    		{
    			swapped = false;
    
    			for(int i = 0; i < (size - 1) - count; i ++)
    			{
    				if (msg[i].getDate() > msg[i + 1].getDate())
    				{
    					swapped = true;
    					temp = msg[i];
    					msg[i] = msg[i + 1];
    					msg[i + 1] = temp;
    					
    				}
    			}count ++;
    		}
    	}
    Here is the error i get for this one:
    The operator > is undefined for the argument type(s) java.lang.String, java.lang.String Assignment 10/src Mailbox.java line 90 1209618068710 8102


    If you would like to see the rest of the code just let me know and i will post it
    Last edited by zhbcool; May 1st, 2008 at 12:25 AM.

  2. #2
    Join Date
    Dec 2006
    Posts
    166

    Re: ostringstream

    Quote Originally Posted by zhbcool
    so i have this code that i had used in c++, but i don't know what i should i use in java...
    Code:
    	public String getStringMessageID()
    	{
    		ostringstream temp;
    		temp << messageId;
    		return temp.str();
    	}
    These are the errors for the above:
    ostringstream cannot be resolved to a type Assignment 10/src Messages.java line 114 1209617205928 8023
    Syntax error on token "<<", invalid AssignmentOperator Assignment 10/src Messages.java line 115 1209617205929 8024
    There is no way this "<<" stuff will work. But you can concatenate pretty much anything into a string.
    Code:
    return "" + temp;
    You can also use StringBuffer if you need to do this a lot.

    Quote Originally Posted by zhbcool
    I also have another error which i dont know what to do about, but would love some help on
    Code:
    	static void sortArray(Message msg[], int size)
    	{
    		Message temp;
    		boolean swapped = true;
    		int count = 0;
    		for(int index = 0; index < size && swapped; index ++) 
    		{
    			swapped = false;
    
    			for(int i = 0; i < (size - 1) - count; i ++)
    			{
    				if (msg[i].getDate() > msg[i + 1].getDate())
    				{
    					swapped = true;
    					temp = msg[i];
    					msg[i] = msg[i + 1];
    					msg[i + 1] = temp;
    					
    				}
    			}count ++;
    		}
    	}
    Here is the error i get for this one:
    The operator > is undefined for the argument type(s) java.lang.String, java.lang.String Assignment 10/src Mailbox.java line 90 1209618068710 8102
    You can do something like
    Code:
    firstString.compareTo(secondString) > 0
    but I don't understand why you are comparing dates as strings (they won't order correctly as dates).

    Also, you should not pass in a "size" argument, because the length of the array you can get with msg.length

    Ideally, you should have your Message class implement Comparable, implement a compareTo() method, and use the Arrays.sort() function to sort your array or something.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: ostringstream

    Sun now recommend using StringBuilder rather than StringBuffer, unless it will be shared by multiple threads.

    If you're planning to port code from C++ to Java, it would be worth reading up on the differences so you know about issues like these. Google turns up a number of articles - e.g. Differences Between Java and C++.

    If you don't think carefully, you might believe that programming is just typing statements in a programming language...
    W. Cunningham
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Mar 2008
    Posts
    78

    Re: ostringstream

    I need to sort the messages by their date

    maybe it will be easier to see the whole program at the point where the Sort comes in
    Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Mailbox {
    	
    	static int ID;
    	static int count =0;
    	Message A;
    	static String a;
    	static Message mes[];
    	
    public static void main (String args[]) throws IOException{
    		
    		File f = new File("messages"); 
    		Scanner in = new Scanner(f);
    
    			System.out.print("Which messageID would you like to view? (ID)");
    			ID = (int) System.in.read();
    			
    			
    			while(count < 1000)
    			{
    				
    				in.nextLine();
    				mes[count].setDate(a);
    
    				in.nextLine();
    				mes[count].setSender(a);
    			
    				in.nextLine();
    				mes[count].setRecipient(a);
    			
    				in.nextLine();
    				mes[count].setBody(a);
    
    				count++;	
    			}
    			sortArray(mes, count);
    			if(IDSearch(mes, ID, count)==-1)
    				System.out.println("Error: Can't find your message");
    			else
    			{
    				System.out.println("Your message is:");
    				System.out.printf(mes[IDSearch(mes, ID, count)].toString());
    			}
    				
    			System.out.print(mes[0].toString());
    			System.out.print(mes[1].toString());
    			System.out.print(mes[2].toString());
    			System.out.print(mes[3].toString());
    			System.out.print(mes[4].toString());
    			
    
    	return;
    	}
    
    
    
    /******************************************************************************
    Name:			sortArray
    Description:	Function that sorts the array.  It a bubble sort algorithm 
    				similar to the one you showed us in class.
    
    Input:			
    		Message, integer
    Output:			
    		none
    ******************************************************************************/
    	static void sortArray(Message msg[], int size)
    	{
    		Message temp;
    		boolean swapped = true;
    		int count = 0;
    		for(int index = 0; index < size && swapped; index ++) 
    		{
    			swapped = false;
    
    			for(int i = 0; i < (size - 1) - count; i ++)
    			{
    				if (msg[i].getDate() > msg[i + 1].getDate())
    				{
    					swapped = true;
    					temp = msg[i];
    					msg[i] = msg[i + 1];
    					msg[i + 1] = temp;
    					
    				}
    			}count ++;
    		}
    	}
    
    
    /******************************************************************************
    Name:			IDSearch
    Description:	Function that searches the array to find position.  It then
    				returns position.
    
    Input:			
    		Message, 2 integers
    Output:			
    		integer		pos
    	******************************************************************************/
    	static int IDSearch(Message msg[],int ID, int elements)
    	{
    		int pos = -1;
    
    		for( int count  = 0; count < elements; count ++)
    		{
    			if(msg[count].getMessageID() == ID)
    			{
    				pos = count;
    			}
    		}
    
    		return pos;
    	}
    
    }
    Last edited by zhbcool; May 1st, 2008 at 12:49 PM.

  5. #5
    Join Date
    Mar 2008
    Posts
    78

    Re: ostringstream

    I figured out the string thing i used:
    string1.compareTo(string2) > 0;

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: ostringstream

    I figured out the string thing i used:
    string1.compareTo(string2) > 0;
    err how exactly does your solution differ from the one spoon! gave you? ie
    Quote Originally Posted by spoon!
    You can do something like
    Code:
    firstString.compareTo(secondString) > 0

  7. #7
    Join Date
    Mar 2008
    Posts
    78

    Re: ostringstream

    It doesn't... i just wanted to re state the method so no one is confused on what method i was talkin about

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured