7 Replies - 5735 Views - Last Post: 15 April 2012 - 11:07 PM Rate Topic: -----

#1 cbennett  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

How to convert String to int array

Posted 15 April 2012 - 10:02 PM

Ok, so I need to read data from a file (I know how to do that one finally), and convert the contents from a string to an int array. Here's the code I have thus far:

 

import java.util.*;
import java.io.*;




public class DialReadings 
{
	
	public static void main(String [] args) throws FileNotFoundException
	{
		
		Scanner reader = new Scanner (new File("Data.txt"));
		String candidateName = reader.nextLine();
		System.out.print(candidateName + " \n");	
		int voterName1 = reader.nextInt();
		System.out.print(voterName1 + "\n");
		
		/*
		int voterName2 = reader.nextInt();
		System.out.print(voterName2+ "\n");
		int voterName3 = reader.nextInt();
		System.out.print(voterName3 + "\n");
		int voterName4 = reader.nextInt();
		System.out.print(voterName4 + "\n");
		int voterName5 = reader.nextInt();
		System.out.print(voterName5 + "\n");
		int voterName6 = reader.nextInt();
		System.out.print(voterName6 + "\n");
		int voterName7 = reader.nextInt();
		System.out.print(voterName7 + "\n");
		int voterName8 = reader.nextInt();
		System.out.print(voterName8 + "\n");
		int voterName9 = reader.nextInt();
		System.out.print(voterName9 + "\n");
		int voterName10 = reader.nextInt();
		System.out.print(voterName10 + "\n");
		String canidateName2 = reader.nextLine();
		System.out.print(canidateName2 + "\n");
		int voterName21 = reader.nextInt();
		System.out.print(voterName21 + "\n");
		int voterName22 = reader.nextInt();
		System.out.print(voterName22+ "\n");
		int voterName23 = reader.nextInt();
		System.out.print(voterName23 + "\n");
		int voterName24 = reader.nextInt();
		System.out.print(voterName24 + "\n");
		int voterName25 = reader.nextInt();
		System.out.print(voterName25 + "\n");
		int voterName26 = reader.nextInt();
		System.out.print(voterName26 + "\n");
		int voterName27 = reader.nextInt();
		System.out.print(voterName27 + "\n");
		int voterName28 = reader.nextInt();
		System.out.print(voterName28 + "\n");
		int voterName29 = reader.nextInt();
		System.out.print(voterName29 + "\n");
		int voterName210 = reader.nextInt();
		System.out.print(voterName210 + "\n");
		*/
	}
	
		
	
	public static double mean(File file) throws FileNotFoundException
	{   
		Scanner reader = new Scanner (new File("Data.txt"));
		
		String voterInput = reader.nextLine();
		char[] voterInputArrayChar = voterInput.toCharArray();
		int number = Integer.parseInt(new String(voterInputArrayChar));

		
		
		int sum=0;
		for (int index=0; index < voterInputArray.length; ++index)
		{
			sum += voterInputArray[index];
		}
		
		
		double average =  sum / (double) voterInputArray.length;
		
		System.out.print(voterName1);
		return 0; 
	}
	
}




