Jump to content

Problem reading numbers into an array cell (java)

Go to solution Solved by madknight3,

It looked super complitcated put like that in the link, the problem still is my input is 22, so do i just put input.split('' '') ?

The split function can do some pretty complicated stuff. But on a basic level, that's all you need.

 

And I see, the underscore is a space (I missed that in your question). Yes, you can use split like that with the space character.

So i would like to read a number that looks like (22) into an array cell. The problem i have is that the number is not actually 22 but its supposed to be 2_2, the _ being a space, i can do that with say sc.nextLine() if im not mistaken but i would like to be able to acess the indiviual numbers later on, as i have to have acess to them as variable a and variable d (TL;DR version: so number format is a_d, how would i read that into 1 array cell to then use later, or how would i input that into a 2d array seeing as the numbers are attached together (like ad) in the file it has to read)

Link to post
Share on other sites

What language?
As for bash :D That is easily done: Pipe the 22 trough

     sed 's/\(.\)/\1 /g'

and you'll get 2_2_ (with _ as a space)

If you need underscores 

     sed 's/\(.\)/\1_/g'

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to post
Share on other sites

Can you just split the string on the underscore when you read it in? Then you can handle each part however you want.

There should be an easier solution to this (its a low level java class they wouldnt bother us with this much effort just reading the data), the problem i have is ill need a switch statement for the first digit and then ill need a switch for the second digit, i have no idea how to get that out of this

Link to post
Share on other sites

There should be an easier solution to this (its a low level java class they wouldnt bother us with this much effort just reading the data), the problem i have is ill need a switch statement for the first digit and then ill need a switch for the second digit, i have no idea how to get that out of this

 

This isn't easy enough?

String input = "2_5";String[] number = input.split("_");// number[0] contains "2"// number[1] contains "5"
Link to post
Share on other sites

 

This isn't easy enough?

String input = "2_5";String[] number = input.split("_");// number[0] contains "2"// number[1] contains "5"

It looked super complitcated put like that in the link, the problem still is my input is 22, so do i just put input.split('' '') ?

Link to post
Share on other sites

It looked super complitcated put like that in the link, the problem still is my input is 22, so do i just put input.split('' '') ?

The split function can do some pretty complicated stuff. But on a basic level, that's all you need.

 

And I see, the underscore is a space (I missed that in your question). Yes, you can use split like that with the space character.

Link to post
Share on other sites

The split function can do some pretty complicated stuff. But on a basic level, that's all you need.

 

And I see, the underscore is a space (I missed that in your question). Yes, you can use split like that with the space character.

Thank you

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×