Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:12:45+00:00 2026-05-28T19:12:45+00:00

I am trying to test if my user submits sensible data , which is

  • 0

I am trying to test if my user submits sensible data , which is later formatted to integer.
Where is the problem with the switch statement? 🙂

void convert(String str)
{
    int i=0;
    String x=str.startsWith();
    switch (x) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 0:
            int i = Integer.parseInt(str);
            break;
        default:
            System.out.println ("Should start with fixnumber");
    }
    System.out.println (i);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:12:46+00:00Added an answer on May 28, 2026 at 7:12 pm

    You’re switching on x which is a String – unless you’re using Java 7, you can’t use String in a Switch statement.

    I expect the error is actually coming from str.startsWith(), where that method is expecting to take a String (which you’re checking what it starts with) and returns a boolean, which you can’t switch on either.

    UPDATE Correcting your code to do what I think you’re trying to do:

    void convert(String str)
    {
        int i = 0;
        switch (str.charAt(0)) {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '0':
                i = Integer.parseInt(str);
                break;
            default:
                System.out.println ("Should start with fixnumber");
        }
        System.out.println (i);
    }
    

    Although the shorter way is just to do the Integer.parseInt call, and handling the NumberFormatException that may occur – then you don’t need to do the switch at all.

    void convert(String str)
    {
        try {
            int i = Integer.parseInt(str);
            System.out.println (i);
        } catch (NumberFormatException e) {
            System.out.println ("Should start with fixnumber");
        }
    }
    

    You need to either return i; and convert the method signature from void to int, or otherwise expose the data in i to make it worthwhile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to test my successfully creates a new user after login (using authlogic).
I'm trying to test an OpenFileDialog that is created when the user clicks on
I'm trying to test that a UserProfile model is created as a new User
Trying to create a user account in a test. But getting a Object reference
I am trying to test the likelihood that a particular clustering of data has
I'm trying to pass user input form data jquery/ajax to check if there's any.
I am trying to test the user agent to set a variable with apache
I'm trying to capture the queries which my code submits to the database by
I'm trying to expect an error in an rspec test. lambda {Participant.create!({:user_id => three.id,
just trying to test for equality in this piece of code, but getting a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.