d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Java Question
Add Reply New Topic New Poll
Member
Posts: 18,413
Joined: Sep 18 2005
Gold: 4,827.00
Feb 11 2014 06:24pm
I am getting an error for
list1 = input.nextLine().split(" ");
I am trying to get rid of the whitespace and replace it with a , in my arrays but i get an incompatible error. would anyone be able to help me out?
example output i get with the input 1 2 3 4 without the split is
[1 2 3 4]
i want
[1,2,3,4]

Code
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class subset2
{


public static void main (String[] args)
{

ArrayList<String> list1 = new ArrayList<String>();
ArrayList<Integer> list2 = new ArrayList<Integer>();


Scanner input = new Scanner(System.in);
while(input.hasNextLine()){
if (input.hasNextLine())
{
if(input.hasNextInt()){

list1.add(input.nextLine());
list1 = input.nextLine().split(" ");
}
}

System.out.println(list1);

}
}
}


This post was edited by Acdc-rocks[tom] on Feb 11 2014 06:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 11 2014 06:30pm
i think you're mistaken with what you want.

if you have a string that looks like this:
Quote
example output i get with the input 1 2 3 4 without the split is
[1 2 3 4]


then you don't want to split it, you want to replace the space with comma. there should be replace(..) method in there. keep in mind that split(..) is looking for the space and breaking it into a list, not combining elements and putting a space in between them.

if you want to create a list, please describe what elements you want.

right now you'll get this:
list[0] = 1
list[1] = 2
list[2] = 3
list[3] = 4

do you want this?

list[0] = 1
list[1] = ,
list[2] = 2
list[3] = ,
...

or are you complaining about toString()'s output? you can simply write your own. instead of System.out.println(list), iterate over the list and print each element along with a corresponding comma. i think apache string utils has something for this but i could be wrong.

This post was edited by carteblanche on Feb 11 2014 06:34pm
Member
Posts: 18,413
Joined: Sep 18 2005
Gold: 4,827.00
Feb 11 2014 06:34pm
im trying to trade the white space for a , so my output looks like [1,2,3,4] rather than [1 2 3 4]
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 11 2014 06:36pm
Quote (Acdc-rocks[tom&#93; @ Feb 11 2014 07:34pm)
im trying to trade the white space for a , so my output looks like [1,2,3,4] rather than [1 2 3 4]


ok, so your list is fine and you're just complaining about toString(). two ways to handle it.

1) as i described earlier, create your own function to do it precisely the way you want
2) more of a hack, but you can do something like this: System.out.println(list.toString().replace(' ', ','));
i don't remember the method signature, but essentially you wanna call toString() yourself then replace space with comma before printing it.
Member
Posts: 18,413
Joined: Sep 18 2005
Gold: 4,827.00
Feb 11 2014 06:41pm
Quote (carteblanche @ Feb 11 2014 08:36pm)
ok, so your list is fine and you're just complaining about toString(). two ways to handle it.

1) as i described earlier, create your own function to do it precisely the way you want
2) more of a hack, but you can do something like this: System.out.println(list.toString().replace(' ', ','));
i don't remember the method signature, but essentially you wanna call toString() yourself then replace space with comma before printing it.


#2 worked perfectly for me thank you!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll