0

I'm trying to use this string

amountStr = amountStr.replace("$", "").replace(" ", "").replace(",", ""); 

but I get an error message which I know I'm getting because that string I just posted is outdated. So I was wondering what would be a good updated version of that string? here is how my whole code looks like.

import java.util.Scanner;

public class QuarterMachine{

    /**
     * Convert the input to cents.
     */

    public static void main(String[] args){

        // PART I INPUT SECTION (COMPLETE)
        // DO NOT CHANGE THESE LINES

        Scanner cin = new Scanner(System.in);
        System.out.print("Part I-Enter the amount input ($ 1,572.52): ");
        String amountStr = cin.nextLine();

        // PLACE YOUR CODE ONLY BELOW THIS LINE
        // PLACE YOUR OUTPUT IN THESE VARIABLES

        // ---------- YOUR CHANGES HERE ----------------------

        double amount = 0;
        int quarters = 0;

//My problem is in line below
        amountStr = amountStr.replace("$", "").replace(" ", "").replace(",", "");

        amount = Double.parseDouble(amountStr);
        quarters = (int) (amount * 100) / 25;

        // PLACE YOUR CODE ONLY ABOVE THIS LINE
        // PART I OUTPUT SECTION COMPLETE

        System.out.printf("Amount received($%,.0f), quarters returned(%,d)%n%n",amount, quarters);

        // PART II INPUT SECTION (COMPLETE)

        System.out.print("Part II-Enter seconds, minutes and hours as integers: ");
        int seconds = cin.nextInt();
        int minutes = cin.nextInt();
        int hours = cin.nextInt();

        // PLACE YOUR CODE ONLY BELOW THIS LINE
        // PLACE YOUR OUTPUT IN THIS VARIABLE

        String result = String.format("%02d:%02d:%02d", hours, minutes, seconds);

        // YOUR CODE MUST GO ABOVE THIS LINE
        // PART II - OUTPUT SECTION COMPLETE

        System.out.println("Result: " + result);
    }
}
scrappedcola
  • 10,423
  • 1
  • 32
  • 43
George Gonzalez
  • 1
  • 1
  • 1
  • 2
  • I edited your code to add the CODE tag. Please, always use it in your posts. Welcome to SO. – AndreDuarte Oct 02 '14 at 18:40
  • I would also recommend removing all those line breaks. it makes your code very hard to read. Rather than typing in line numbers, simply add a comment to the line where the issue occurs to indicate where we should look for the error. – Ideasthete Oct 02 '14 at 18:47
  • 1
    In "... because that string I just posted is outdated" what do you mean by "that string" and by "outdated"? – Pshemo Oct 02 '14 at 18:53
  • What are you feeding to `amountStr = cin.nextLine()` statement? – Tirath Oct 02 '14 at 18:54
  • what version of java are you using and are you using Eclipse? – scrappedcola Oct 02 '14 at 18:56
  • I'm using dr.java. Eclipse compiler 0.A48 – George Gonzalez Oct 02 '14 at 19:00
  • You might check this [thread](http://sourceforge.net/p/drjava/bugs/944/) There is an issue with Dr. Java and certain version of eclipse. Ensure you have JDK installed and not just JRE. – scrappedcola Oct 02 '14 at 19:01
  • 1
    I like how @Pshemo's edit changed the spelling of 'message' to the wrong one, when it was right before. As for OP's question, you have an issue with your environment; this code runs fine for me. – bcsb1001 Oct 02 '14 at 19:02
  • if you have problems with eclipse, you really should include eclipse tag in the question... – eis Oct 03 '14 at 23:14
  • 1
    In case you are using jdk or jre 8 consider [this answer](http://stackoverflow.com/a/26105217/2711488) – Holger Dec 08 '14 at 20:35
  • 1
    This is most likely "just" because you are using Java 8; see http://stackoverflow.com/questions/24301986/the-type-java-lang-charsequence-cannot-be-resolved-in-package-declaration. – vorburger Feb 16 '15 at 17:01

2 Answers2

4

I think this is some kind of a bug in Eclipse. You can fix this by trying each solution written below. If one doesn't work try the next one.

Hope this helps.

0

Probably you will face this error after upgrading to JDK 8 as some old interfaces now have new default methods in 1.8.

  • So first verify that Your eclipse supports jdk 1.8, eclipse should be greater than or equal to 4.3.2(Eclipse Kepler SR2).
  • Eclipse complains this error if source level of your project is lower than 1.8, so set source level to 1.8 then complier will not through any exception.
  • Third option is, rollback to JDK 7 to use old interfaces.

Hope this will helps

Vaibhav Panmand
  • 349
  • 4
  • 10