Click to See Complete Forum and Search --> : ostringstream


zhbcool
April 30th, 2008, 11:50 PM
so i have this code that i had used in c++, but i don't know what i should i use in java...

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

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

spoon!
May 1st, 2008, 01:12 AM
so i have this code that i had used in c++, but i don't know what i should i use in java...

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.
return "" + temp;
You can also use StringBuffer if you need to do this a lot.

I also have another error which i dont know what to do about, but would love some help on

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
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.

dlorde
May 1st, 2008, 05:19 AM
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++ (http://www.dickbaldwin.com/java/Java008.htm).

If you don't think carefully, you might believe that programming is just typing statements in a programming language...
W. Cunningham

zhbcool
May 1st, 2008, 10:54 AM
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

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;
}

}

zhbcool
May 1st, 2008, 01:28 PM
I figured out the string thing i used:
string1.compareTo(string2) > 0;

keang
May 1st, 2008, 03:28 PM
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 You can do something like
Code:
firstString.compareTo(secondString) > 0

zhbcool
May 4th, 2008, 02:38 AM
It doesn't... i just wanted to re state the method so no one is confused on what method i was talkin about