• Post Reply Bookmark Topic Watch Topic
  • New Topic

A very basic code problem  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I would like to know why the first chapter of my text has this code basically the same as I present to you, yet I have only been able to have JGrasp freeze, I also got a string of errors.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 98098
at java.lang.String.substring(String.java:1950)
at java.util.Scanner.buildIntegerPatternString(Scanner.java:423)
at java.util.Scanner.integerPattern(Scanner.java:443)
at java.util.Scanner.nextInt(Scanner.java:2117)
at easydoesit.main(easydoesit.java:16)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete


These console scanner things are giving me trouble. they all compile but dont work.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you enter in console?

BTW, please UseCodeTags when posting. I added them for you this time.
Also, please try to write full question in one post. I merged your three posts into one.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

Why you give nextInt parameter 2 ?
You can use Scanner class as following :

 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As in your other query, you are using a parameter to nextInt(), which is the radix. If you pass 2 then it expects the number to be in binary. Leave the parameter out and it will read the number in decimal.
 
Pat Steele
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner scan = new Scanner(System.in);
String s = scan.next(); // to get string
int i = scan.nextInt();

I have no idea what this does as far as input or output. If someone could inform me as to why the scan class is even used that would be great. The only idea i have right now is to pop in numbers in the parenthesis. i know that is an error, so what does String =scan.next(); do ??
 
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first step when I don't know what a class does would be to check out the Java API Documentation
You can read all about java.util.Scanner
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation shows you some simple examples; you can read a bit more in the Java® Tutorials. There are problems with the nextLine method, which I have never seen discussed in a tutorial.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!