The Artima Developer Community
Sponsored Link

Java Answers Forum
Array problem

3 replies on 1 page. Most recent reply: Feb 9, 2006 11:08 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Madhan Kumar

Posts: 10
Nickname: madhan
Registered: Mar, 2002

Array problem Posted: Feb 8, 2006 1:12 PM
Reply to this message Reply
Advertisement
Hi,

I have been struggling in this program for 2 days.I am new to programming.
please help me.

public static void operation1 (String[] dbcolumn1){
 
int s=Integer.parseInt( dbcolumn1[0]);
int s1=Integer.parseInt(dbcolumn1[2]);
 
 
if (dbcolumn1[1].equals("+")) {
dbcolumn1[2]=dbcolumn1[0] + dbcolumn1[2]; 
System.out.println(dbcolumn1[2]);
 
}
else if (dbcolumn1[1].equals("*")) {
dbcolumn1[2]= dbcolumn1[0] * dbcolumn1[2]; //1
}
else if (dbcolumn[1].equals("-")){
dbcolumn1[2]= dbcolumn1[0] * dbcolumn1[2]; //2
}
else if (dbcolumn1[1].equals("/")) {
dbcolumn1[2]= dbcolumn1[0] * dbcolumn1[2]; //3
}
}
    

Question 1)

In the above method,the display line
System.out.println(dbcolumn1[2]);
should not give 1+2 total 3.why?

Question 2)

lines 1,2,3 showng these operators should not apply to String

Please help me to find out what i am doing wrong?


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Array problem Posted: Feb 8, 2006 6:55 PM
Reply to this message Reply
Please see the comments in the program below.

public class Test {
	public static void main(String[] args) {
		String[] a = { "1", "+", "99" } ;
		operation1(a);
	}
	public static void operation1 (String[] dbcolumn1){
 
		// You need to place the following two statements in try-catch - in case
		// values are not really integers
 
		int s = Integer.parseInt(dbcolumn1[0]);
		int s1= Integer.parseInt(dbcolumn1[2]);
 
 
		if (dbcolumn1[1].equals("+")) {
			//dbcolumn1[2]= dbcolumn1[0] + dbcolumn1[2];
			dbcolumn1[2]= (s + s1)  + "";
			System.out.println(dbcolumn1[2]);
 
		}
		else if (dbcolumn1[1].equals("*")) {
			dbcolumn1[2]= ( s * s1 ) + "";
		}
		else if (dbcolumn1[1].equals("-")){
			dbcolumn1[2]= (s - s1) + "";
		}
		else if (dbcolumn1[1].equals("/")) {
			dbcolumn1[2]= (s/s1) + ""; // Should check for divide by zero exception
		}
	}
 
}

Madhan Kumar

Posts: 10
Nickname: madhan
Registered: Mar, 2002

Re: Array problem Posted: Feb 9, 2006 6:23 AM
Reply to this message Reply
Thanks for your response.Can anyone please help me with the array? for ex,what if i give

String[] a = { "1", "+", "99","*","99","/","55" } ;

them we don't need to use that a[0],a[1] ans so on.
could you please show me how to do that in a[i].

Thanks.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Array problem Posted: Feb 9, 2006 11:08 PM
Reply to this message Reply
This sounds much like homework to mee, but I'm in a good mood, so here you go:

It's very easy, but I had to make one change. a number follows another number, the second number will be added.

enum Operations {ADD, SUBTRACT, DIVIDE, MULTIPLY};
public static void operation1 (String[] dbcolumn1){
    double currentValue = 0;
    Operations operation = ADD;
    for (String s : dbcolumn1) { //this works only for Java 1.5, use tthe following lines for earlier Java versione
//    for (int i = 0; i < dbcolumn1.length; i++) {
//        String s = dbcolumn1[i];       
        if ("/".equals(s))
            operation = Operations.DIVIDE;
        else if ("*".equals(s))
            operation = Operations.MULTIPLY;
        else if ("+".equals(s))
            operation = Operations.ADD;
        else if ("-".equals(s))
            operation = Operations.SUBTRACT;
        else { //Number
            double val = 0;
            try {
                val = Double.parseDouble(s);
            } catch (NubmerFormatException e) {
                System.out.println ("Parse error. Assuming 0");
            }
            if (operation == Operations.ADD)
                currentValue = currentValue + val;
            else if (operation == Operations.SUBTRACT)
                currentValue = currentValue - val;
            else if (operation == Operations.MULTIPLY)
                currentValue = currentValue * val;
            else // divide
                currentValue = currentValue / val; //this can result in an Exception if val == 0
            operation = Operations.ADD; //this is the change I mentioned above.
        }
    }
    System.out.println (currentValue);
}


Sorry if there are some errors in the text, but I didn't write this in a IDE and didn't compile it.

Flat View: This topic has 3 replies on 1 page
Topic: Regular expressions  - how Previous Topic   Next Topic Topic: JSP Recommendation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use