Results 1 to 1 of 1
Thread: String Method problem.
- 04-27-2012, 03:40 AM #1Member
- Join Date
- Apr 2012
- Rep Power
String Method problem.
Hey guys, I am new to Java and I need some help trying to solve this problem.
public class Tester
public static void main(String args)
Scanner kb = new Scanner(System.in);
System.out.print("Enter something like 8 + 33 + 1,345 +137 : ");
String s = kb.nextLine( ); //Best to store in a String and then create a new Scanner
//object; otherwise, it can get stuck waiting for input.
Scanner sc = new Scanner(s);
//Set delimiters to a plus sign surrounded by any amount of white space...or...
// a minus sign surrounded by any amount of white space.
int sum = 0;
sum = sum + sc.nextInt( );
System.out.println("Sum is: " + sum);
I have been stuck at trying to solve this problem:
"Now modify this program so as to allow either plus or minus signs. Donít forget to allow for a
leading plus or minus sign on the first number in the sequence. If the leading number has no sign,
assume the number is positive. Your output should typically appear as follows:
Enter something like 8 + 33 + 1,345 -137
Sum is: 1249"
Can you please also explain abit about how these delimiters work?
- By shihad_s in forum New To JavaReplies: 10Last Post: 12-07-2010, 12:29 PM
- By gonzoateafly in forum New To JavaReplies: 6Last Post: 12-06-2010, 09:26 AM
- By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM
- By rons_sacramental in forum New To JavaReplies: 7Last Post: 10-15-2009, 05:15 AM
- By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM