I am a very new java student.I need to write a program ask the users to enter the series numbers separated by commas, with the valid input is : 23,45,90,4,5
The program should calculate and display the sume of all the numbers.

I had written the code. I knew it is not a good code but I did not know undertand much about String ...Could someone please help? Thanks much.


My first class code is :

import java.util.StringTokenizer;
public class ClassNumber
 {
   private String series;
 // The construc accepts a string containning series
  public ClassNumber(String numberStr)
  {
	// Create a StringTokenizer object.
	StringTokenizer strTokenizer = new StringTokenizer(numberStr,"/");
	series = strTokenizer.nextToken();
  }

  // getSeries method returns the month field.
  public String getSeries()
  {
	 return series;
  }
}
public class TestClassNumber

{

  public static void main (String[] args)
  {
  // create a string contataing series numbers
  String series = "23,45,90,4,5";

  TestClassNumber sr = new TestClassNumber (series);
  //Display the component of the series

  System.out.println(" Input a series of numbers separated by commas:" + series);


  }
 }

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

I don't get what you need help with . pass the value to ur function and make some extra code to add the values...

Well, ur code looks clueless. But congratz! u have made a good choice of using StringTokenizer for parsing the text. But do u know how the String tokenizer works?
Read here properly: http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
The second argument to the StrinTokenizer Constructor is the delimiter which in your case is the comman (,) not a fwd slash.
and the series String object in ur ClassNumber class (based on what uve coded) will contain only the first string.
Ok. Since u havent done anything at all. Ill give u a small pseudocode that u can follow.

1. Get the String
2. Pass it to your class
3. Create a StringTokenizer by passing the above string and "," as args
4. Now setup a loop that will run until strTokenizer.hasMoreTokens() returns false
5. Now for each iteration use nextToken() to get the string and parse it into an Integer. like this
Integer num=Integer.parseInt(strTokenizer.nextToken());
6. Now add this to a sum variable initialized to 0
7. do this until the loop fails.
8. return the sum to main class.
9. print OP.

Good luk!!!!

The alternative method is employing the Scanner class.

Assuming you have completed this with the StringTokenizer class, this is the route I would have went with, and also to give you ideas of methods that can be used.

I prefer to make the variables public within the class:

Public class Test { //Test is the name of your java file, for example.

//Initiate your variables here
Scanner in;
int number;
int total;

When constructing the Scanner can now be used anywhere within the class. In comparison with the StringTokenizer class, you would use the useDelimiter() method instead.

public Test() {
	in = new Scanner(System.in);
         in.useDelimiter(",");
}

Then when receiving the numbers input with the nextInt() method you could add them up to the total variable. These variables could also be included with the public method or any other functions included within the class.

while(in.hasNextInt()){
     number = in.nextInt();
     total = total + number; 
}

I hope this helps.

Regards,
Cleo

Your code is an incomplete code.

1)You have a class "TestClassNumber" with no constructor, but you attempt to create the class instance with a constructor passing in a string?

public class TestClassNumber {
  public static void main(String[] args) {
    ...
    TestClassNumber sr = new TestClassNumber (series);  // HERE?
    ...
  }
}

2)Your StringTokenizer is not using a correct delimiter. You declared string using "," as your delimiter.

String series = "23,45,90,4,5";

But you are using "/" to get the token.

StringTokenizer strTokenizer = new StringTokenizer(numberStr,"/");

As a result, the string returned from the StringToeknizer is the whole string you enter. Change the "/" to "," to get correct tokens.

This article has been dead for over six months. Start a new discussion instead.