The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with a basic JAVA program for class-

3 replies on 1 page. Most recent reply: Oct 20, 2005 10:45 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
jimbo fisher

Posts: 4
Nickname: r0punk
Registered: Oct, 2005

Help with a basic JAVA program for class- Posted: Oct 20, 2005 4:02 PM
Reply to this message Reply
Advertisement

import java.lang.String;
import java.util.Scanner;

class Project5B
{
public static void main(String[] args)
{
//1. Set up all variables(set some values of the variables)
int numOfA=0; //a,A
int numOfE=0; //e,E
int numOfI=0; //i,I
int numOfO=0; //o,O
int numOfU=0; //u,U
int numOfOther=0; //other characters
int control=1; //control is the variable used with the while loop
int i=0; //i is the variable used to count in the char.At if statement
String randomString=" "; //user input string
char charLetter; //sets up the char for the for and nested ifs

//2. Set up the loop for user end function
do
{
//3. Set up the scanner and scan the string from user
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string: ");
randomString = scan.next();

//4. Determine the various information about the string
for (int letter = 0; letter <= randomString.length(); letter++)
{
charLetter = randomString.charAt(letter);

if(charLetter == 'a' || charLetter == 'A')
{
numOfA++;
}
else if(charLetter == 'e' || charLetter == 'E')
{
numOfE++;
}
else if(charLetter == 'i' || charLetter == 'I')
{
numOfI++;
}
else if(charLetter == 'o' || charLetter == 'O')
{
numOfO++;
}
else if(charLetter == 'u' || charLetter == 'U')
{
numOfU++;
}
else
{
numOfOther++;
}

//5. Print that which was determined
System.out.println("\nNo. of 'A' / 'a' = " +numOfA);
System.out.println("No. of 'E' / 'e' = " +numOfE);
System.out.println("No. of 'I' / 'i' = " +numOfI);
System.out.println("No. of 'O' / 'o' = " +numOfO);
System.out.println("No. of 'U' / 'u' = " +numOfU);
System.out.println("No. of non-vowels = " +numOfOther);

//6. Scan for the user end function interger
System.out.println("If you would like to run the program for another string please enter 1.");
control = scan.nextInt();
}while(control==1);
}
}



i get the following run time error after a correct compile

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(Unknown Source)


i have been sick and absent from school with mono for two weeks and i'm trying to teach myself but its been to hard and i'm trying to find some help

thanks in advance
-rhoades


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Help with a basic JAVA program for class- Posted: Oct 20, 2005 7:34 PM
Reply to this message Reply
There are three changes:
1. For your exception: use < instead of <= in for loop condition. The changed for loop is:
      for (int letter = 0; letter < randomString.length(); letter++) {
 // your same code goes here
}


2. In case user wants to start the program again by typing1, you need to reinitialize all characters count to zero. I placed that code just after
do { ...

3. You had some messed up braces at the end of while(control == 1 ) ... I fixed that one too.

So, here is you working code.


package test;
 
import java.util.Scanner;
 
class Project5B {
  public static void main(String[] args) {
    //1. Set up all variables(set some values of the variables)
    // These variables are moved inside do-while loop so that if
    // user wants to start over by typing 1 then all of these values
    // are reinitialized to zero
    int numOfA = 0; //a,A
    int numOfE = 0; //e,E
    int numOfI = 0; //i,I
    int numOfO = 0; //o,O
    int numOfU = 0; //u,U
    int numOfOther = 0; //other characters
 
    int control = 1; //control is the variable used with the while loop
    int i = 0; //i is the variable used to count in the char.At if statement
    String randomString = " "; //user input string
    char charLetter; //sets up the char for the for and nested ifs
 
    //2. Set up the loop for user end function
    do {
      numOfA = 0; //a,A
      numOfE = 0; //e,E
      numOfI = 0; //i,I
      numOfO = 0; //o,O
      numOfU = 0; //u,U
      numOfOther = 0; //other characters
 
      //3. Set up the scanner and scan the string from user
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a string: ");
      randomString = scan.next();
 
      //4. Determine the various information about the string
      for (int letter = 0; letter < randomString.length(); letter++) {
        charLetter = randomString.charAt(letter);
 
        if (charLetter == 'a' || charLetter == 'A') {
          numOfA++;
        }
        else if (charLetter == 'e' || charLetter == 'E') {
          numOfE++;
        }
        else if (charLetter == 'i' || charLetter == 'I') {
          numOfI++;
        }
        else if (charLetter == 'o' || charLetter == 'O') {
          numOfO++;
        }
        else if (charLetter == 'u' || charLetter == 'U') {
          numOfU++;
        }
        else {
          numOfOther++;
        }
      }
 
      //5. Print that which was determined
      System.out.println("\nNo. of 'A' / 'a' = " + numOfA);
      System.out.println("No. of 'E' / 'e' = " + numOfE);
      System.out.println("No. of 'I' / 'i' = " + numOfI);
      System.out.println("No. of 'O' / 'o' = " + numOfO);
      System.out.println("No. of 'U' / 'u' = " + numOfU);
      System.out.println("No. of non-vowels = " + numOfOther);
 
      //6. Scan for the user end function interger
      System.out.println(
          "If you would like to run the program for another string please enter 1.");
      control = scan.nextInt();
    }
    while (control == 1);
    // {
    //  ;
    //}
  }
}

jimbo fisher

Posts: 4
Nickname: r0punk
Registered: Oct, 2005

Re: Help with a basic JAVA program for class- Posted: Oct 20, 2005 8:35 PM
Reply to this message Reply
Thanks so much for your help. I had one more problem coming up with spaces but i fixed it by adding the bolded else if

I genuinely appreciate the help, thanks again :)


import java.util.Scanner;

class Project5B
{
public static void main(String[] args)
{
//1. Set up all variables(set some values of the variables)
int control = 1; //control is the variable used with the while loop
int i = 0; //i is the variable used to count in the char.At if statement
String randomString = " "; //user input string
char charLetter; //sets up the char for the for and nested ifs

//2. Set up the loop for user end function
do
{
int numOfA = 0; //a,A
int numOfE = 0; //e,E
int numOfI = 0; //i,I
int numOfO = 0; //o,O
int numOfU = 0; //u,U
int numOfOther = 0; //other characters

//3. Set up the scanner and scan the string from user
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string: ");
randomString = scan.next();

//4. Determine the various information about the string
for (int letter = 0; letter < randomString.length(); letter++)
{
charLetter = randomString.charAt(letter);

if (charLetter == 'a' || charLetter == 'A')
{
numOfA++;
}
else if (charLetter == 'e' || charLetter == 'E')
{
numOfE++;
}
else if (charLetter == 'i' || charLetter == 'I')
{
numOfI++;
}
else if (charLetter == 'o' || charLetter == 'O')
{
numOfO++;
}
else if (charLetter == 'u' || charLetter == 'U')
{
numOfU++;
}
[b] else if (charLetter == ' ')
{
numOfOther++;
}[/b]
else
{
numOfOther++;
}
}

//5. Print that which was determined
System.out.println("\nNo. of 'A' / 'a' = " + numOfA);
System.out.println("No. of 'E' / 'e' = " + numOfE);
System.out.println("No. of 'I' / 'i' = " + numOfI);
System.out.println("No. of 'O' / 'o' = " + numOfO);
System.out.println("No. of 'U' / 'u' = " + numOfU);
System.out.println("No. of non-vowels = " + numOfOther);

//6. Scan for the user end function interger
System.out.println("If you would like to run the program for another string please enter 1.");
control = scan.nextInt();
}while (control == 1);
}
}




-rhoades

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help with a basic JAVA program for class- Posted: Oct 20, 2005 10:45 PM
Reply to this message Reply
Just use the java tags as shown in the "formating your code" section before and after your java code to make it look readable.

Flat View: This topic has 3 replies on 1 page
Topic: need help with concept of writing A Class Previous Topic   Next Topic Topic: Is JAVA a Language ??

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use