Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [solved]Conversion from multiple ints to single string [java]
New Posts  All Forums:Forum Nav:

[solved]Conversion from multiple ints to single string [java]

post #1 of 6
Thread Starter 
I'm passing a bunch of ints to this method...and want to convert them all to one string with spaces in between the ints

ex. "1 4 8 9"

i've tried this but the error is ';' expected on the bold statement
Code:
public String process_Job(int arrival, int pid, int cputime, int jobclock, int Qclock, int llq){
 
 
        String string = "" + arrival " " + pid " " + cputime " " + jobclock " " + Qclock " " + llq;     

 
    }

Edited by surfbumb - 3/12/11 at 6:03pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #2 of 6
I have not programmed in java in years but I'll give this a shot...

Try replacing each of the int names in the line giving you an error with Integer.toString(inName)

So basically:
Code:
String string = "" + Integer.toString(arrival) " " + Integer.toString(pid) " " + Integer.toString(cputime) " " + Integer.toString(jobclock) " " + Integer.toString(Qclock) " " + Integer.toString(llq);

I could be totally wrong though lol
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #3 of 6
Thread Starter 
Quote:
Originally Posted by Xazen View Post
I have not programmed in java in years but I'll give this a shot...

Try replacing each of the int names in the line giving you an error with Integer.toString(inName)

So basically:
Code:
String string = "" + Integer.toString(arrival) " " + Integer.toString(pid) " " + Integer.toString(cputime) " " + Integer.toString(jobclock) " " + Integer.toString(Qclock) " " + Integer.toString(llq);

I could be totally wrong though lol
I got frustrated so i just did it the long way.

Code:
public String process_job(int arrival, int pid, int cputime, int jobclock, int Qclock, int llq){
         Integer.toString(arrival);
         Integer.toString(pid);
         Integer.toString(cputime);
         Integer.toString(jobclock);
         Integer.toString(Qclock);
         Integer.toString(llq);
        
        String string = arrival + " " + pid + " " + cputime + " " + jobclock + " " + Qclock + " " + llq;
     
        return string;
    }
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #4 of 6
Did that work? The Integer.toString() method should return a string, which you would need to save in order to use later in your concatenation line. As you have it I think the return from each of those statements would be thrown away.
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #5 of 6
Thread Starter 
Quote:
Originally Posted by Xazen View Post
Did that work? The Integer.toString() method should return a string, which you would need to save in order to use later in your concatenation line. As you have it I think the return from each of those statements would be thrown away.
your logic was right...i just used your advice since i could use one statement...but you forgot a few + characters

Code:
public String process_job(int arrival, int pid, int cputime, int jobclock, int Qclock, int llq){
      
        String string = "" + Integer.toString(arrival) + " " + Integer.toString(pid) + " " + Integer.toString(cputime) + " " + Integer.toString(jobclock) + " " + Integer.toString(Qclock) + " " + Integer.toString(llq);

        return string;
    }
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #6 of 6
if you look carefully at the original example you posted, you're missing a bunch of plus signs


String string = "" + arrival " " + pid " " + cputime " " + jobclock " " + Qclock " " + llq;



String string = "" + arrival + " " + pid + " " + cputime + " " + jobclock + " " + Qclock + " " + llq;

whoops.

for concatenating strings, the most efficient way is to use StringBuilder. If you need it to be thread safe, use StringBuffer (buffer is synchronized, builder is not).

String string = new StringBuilder("one").append("two")
.append('c').append(integer).append("four").toStri ng();

java will auto invoke toString() on the ints. For just one int,

int x = 4;
String p = "" + x;

is the best way

if you have any other string in there, you don't need the empty string

int x = 4;
int y = 5;
String p = x + " " + y

Switch the + to string builder as you see fit - i typically only go that road for a more complex string, for just two or 3 ints like that, plus is clean enough.

An other way that is a little wierd but I really like is to use message format.

String pattern = "{0} {1}xyz{2}"
String output = MessageFormat.form(pattern, 4, 2, "abc");
4 2xyzabc

And there's also printf() with String.format()
Edited by lloyd mcclendon - 3/13/11 at 12:02pm
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [solved]Conversion from multiple ints to single string [java]