Results 1 to 14 of 14
Thread: String to int troubles
- 11-26-2011, 02:38 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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
- 11-26-2011, 02:44 AM #2
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: " \\? \\+"
- 11-26-2011, 06:28 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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?
- 11-26-2011, 02:14 PM #4
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.
- 11-27-2011, 12:13 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: String to int troubles
derp. I forgot before the for there is supposed to be this line
splitTest[] = inputS.split(\\? \\+);
- 11-27-2011, 12:15 AM #6
Re: String to int troubles
Can you show the input to and the output for your test?
- 11-27-2011, 12:23 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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
- 11-27-2011, 12:36 AM #8
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.
- 11-27-2011, 12:43 AM #9
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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]); } } }
- 11-27-2011, 12:45 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
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]); } } }
- 11-27-2011, 06:38 AM #11
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: String to int troubles
I apologize for the double post
- 11-27-2011, 01:56 PM #12
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.
- 11-27-2011, 07:34 PM #13
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
- 11-27-2011, 07:38 PM #14
Similar Threads
-
Troubles with a Change app
By kiLzeD in forum New To JavaReplies: 2Last Post: 09-29-2011, 12:29 AM -
I have troubles with this code ..!
By Veton in forum EclipseReplies: 4Last Post: 06-05-2011, 06:51 PM -
Textpad troubles
By Sthassaris in forum Other IDEsReplies: 3Last Post: 06-03-2011, 10:38 PM -
Gif decoding/LZW troubles
By hellochar in forum Advanced JavaReplies: 2Last Post: 07-15-2009, 12:26 AM -
Image troubles
By Theodoreb in forum New To JavaReplies: 24Last Post: 07-14-2009, 01:41 AM
Bookmarks