What I am trying to do is get the voter results into an integer array so I can find the mean, median, mode, minimum, and maximum. I know how to find the functions, I am just having trouble creating the arrays from strings :(

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert String to int array

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,136
  • Joined: 19-March 11

Re: How to convert String to int array

Posted 15 April 2012 - 10:14 PM

the split() method of the String class is probably what you're looking for. It splits a String into an array of Strings, using some regular expression as the separator. Since the separator is not included in any String, you could use a regular expression specifying "any sequence of non-integers". I believe that \D matches a non-digit character (you can look it up to verify) so splitting on "\\D+" should get you what you're looking for: The String

"1234 3245 adsfalkj 245634561&3452O=1"

becomes the int array

{1234,3245,245634561,3452O,1}
Was This Post Helpful? 0
  • +
  • -

#3 cbennett  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

Re: How to convert String to int array

Posted 15 April 2012 - 10:16 PM

View Postjon.kiparsky, on 15 April 2012 - 10:14 PM, said:

the split() method of the String class is probably what you're looking for. It splits a String into an array of Strings, using some regular expression as the separator. Since the separator is not included in any String, you could use a regular expression specifying "any sequence of non-integers". I believe that \D matches a non-digit character (you can look it up to verify) so splitting on "\\D+" should get you what you're looking for: The String

"1234 3245 adsfalkj 245634561&3452O=1"

becomes the int array

{1234,3245,245634561,3452O,1}


Would it then be an int array that I can utilize for computations?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,136
  • Joined: 19-March 11

Re: How to convert String to int array

Posted 15 April 2012 - 10:25 PM

It's an array of ints, you can do what you like with it as long as you don't scare the horses. Now keep in mind, any time you use a trick like this you're making some assumptions about the nature of the data.

In this case you're assuming - or I'm assuming on your behalf - that you've got a string which consists of some things which are ints and some which are not, and you want all of the ints that appear in the String.


Looking at your problem statement again, I think I might have misread what your data looks like. Actually, I just read "how to convert a string to int array" and assumed you had one long String - doesn't look like that was correct.

So I think what you're actually trying to do is to put together an array of ints, which you're reading in one String at a time.
That'd be a different story altogether - is that closer to what you're up to?
Was This Post Helpful? 0
  • +
  • -

#5 cbennett  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

Re: How to convert String to int array

Posted 15 April 2012 - 10:31 PM

View Postjon.kiparsky, on 15 April 2012 - 10:25 PM, said:

It's an array of ints, you can do what you like with it as long as you don't scare the horses. Now keep in mind, any time you use a trick like this you're making some assumptions about the nature of the data.

In this case you're assuming - or I'm assuming on your behalf - that you've got a string which consists of some things which are ints and some which are not, and you want all of the ints that appear in the String.


Looking at your problem statement again, I think I might have misread what your data looks like. Actually, I just read "how to convert a string to int array" and assumed you had one long String - doesn't look like that was correct.

So I think what you're actually trying to do is to put together an array of ints, which you're reading in one String at a time.
That'd be a different story altogether - is that closer to what you're up to?


I think you may have been right the first time haha, the data is a text file, which has a name Candidate A followed by ints. The first three ints is a codename for the person, followed by data they inputed. So I will be given a text file and have to take the inputed data and find the mean median and mode for the codenamed person. IE:


String candidateName = reader.next();
int voterName = reader.nextInt();



Followed by 20 ints that I want to put in an array so I can find the mean, median, and mode of said array.

I will have 10 voters per candidate, and two candidates so the text file will look like:

Candidate A
101 5 5 5 6 7 8 7 7 6 6 5 5 4 4 3 3 2 2 1 2
102 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
103 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
104 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
105 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
106 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
107 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
108 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
109 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
110 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
Candidate B
101 5 5 5 6 7 8 7 7 6 6 5 5 4 4 3 3 2 2 1 2
102 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
103 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
104 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
105 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
106 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
107 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
108 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
109 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
110 5 6 7 8 8 9 9 8 8 7 7 8 8 9 9 9 9 9 9 9
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,136
  • Joined: 19-March 11

Re: How to convert String to int array

Posted 15 April 2012 - 10:36 PM

Quote

The first three ints is a codename for the person, followed by data they inputed


This does become a little complex, but yes, the one-liner I gave you above will convert each line of this into an array of Strings.

What you do with those is your business...
Was This Post Helpful? 0
  • +
  • -

#7 cbennett  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 13-February 12

Re: How to convert String to int array

Posted 15 April 2012 - 10:53 PM

View Postjon.kiparsky, on 15 April 2012 - 10:36 PM, said:

Quote

The first three ints is a codename for the person, followed by data they inputed


This does become a little complex, but yes, the one-liner I gave you above will convert each line of this into an array of Strings.

What you do with those is your business...


Can you explain a litle more how to use the split() method? I have it to make an array of Strings, but I don't know how to make it into an array of ints. Here's what I have now:

public static double mean(File file) throws FileNotFoundException
	{   
		Scanner reader = new Scanner (new File("Data.txt"));
		
		String voterInput = reader.nextLine();
		String[] voterInputArray = voterInput.split("\\D+");
		String result = 0;
		
		
		int sum=0;
		for (int index=0; index < voterInputArray.length; ++index)
		{
			result = result + voterInputArray[index]; 
		}
		
		
		double average =  sum / (double) voterInputArray.length;
		
		
		return average; 
	}
	
}



Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,136
  • Joined: 19-March 11

Re: How to convert String to int array

Posted 15 April 2012 - 11:07 PM

Ah, sorry, yes. I was collapsing steps in my head. I'm afraid I'm working on a project of my own, and I'm a little distracted.
So, I gave you an array of Strings which are guaranteed to be composed of only digits, and you wanted an array of ints. I'm afraid you're going to have to walk that String array and use parseInt to do the conversion. You can either populate a new int array, or else just use the values in sequence if you know you'll only need them once.

You're just about there, actually - just parse that String in this line:
  result = result + voterInputArray[index]; 


and you're in play. (and, of course, make result an int or use sum as your accumulator, either way)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1