Results 1 to 6 of 6
  1. #1
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default 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

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    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;

  3. #3
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default

    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);
    			}
    	}
    }

  4. #4
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default

    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.

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    This is correct use a while loop to get all the information

    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;
    }
    This way you could input any "correct" string and it would still work for any length of string.

  6. #6
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default

    thanks mate, i just broke down and used a JOptionPane lol. It works that way by using String[] input = input1.split("\\s");

Similar Threads

  1. Getting tokens using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:11 AM
  2. Using Scanner class to read int
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:50 AM
  3. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  4. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •