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

  • Home
  • SEARCH
  • 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 8573623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:17:40+00:00 2026-06-11T19:17:40+00:00

I am working on a class assignment this morning and I want to try

  • 0

I am working on a class assignment this morning and I want to try and solve a problem I have noticed in all of my team mates programs so far; the fact that spaces in an int/float/double cause Java to freak out.

To solve this issue I had a very crazy idea but it does work under certain circumstances. However the problem is that is does not always work and I cannot figure out why. Here is my “main” method:

import java.util.Scanner;    //needed for scanner class

public class Test2
{
  public static void main(String[] args) 
  {

      BugChecking bc = new BugChecking();
      String i;
      double i2 = 0;

      Scanner in = new Scanner(System.in);   

      System.out.println("Please enter a positive integer");

      while (i2 <= 0.0)
      {
             i = in.nextLine();

             i = bc.deleteSpaces(i);  

            //cast back to float
            i2 = Double.parseDouble(i);

            if (i2 <= 0.0)
            {
                System.out.println("Please enter a number greater than 0.");
            }
      }

      in.close();
      System.out.println(i2);       
 }
}

So here is the class, note that I am working with floats but I made it so that it can be used for any type so long as it can be cast to a string:

public class BugChecking
{
    BugChecking()
    {

    }

    public String deleteSpaces(String s)
    {  

        //convert string into a char array
        char[] cArray = s.toCharArray();

        //now use for loop to find and remove spaces
        for (i3 = 0; i3 < cArray.length; i3++)
        {
            if ((Character.isWhitespace(cArray[i3])) && (i3 != cArray.length)) //If current element contains a space remove it via overwrite
            {
                for (i4 = i3; i4 < cArray.length-1;i4++)
                {
                    //move array elements over by one element
                    storage1 = cArray[i4+1];
                    cArray[i4] = storage1;                  
                }
            }
        }

        s = new String(cArray);

        return s; 
    }  

    int i3; //for iteration
    int i4; //for iteration
    char storage1; //for storage
}

Now, the goal is to remove spaces from the array in order to fix the problem stated at the beginning of the post and from what I can tell this code should achieve that and it does, but only when the first character of an input is the space.

For example, if I input ” 2.0332″ the output is “2.0332”.

However if I input “2.03 445 ” the output is “2.03” and the rest gets lost somewhere.

This second example is what I am trying to figure out how to fix.

EDIT:

David’s suggestion below was able to fix the problem. Bypassed sending an int. Send it directly as a string then convert (I always heard this described as casting) to desired variable type. Corrected code put in place above in the Main method.

A little side note, if you plan on using this even though replace is much easier, be sure to add an && check to the if statement in deleteSpaces to make sure that the if statement only executes if you are not on the final array element of cArray. If you pass the last element value via i3 to the next for loop which sets i4 to the value of i3 it will trigger an OutOfBounds error I think since it will only check up to the last element – 1.

  • 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-06-11T19:17:41+00:00Added an answer on June 11, 2026 at 7:17 pm

    If you’d like to get rid of all white spaces inbetween a String use replaceAll(String regex,String replacement) or replace(char oldChar, char newChar):

        String sBefore = "2.03 445 ";
    
        String sAfter = sBefore.replaceAll("\\s+", "");//replace white space and tabs
        //String sAfter = sBefore.replace(' ', '');//replace white space only
        double i = 0;
    
        try {
            i = Double.parseDouble(sAfter);//parse to integer
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
        System.out.println(i);//2.03445
    

    UPDATE:

    Looking at your code snippet the problem might be that you read it directly as a float/int/double (thus entering a whitespace stops the nextFloat()) rather read the input as a String using nextLine(), delete the white spaces then attempt to convert it to the appropriate format.

    This seems to work fine for me:

    public static void main(String[] args) {
    
        //bugChecking bc = new bugChecking();
        float i = 0.0f;
        String tmp = "";
    
        Scanner in = new Scanner(System.in);
    
        System.out.println("Please enter a positive integer");
    
        while (true) {
    
            tmp = in.nextLine();//read line
            tmp = tmp.replaceAll("\\s+", "");//get rid of spaces
    
            if (tmp.isEmpty()) {//wrong input
                System.err.println("Please enter a number greater than 0.");
            } else {//correct input
    
                try{//attempt to convert sring to float
                i = new Float(tmp);
                }catch(NumberFormatException nfe) {    
                    System.err.println(nfe.getMessage());
                }
    
                System.out.println(i);
                break;//got correct input halt loop
            }
    
        }
        in.close();
    }
    

    EDIT:

    as a side note please start all class names with a capital letter i.e bugChecking class should be BugChecking the same applies for test2 class it should be Test2

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

Sidebar

Related Questions

This is an assignment i am working on for class. To make a long
I'm working on a assignment for class so this is only part of the
I have been working on and off on an assignment for my C class
Working on a class assignment and when I run this code function paycheck1($hours,$payrate,$othours,$otrate){ if
ok so this is the assignment that I'm working on: Implement a class Student
Working on my assignment for Java and I have created a class called Triangle.
I have been working on this all day, much of my code is from
I've been butting my head against this problem in an assignment I've been working
I'm working on a class assignment that started small, so I had it all
I am working on a homework assignment for a class and I have solved

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.