Easy help but can't figure it out.
ok I managed to get this working correctly. The code is to take in a string and add the numbers what I am trying to change is to also allow subtraction including checking the first number for a + or - sign. I have tried many things but I guess I am not seeing hot to set the If statement for that seems to be what would work. NOt sure how to check for - or + using the scanner. Any help would be great.
Code :
import java.io.*;
import java.util.*;
public class Tester
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter something like 8 + 33 + 1345 +137 : ");
String s = kb.nextLine( );
Scanner sc = new Scanner(s);
sc.useDelimiter("\\s*\\+\\s*");
int sum = 0;
while(sc.hasNextInt( ))
{
sum = sum + sc.nextInt( );
}
System.out.println("Sum is: " + sum);
}
}
Re: Easy help but can't figure it out.
Quote:
Originally Posted by
weezer562
ok I managed to get this working correctly. The code is to take in a string and add the numbers what I am trying to change is to also allow subtraction including checking the first number for a + or - sign. I have tried many things but I guess I am not seeing hot to set the If statement for that seems to be what would work. NOt sure how to check for - or + using the scanner. Any help would be great.
Code :
import java.io.*;
import java.util.*;
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( );
Scanner sc = new Scanner(s);
sc.useDelimiter("\\s*\\+\\s*");
int sum = 0;
while(sc.hasNextInt( ))
{
sum = sum + sc.nextInt( );
}
System.out.println("Sum is: " + sum);
}
}
The Scanner class can't add for you.
To take the values you have in a String and make them into numbers, you use the wrapper classes.
Integer.parseInt(String str);
Double.parseDouble(String str);
Long.parseLong(String str);
But you can only do that with one String at a time. Inputting 8+5 will make a String called "8+5", which is a String.
Scanner(String source)
Constructs a new Scanner that produces values scanned from the specified string.
Again, I don't see how nextInt() will work for Strings. You can't take in "7+5" as a String and expect it to print out 12.
At least, not to my knowledge.
Re: Easy help but can't figure it out.
It is actually stopping at the + symbol and then adding the nextInt this does work , my problem is adding subtraction to the mix. I have tried adding an If statementwithin the while loop , but with no luck not sure what the conditional statement would be.
Re: Easy help but can't figure it out.
System.out.print("Enter something like 8 + 33 + 1,345 +137 : ");
I'm not sure if the int thing will recognize commas.
Re: Easy help but can't figure it out.
Fixed that took out the example but in that code if a person enters lets say 20 + 35 + 45 it should work. It has for me.
Re: Easy help but can't figure it out.
Quote:
Originally Posted by
weezer562
It is actually stopping at the + symbol and then adding the nextInt this does work , my problem is adding subtraction to the mix. I have tried adding an If statementwithin the while loop , but with no luck not sure what the conditional statement would be.
Ok, well you have used + has a delimiter but you want both. I would do this a different way.
NOTE: But for my way to work, there needs to be 1 requirement. There must be a space before and after the minus or plus sign. With some trickery, that can probably be fixed, but I'm not wanting to go so in depth.
I would read in the line instead of each int. That would return to you a String. You can then use the String.split(" ") method. That will return a String[] where each index is either a number or a plus or minus. So, you would only need a conditional statement to see if a value is a plus or minus symbol. If it isn't either, then you know it is a number. Even better than that, you know that every even index will be a number and every odd index will be a mathematical symbol. Here is my code (please don't use for class, but instead as an alternative way of doing it):
Code java:
import java.util.Scanner;
public class TestRead
{
public static void main(String[] args)
{
//Create Scanner Object
Scanner kb = new Scanner(System.in);
//Prompt User
System.out.print("Enter something like 8 + 33 + 1345 + 137: ");
//Get User Input
String s = kb.nextLine();
//Create split of User Input
String[] line = s.split(" ");
//Results Variable
int result = 0;
//Boolean to tell us to add or subtract
boolean plus = true;
//Loop through User Input split
for(int i=0;i<line.length;i++)
{
//Check if this index is odd for increased efficency
if(i%2==1)
{
//Check if this index holds a plus sign
if(line[i].equals("+"))
plus = true;
//Check if this index holds a minus sign
else if(line[i].equals("-"))
plus = false;
}
//If this is an even index
else
{
//If we want to add numbers
if(plus)
result += Integer.parseInt(line[i]);
//If we want to subtract numbers
else
result -= Integer.parseInt(line[i]);
}
}
//Print out result
System.out.println(result);
}
}
Re: Easy help but can't figure it out.
Completely agree with you, but my assignment was to get both addition and subtraction working using scanner methods. I believe its being used to get us accustomed to this. I really appreciate all the input. :)
Re: Easy help but can't figure it out.
Quote:
Originally Posted by
weezer562
Completely agree with you, but my assignment was to get both addition and subtraction working using scanner methods. I believe its being used to get us accustomed to this. I really appreciate all the input. :)
Ok, well we can modify this to do something similar. Once again, if there are spaces between the numbers and symbols, we can use the next() method while the scanner hasNext(). Then, if you keep a count of how many times you call that next() method, you can tell if it is a symbol or number (even are numbers and odd are symbols, starting from 0). You can then do basically the same thing I did above.
That way we are using the scanner methods but still doing the same thing technically.