• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner hasNext() and indexoutofboundsexception

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I need some help here. So this program takes an input file that I will post at the bottom with ints.

Assignment: Write a program that inputs a list of numbers that are arranged into groups of various sizes. The program outputs the sum of the numbers in each group. Each group starts with a one-line descriptive phrase. The phrase can be anything that is not a number. Some groups may have zero number in them.

Problem: Everything runs fine at first and I get my first group sum of -44 which is correct but then i get this
I'm getting an StringIndexOutOfBoundsException String index out of range: 0 at java.lang.String.charAt(unknown source)




Input file:

Group A
23
-12
29
-84
Group B
-2
-45
-90
123
26
19
-5
-30
9
Group C
Last Group
12
-34
23
47
52
8

Solution should be:

Group 1
Sum = -44

Group 2
Sum = 5

Group 3
Sum = 0

Group 4
Sum = 108

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put some System.out.printf calls in for debugging. You may be getting a 0-length line. That is an obscure problem, which isn't well described in the books I have read. Ignore the rants, and have a look at what one of our more disreputable members said about that problem last year.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.

Try to give
scan.nextLine() once after
System.out.println("Group " + count + " equals " + sum);

check its working or not.. I believe you will be able to identity the error after that(line 27 if i am correct) and give the reason for that(as details given by Wouter).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic