Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Question String to int troubles

    Hello forums, I am new here, so hopefully you get what i am trying to ask. I am currently creating a program that simulates a simple computer. It does this by having the user input pseudo-assembly code. I am using the Scanner to catch this input. The input needs to be formatted like this.

    00 ? +1007

    What I need to do with this is get the 00 and the +1007 and store them in separate ints, but I am having trouble figuring out exactly how to do this.
    Any help would be appreciated.
    The Chuckling Atom

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: String to int troubles

    The problem is the + is not a numeric digit. You need to strip that off before you can use the Integer class's parse method.
    You could use the String class's split() method to parse the Strings if you can work out the regular expression to use. Something like this: " \\? \\+"

  3. #3
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: String to int troubles

    Thanks for the suggestion. I have been looking at the String's split() method because that seems to be the best option right now, and i am confused on how to make it store both halves in the array. i can get it to split the 00 off and store that, but i cannot get it to store anything else.
    the code i am using to do that is this. It has java.lang.String imported.

    Scanner userInput = new Scanner(System.in);
    String[] splitTest = new String[3];
    String inputS = userInput.next();

    for (int i = 0; i < 3; i++)
    {
    System.out.println(splitTest[i]);
    }

    any ideas?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: String to int troubles

    I don't see where you are using the String class's split method in the code you posted.
    Your posted code does not put any values into the splitTest array.
    The one value you put into the inputS variable is ignored.

  5. #5
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: String to int troubles

    derp. I forgot before the for there is supposed to be this line

    splitTest[] = inputS.split(\\? \\+);

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: String to int troubles

    Can you show the input to and the output for your test?

  7. #7
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: String to int troubles

    yes this is what i input

    00 ? +1007
    and this is the output

    00
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at simpletron.Simpletron.main(Simpletron.java:41)
    Java Result: 1

    41 is the system.out in the for loop

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: String to int troubles

    You didn't post the code that you execute. You have bits and pieces of code laying around here. It is important that you post the EXACT code that you execute.

    Add a println in your code that shows what the program read into the String variable.
    Why did you get the ArrayIndexOutOfBoundsException?
    You should use the .length attribute of the array to control how many times you loop.

  9. #9
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: String to int troubles

    Java Code:
    package simpletron;
    import java.util.Scanner;
    import java.lang.Integer;
    import java.lang.String;
    /**
     *
     * @author Chuckling Atom
     */
    public class Simpletron {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            Scanner userInput = new Scanner(System.in);
            int[] SimpleMacLanguage = new int[100];
            String inputS;
            int input = 0;
            int temp = 0;
            
            System.out.println("*** Welcome to the Simpletron! ***\n*** Please"
                    + " enter your program one instruction ***\n*** (or data"
                    + " world) at a time. I will display ***\n*** the location"
                    + "number and a question mark (?) ***\n*** You then type the"
                    + "word for that location. ***\n*** Type -99999 to stop "
                    + "entering your program. ***");
            
            String[] splitTest = new String[3];
            
            inputS = userInput.next();
            
            splitTest = inputS.split("\\? \\+");
            
            for (int i = 0; i < 3; i++)
            {
                System.out.println(splitTest[i]);
                
            }
    
            
        }
    }
    that is the exact code i am currently using

  10. #10
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: String to int troubles

    Java Code:
    package simpletron;
    import java.util.Scanner;
    import java.lang.Integer;
    import java.lang.String;
    /**
     *
     * @author Chuckling Atom
     */
    public class Simpletron {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            Scanner userInput = new Scanner(System.in);
            int[] SimpleMacLanguage = new int[100];
            String inputS;
            int input = 0;
            int temp = 0;
            
            System.out.println("*** Welcome to the Simpletron! ***\n*** Please"
                    + " enter your program one instruction ***\n*** (or data"
                    + " world) at a time. I will display ***\n*** the location"
                    + "number and a question mark (?) ***\n*** You then type the"
                    + "word for that location. ***\n*** Type -99999 to stop "
                    + "entering your program. ***");
            
            String[] splitTest = new String[3];
            
            inputS = userInput.next();
            
            splitTest = inputS.split("\\? \\+");
            
            for (int i = 0; i < 3; i++)
            {
                System.out.println(splitTest[i]);
                
            }        
            
        }
    }
    this is the code i am currently using

  11. #11
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: String to int troubles

    I apologize for the double post

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: String to int troubles

    You don't show the console from when you execute the code. You need to add a println to output what was read in from the user. Or better yet, put the the test value in your program:
    inputS = "THE TEST STRING HERE"; //userInput.next();

    On line 35: You should use the .length attribute of the array to control how many times you loop. Don't hard code number like 3!!! You do NOT know the size of the array the split method returns.

  13. #13
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: String to int troubles

    Quote Originally Posted by Chuckling_atom View Post
    Java Code:
    package simpletron;
    import java.util.Scanner;
    import java.lang.Integer;
    import java.lang.String;
    /**
     *
     * @author Chuckling Atom
     */
    public class Simpletron {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            Scanner userInput = new Scanner(System.in);
            int[] SimpleMacLanguage = new int[100];
            String inputS;
            int input = 0;
            int temp = 0;
            
            System.out.println("*** Welcome to the Simpletron! ***\n*** Please"
                    + " enter your program one instruction ***\n*** (or data"
                    + " world) at a time. I will display ***\n*** the location"
                    + "number and a question mark (?) ***\n*** You then type the"
                    + "word for that location. ***\n*** Type -99999 to stop "
                    + "entering your program. ***");
            
            String[] splitTest = new String[3];
            
            inputS = "00 ? +1007" //userInput.next();
            
            splitTest = inputS.split("\\? \\+");
            
            for (int i = 0; i < splitTest.length; i++)
            {
                System.out.println(splitTest[i]);
                
            }        
            
        }
    }
    This is the code using what you suggested.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: String to int troubles

    Where is the output from when you execute the program that shows the problem?

Similar Threads

  1. Troubles with a Change app
    By kiLzeD in forum New To Java
    Replies: 2
    Last Post: 09-29-2011, 12:29 AM
  2. I have troubles with this code ..!
    By Veton in forum Eclipse
    Replies: 4
    Last Post: 06-05-2011, 06:51 PM
  3. Textpad troubles
    By Sthassaris in forum Other IDEs
    Replies: 3
    Last Post: 06-03-2011, 10:38 PM
  4. Gif decoding/LZW troubles
    By hellochar in forum Advanced Java
    Replies: 2
    Last Post: 07-15-2009, 12:26 AM
  5. Image troubles
    By Theodoreb in forum New To Java
    Replies: 24
    Last Post: 07-14-2009, 01:41 AM

Tags for this Thread

Posting Permissions

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