Results 1 to 6 of 6
Thread: Scanner class question
- 04-24-2008, 07:01 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
Scanner class question
ok, so i want to input a Math equation say 3 * 2. Now if i remember right the * either comes out as a string or char. I was curious how to store this into a single array to be used in a switch case
now using a String[] just throws out incompatible types on the 3 and 2.... while int[] throws out incompatible types on the *. I didn't see anywhere in any documentation for the scanner class where you can do something along these lines char[] = scanner.nextChar(); Any ideas? and no parsing out each number etc wasn't an option in this lab smile.gif
- 04-24-2008, 07:19 PM #2
What I would do is something along the lines of 3 * 4 then when the user presses enter you take that string in and then just use the String class to parse it. For Example: Sorry I don't quite remember the scanner class so forgive me
Java Code:String tempStr = What ever way you get the String. int tempInt = tempStr.getNextInt(); // I think this is right check the api if(tempStr.getNextChar.equals(*) //change your switch variable // then your switch statement case whatever: tempInt = tempInt * tempStr.getNextInt(); break;
- 04-24-2008, 07:29 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
i have thought about doing it this way; however, it would require me to parse out the input instead of setting it equal to a single string. Now, i know that i can do it by using String[] args and doing comand line running as in:
java lab6 3 *4
but i need it to be an input that is presented after the program loads. It also needs to go into a single array to be used in the switch case
code:
Java Code:/*James Bridgeford Lab 6 this lab throws exceptions when a user imputs wrong input */ import javax.swing.*; import java.lang.*; import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import java.util.Scanner; public class lab6 { /** main method no constructors **/ public static void main(String[] args) throws IllegalArgumentException{ //create the scanner Scanner scanner = new Scanner(System.in); //get input from user System.out.print("Enter an operation I.E 3 * 2"); char[] input = scanner.next(); /*for (int i=0;i<input1.length;i++){ String[] input = String.parseString(input1); }*/ if (input.length != 3) throw new IllegalArgumentException("Wrong Input: " + input[0]); else { int result = 0; switch(args[1].charAt(0)){ case '+': result = (input[0]) + (input[2]); break; case '-': result = (input[0]) - (input[2]); break; case '*': result = (input[0]) * (input[2]); break; case '/': result = (input[0]) / (input[2]); break; } System.out.println(input[0] + ' ' + input[1] + ' ' + input[2] + "=" + result); } } }
- 04-24-2008, 10:49 PM #4
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
ok so basically I found out that using scanner.next() is only grabbing 5 instead of 5 * 3. if i try putting it in quotes it only grabs "5. if i try using single quotes it just grabs '5. I'm at a loss as to how to grab the whole input.
- 04-24-2008, 11:09 PM #5
This is correct use a while loop to get all the information
This way you could input any "correct" string and it would still work for any length of string.Java Code:String tempStr = What ever way you get the String. while(tempStr != null) { int tempInt = tempStr.getNextInt(); // I think this is right check the api if(tempStr.getNextChar.equals(*) //change your switch variable // then your switch statement case whatever: tempInt = tempInt * tempStr.getNextInt(); break; }
- 04-25-2008, 12:41 AM #6
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Getting tokens using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:11 AM -
Using Scanner class to read int
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:50 AM -
Scanner class
By ajaymenon.k in forum Advanced JavaReplies: 1Last Post: 11-26-2007, 07:01 AM -
JDK 5.0 Scanner Class
By Sircedric88 in forum New To JavaReplies: 3Last Post: 07-27-2007, 06:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks