Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My program should ask the user for the name, the price as well as the amount of what he wants, until he presses "x". At this point the program should print the receipt.
How can I save all the user inputs without knowing how many items he wishes to buy?
I am using three classes. I do not know how to give the names, amounts, prices from the Main-class to the class Eintrag.
How do I call the list from the Kassenzettel class in the main for me to print?
Also, I override the to String method, for my receipt to "supermarket-like", will this formatting be applied in the main class?
here is my main -class:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("What would you like? ");
String produkt = scanner.nextLine();
System.out.println("How many pieces do you want?");
int anzahl = scanner.nextInt();
System.out.println("How much does " + produkt + "cost?");
double preis = scanner.nextDouble();
//Im not even sure if that should be there
Kassenzettel list =new Kassenzettel();
Eintrag carrots = new Eintrag("carrots", 5, 0.40);
Kassenzettel.add(carrots);
System.out.println("__________________________________");
System.out.println(" IHRE RECHNUNG ");
System.out.println("__________________________________");
}
}
My Eintrag-class:
public class Eintrag {
private String produkt;
private double preis;
private int anzahl;
public Eintrag(String produkt, int anzahl, double preis) {
this.anzahl=anzahl;
this.produkt=produkt;
this.preis=preis;
}
public int getAnzahl(){
return this.anzahl;
}
public double getPreis(){
return this.preis * this.anzahl;
}
public String getProdukt() {
return this.produkt;
}
public void setAnzahl(int anzahl) {
this.anzahl = anzahl;
}
public String toString() {
return (String.format("%-9s %2d x %5.2f EUR",produkt , anzahl, preis
+ "%30.2f EUR", anzahl * preis));
}
}
My Kassenzettel class, which should consist of a list of Eintrag objects
import java.util.ArrayList;
public class Kassenzettel {
private static ArrayList<Eintrag> kassenZettel;
private double summe;
// Constructs a new empty grocery list.
public static void add(Eintrag item) {
kassenZettel.add(item);
}
#Override
public String toString() {
return (String.format("%-9s %2d x %5.2f EUR",kassenZettel
+ "%30.2f EUR"
+ "SUMME", summe));
}
}
Not sure, if i got, what you want, but you can use something like this:
change the toString method of Kassenzettel to:
double sum = 0;
String ret = "";
for(int i = 0; i< kassenZettel.size(); i++){
ret += kassenZettel.get(i).toString()+"\r\n";
sum += kassenZettel.get(i).getPreis();
}
ret += "\r\nSumme: "+sum;
The next point, you have to make a loop for user input:
Scanner scanner = new Scanner(System.in);
//create an instance of kassenzettel to add your produkts to
Kassenzettel list =new Kassenzettel();
//set variable to check if read or not (not needed if using break!)
boolean readOn = true;
//loop for reading until user inputs x or X
while(readOn){
System.out.println("What would you like? ");
String produkt = scanner.nextLine();
//check if produkt name is X or x => exit the loop
if(produkt.equalsIgnoreCase("x")){
readOn = false;
break;
}
System.out.println("How many pieces do you want?");
int anzahl = scanner.nextInt();
System.out.println("How much does " + produkt + "cost?");
double preis = scanner.nextDouble();
//add a new item to kassenzettel
list.add(new Eintrag(produkt, anzahl, preis));
}
//print the output of kassenzettel
System.out.println(list.toString());
EDIT:
and remove:
//Im not even sure if that should be there
Kassenzettel list =new Kassenzettel();
Eintrag carrots = new Eintrag("carrots", 5, 0.40);
Kassenzettel.add(carrots);
Working code:
http://pastebin.com/3acgm5St
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So I have this code already pre-written for me
public class Car
{
private String make;
private int yearModel;
private double price;
private int mileage;
public Car (String mk, int year, double pr, int miles)
{
make = mk;
yearModel = year;
price = pr;
mileage = miles;
}
public String getMake()
{
return make;
}
public int getYearModel()
{
return yearModel;
}
public double getPrice()
{
return price;
}
public int getMileage()
{
return mileage;
}
public String toString()
{
return "Make: " + make+"\n"+"YearModel: " + yearModel +"\n"+
"Price: $" + price +"\n" + "Mileage: "+ mileage + " miles\n\n";
}
}
My task is to first prompt the user for the number of cars on a car lot. Then create an array of Car to hold the input number of cars. Then fill the array with the info of every car on the carlot. I know the general concept, but when I'm trying to input a String, it says a type mismatch with the array Cars only allowing for the type Car. I'm confused what that is and how to get around it. Here's my code.
import java.util.Scanner;
public class CarLot {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
System.out.println("Input number of cars: ");
Car [] Cars = new Car [input.nextInt()];
for(int i = 0; i < Cars.length; i++) {
System.out.println("Make");
Cars[i] = input.nextLine();
System.out.println("Model");
Cars[i] = input.nextLine();
System.out.println("Price");
Cars[i] = input.nextLine();
System.out.println("Mileage");
Cars[i] = input.nextLine();
System.out.println();
}
input.close();
}
}
It's important to note that Scanner .nextLine() does not return a Car, it returns a String. You need to make a Car out of the inputs.
So in the loop you should do something like this (I made the cars variable name lowercase to follow Java naming conventions):
System.out.println("Make");
String make = input.nextLine();
System.out.println("Model");
String model = input.nextLine();
System.out.println("Price");
double price = input.nextDouble();
System.out.println("Mileage");
int mileage = input.nextInt();
cars[i] = new Car(make, model, price, mileage);
I know that java can't normally return two values, but I'm trying to return the value as a method.. From there I want to use the values of score1 and maxScore1 in a string in the main method. getHwGrades is the method I'm having issues with. I get "error: cannot find symbol". I'm not allowed to use arrays for this program. On another note, I'm also not supposed to use any if/else statements, but I could not find any other way to limit the value of discussionGrade to 20. Any help is appreciated
import java.util.Scanner;
public class Grades
{
public static void main(String[] args)
{
Scanner getG = new Scanner(System.in);
int weight1;
int weight2;
int weight3;
int score2 = 0;
int maxScore2 = 0;
int sections;
int discussionGrade;
System.out.println("What is the weight for the HW and Exam 1?");
weight1 = getG.nextInt();
weight2 = getG.nextInt();
weight3 = getWeight(weight1, weight2);
System.out.println("Using weights of " + weight1 + " " + weight2 + " " + weight3);
getHwGrades();
sections = numberSections();
discussionGrade = calcDGrade(sections);
System.out.println("What is your grade for exam1?"); //move under hw total points and final grade, add curve
score2 = getG.nextInt();
maxScore2 = getG.nextInt();
System.out.println("Total Points =" + (score1+ discussionGrade) + "/ "(maxScore1 + 20));
}
public static int getHwGrades()//method for asking and storing hw grades
{
int score1;
int maxScore1;
int nOfA;
Scanner getG = new Scanner(System.in);
System.out.println("How many HW assignments are there?");
nOfA = getG.nextInt();
for (int i = 1; i <= nOfA; i++) //loop that asks for the grades corresponding to the amount of hw assignments there were
{
System.out.println("What is your grade and then the max grade for assignment " + i + "?");
score1 += getG.nextInt();
maxScore1 += getG.nextInt();
}
return new getHwGrade(score1, maxScore1); //returns results of inputs into method holding the 2 variables
}
public static int numberSections() //finds out how many sections the student attended
{
Scanner getG = new Scanner(System.in);
System.out.println("How many sections did you attend?");
return getG.nextInt();
}
public static int calcDGrade(int sections) //method to calculate the grade for the students sections
{
int maxDGrade = ((sections*4)); if (maxDGrade > 20) //limits total score to 20
{
return 20;
}
else
{
return maxDGrade;
}
}
public static int getWeight(int weight1, int weight2)//returns the weight that will be used for weight3
{
return (100-(weight1 + weight2));
}
public static double round2(double number)
{
return Math.round(number * 100.0) / 100.0;
}
}
Since you can only return a single value, you need to return a single value. :-) Normally if a method needs to return complex information, the way you have it do that is either:
Return a newly-created instance of a class that has fields for the individual pieces of information
Have the method fill in an instance of such a class that's passed to it (usually not ideal barring a good reason for doing it)
So for instance:
class HwGrades {
private int score1;
private int maxScore1;
ScoreInfo(int _score1, int _maxScore1) {
this.score1 = _score1;
this.maxScore1 = _maxScore1;
}
// ...accessors as appropriate, presumably at least getters...
}
Then return an instance of HwGrades from getHwGrades:
public static int getHwGrades()//method for asking and storing hw grades
{
int score1;
int maxScore1;
// ...
return new HwGrades(score1, maxScore1);
}
If you needed, you could further decouple things by making HwGrades an interface, which you then implement with a private class, so that the API isn't tied to a specific class. Almost certainly overkill for a small school project.
getHwGrade is expected to be a class.
Have a class like this
class getHwGrade{
int a;
int b;
public getHwGrade(int a,b){
this.a=a;this.b=b;
}
I have an ArrayList of FlowerClass objects. Each of these FlowerClass objects has a name. I want to go through the ArrayList and count them. I want to display the amount of each. So if I have three FlowerClass objects named Rose, two named Daffodil, and one named Tulip...I want to display the following:
Found 3 Rose
Found 3 Daffodil
Found 3 Tulip
So far, I've gotten it to count correctly using two functions I made. The problem is that I iterate through the entire ArrayList...so it'll show me the results more than once. For example, if the user adds 3 Roses and 2 Daffodils...The output is like this:
Found 3 Roses
Found 3 Roses
Found 3 Roses
Found 2 Daffodils
Found 2 Daffodils
I know why the code does this but I don't know how to erase repeats of output. I also don't know how to implement Collections correctly. I've used Collections on an ArrayList of strings before...and it works. But this time I'd be using Collections on an ArrayList of Objects, and I want to check for the frequency of each specific name. Here is the main class:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class MainClass {
static ArrayList<FlowerClass> flowerPack = new ArrayList<FlowerClass>();
public static void main(String[] args){
Scanner input = new Scanner(System.in);
while(true){
System.out.println("1. Add flower to flowerpack.");
System.out.println("2. Remove flower from the flowerpack.");
System.out.println("3. Search for a flower in the flowerpack.");
System.out.println("4. Display the flowers in the flowerpack.");
System.out.println("5. Exit the program.");
int userChoice = input.nextInt();
switch(userChoice){
case 1:
addFlower();
break;
case 2:
searchFlower();
break;
case 3:
displayFlowers();
break;
case 4:
System.out.println("Goodbye!");
System.exit(0);
}
}
}
public static void addFlower(){
if (FlowerClass.numberFlowers() == 25){
System.out.println("There are 25 flowers in the flowerpack. Remove at least one in order to add more.");
return;
}
Scanner input = new Scanner(System.in);
System.out.println("What is the flower's name?");
String desiredName = input.nextLine();
System.out.println("What is the flower's color?");
String desiredColor = input.nextLine();
System.out.println("How many thorns does it have?");
Scanner input2 = new Scanner(System.in);
int desiredThorns = input2.nextInt();
System.out.println("What does it smell like?");
String desiredSmell = input.nextLine();
flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
}
public static void searchFlower(){
System.out.println("Enter the flower you want to search for.");
Scanner input = new Scanner(System.in);
String userChoice = input.nextLine();
int occurrences = 0;
for (FlowerClass flower: flowerPack){
String name = flower.getName();
if (userChoice.equals(name)){
occurrences++;
}
else if(occurrences == 0){
System.out.println("Match not found.");
return;
}
}
System.out.println("Found " + occurrences + " " + userChoice);
}
public static void searchFlower(String desiredFlower){
int occurrences = 0;
String userChoice = desiredFlower;
for (FlowerClass flower: flowerPack){
String name = flower.getName();
if (userChoice.equals(name)){
occurrences++;
}
}
System.out.println("Found " + occurrences + " " + userChoice);
}
public static void displayFlowers(){
int repeats = 0;
/*for (FlowerClass flower: flowerPack){
System.out.println(flower.getName());
}
System.out.println("Number of flowers in pack: " + FlowerClass.numberFlowers());*/
//int occurrences = Collections.frequency(flowerPack, name);
//System.out.println(name + ": " + occurrences);
for (FlowerClass flower: flowerPack){
String name = flower.getName();
searchFlower(name);
}
}
}
Here is the FlowerClass:
public class FlowerClass {
public static int numberOfFlowers = 0;
public String flowerName = null;
public String flowerColor = null;
public int numberThorns = 0;
public String flowerSmell = null;
FlowerClass(){
}
FlowerClass(String desiredName, String desiredColor, int desiredThorns, String desiredSmell){
flowerName = desiredName;
flowerColor = desiredColor;
numberThorns = desiredThorns;
flowerSmell = desiredSmell;
numberOfFlowers++;
}
public void setName(String desiredName){
flowerName = desiredName;
}
public String getName(){
return flowerName;
}
public static int numberFlowers(){
return numberOfFlowers;
}
}
If you look at my last function in the main class, you'll see that I commented out the way I was attempting to implement Collections.frequency. I also tried making a multidimensional array of Strings and storing the names of the flowers and also the number of flowers in the arrays. This was counting everything correctly but I wasn't sure how to display the names alongside the counts. It was getting very messy so I abandoned that attempt for now in favor of trying these other two options. If I can find a way to erase repeated lines of output (or if I can find a way to get Collections to work) then I won't need to tinker with the multidimensional array.
Any tips would be very much appreciated. Thank you for your time.
Interesting code, but it doesn't work the way I would do it.
In this current case as you've done it, you would need to keep track of the flower names you have already encountered:
public static void displayFlowers(){
//int repeats = 0;
List<String> displayedFlowerTypes = new ArrayList<String>();
for (FlowerClass flower: flowerPack){
String name = flower.getName();
if(!displayedFlowerTypes.contains(name))
{
displayedFlowerTypes.add(name);
searchFlower(name);
}
}
}
What I would rather do is maintain a Map that keeps track of the counts of the flower types, and just obtain the numbers for the types from that:
public class MainClass {
static List<FlowerClass> flowerPack = new ArrayList<FlowerClass>();
static Map<String, Integer> flowerCount = new HashMap<String, Integer>();
public static void addFlower() {
if (FlowerClass.numberFlowers() == 25) {
System.out.println("There are 25 flowers in the flowerpack. Remove at least one in order to add more.");
return;
}
Scanner input = new Scanner(System.in);
System.out.println("What is the flower's name?");
String desiredName = input.nextLine();
System.out.println("What is the flower's color?");
String desiredColor = input.nextLine();
System.out.println("How many thorns does it have?");
Scanner input2 = new Scanner(System.in);
int desiredThorns = input2.nextInt();
System.out.println("What does it smell like?");
String desiredSmell = input.nextLine();
flowerPack.add(new FlowerClass(desiredName, desiredColor, desiredThorns, desiredSmell));
if(!flowerCount.containsKey(desiredName))
{
flowerCount.put(desiredName, 1);
}
else
{
int currentCount = flowerCount.get(desiredName);
flowerCount.put(desiredName, currentCount+1));
}
}
That way, you could just display the flowers as the following:
public static void displayFlowers() {
for (String name : flowerCount.keySet()) {
//searchFlower(name);
System.out.println("Found " + flowerCount.get(name) + " " + name);
}
}
You could put your Flower(s) in a Set. But the easiest solution I can think of is to sort your flowers. So first, implement a Comparator<FlowerClass>
public static class FlowerComparator implements Comparator<FlowerClass> {
#Override
public int compare(FlowerClass o1, FlowerClass o2) {
return o1.getName().compareTo(o2.getName());
}
}
Then you can sort with Collections.sort(List, Comparator)
FlowerComparator flowerComparator = new FlowerComparator();
Collections.sort(flowerPack, flowerComparator);
And then your for loop needs to be something like this (to stop searching for the same flower),
String lastName = null;
for (int i = 0; i < flowerPack.size(); i++){
FlowerClass flower = flowerPack.get(i);
String name = flower.getName();
if (lastName == null || !lastName.equals(name)) {
lastName = name;
searchFlower(name); // or return the number found, and then add that count to i.
}
}
I'm having a problem with my project in Java.
Here's the situation: the user will enter a character and integer of course code subj and units with a loop and restriction that indicates that when the total unit reaches "25" it will stop adding subj and course.
For example:
course code | subject | units
------------+-----------+-------
GE 111 | education | 3
GE 112 | history | 1
and so on..
After the user has entered the codes above, it will count all units and it will print to something thanks for the help. The code is too long, here is the most important part:
int[] a = new int[6];
Scanner sc = new Scanner(System.in);
System.out.println("\nenter units");
for (int j = 0; j < 9; j++) {
a[j] = sc.nextInt();
}
System.out.println("your subjects are:\n : ");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
System.out.print("\nEnter course code:" + a);
code = br.readLine().charAt(0);
System.out.print("\nenter course description");
subj = br.readLine().charAt(0);
System.out.print("\nenter units");
units = br.readLine().charAt(0);
case 2: break;
case 3: break;
case 4: break;
}
}
Are you trying to achieve this:
Main.java
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Make one Course container list to store a set of courses
List<Course> courses = new ArrayList<Course>();
//to store sum of units, intially 0
int unitSum = 0;
Course course = null;
while(true){
//make new course
course = new Course();
System.out.println("Enter course code:");
course.setSubjectCode(sc.nextInt());//store course code
System.out.println("Enter subject name:");
course.setSubjectName(sc.next());//store subject name
System.out.println("Enter units:");
course.setUnits(sc.nextInt());//store units
courses.add(course);//add Course to container
//take sum of units
unitSum += course.getUnits();
if(unitSum>=25)//end loop if sum reached 25
break;
}
System.out.println(courses);
sc.close();
}
}
Course.java
public class Course {
private int subjectCode;
private String subjectName;
private int units;
public Course() {
}
public int getSubjectCode() {
return subjectCode;
}
public void setSubjectCode(int subjectCode) {
this.subjectCode = subjectCode;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public int getUnits() {
return units;
}
public void setUnits(int units) {
this.units = units;
}
#Override
public String toString() {
return "Course [subjectCode=" + subjectCode + ", subjectName="
+ subjectName + ", units=" + units + "]";
}
}
this may help you..
Why don't you make object of what you need to work with? Then you can have it so much easier. Make a class and constructor of whathewer you want to use as entry new Entry(data1,data2,data3) work with all of those. It will help you when you make your program more complex.
To sum up the array to get total units
int sum=0;
for(int i:yourArray){
sum+=i;
}
The problem is that you work with all of your data as individual pars try to make some connection between them. Make object and put some functions in. (sum units), (get desc) etc. - it will be easy for you then to precede.
the question is :
A fruit shop sells several types of fruits each day. Write a program that reads from user several lines of input.Each line includes a fruit's name,price per kilogram (as an integer), number of kilograms sold (as an integer).
the program should calculate and print the earned money of all fruits sold and fruit that achieved largest profit.
hint: -you could assume that user will insert valid data -user could stop the program via entering the word "stop" as a fruit's name.
Sample input and out put:
in each line, insert a fruit's name, price per kilogram, number of kilograms sold. To halt the program,insert "stop" as a fruit's name
banana 2 11
mango 3 8
peach 4 5
stop
the earned money of all fruits sold: 66
fruit that achieved the largest profit: mango
what i wrote now:
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner (System.in);
String fruitname= " ";
String maxfruit = " ";
int price = 0,number=0;
int sum=0;
int max=0;
System.out.print("Fruit name, " + "price in killogram, number of killogram sold: ");
while (!fruitname.equals("stop"))
{
fruitname = input.next();
price = input.nextInt();
number = input.nextInt();
}
if (fruitname.equals("stop"))
{
sum = sum+(price*number);
}
if (max<(price*number))
{
max = price*number;
maxfruit = fruitname;
}
System.out.println("the earned money of all fruits is " + sum);
System.out.println("fruit that achieved the largest profit is "+ maxfruit);
}
}
the program is not reading what i submit to it, don't know why and not giving me the sum and the max fruit.. what is the problem of what i wrote?
As you can see your reads happen in the while loop:
while (!fruitname.equals("stop"))
{
fruitname = input.next();
price = input.nextInt();
number = input.nextInt();
}
Every time it loops - it overrides the values. Finally when you read stop and exit the loop - your fruitname is stop. So you need to fix your logic on how you would want to read in the input
Working variant:
public class FruitTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Fruit name, " + "price in killogram, number of killogram sold: ");
String text = input.nextLine();
String[] words = text.split(" ");
List<Fruit> fruits = parseInput(words);
int sum = getSum(fruits);
String popular = getPopularFruitName(fruits);
System.out.println("Got fruits: " + fruits.toString());
System.out.println("the earned money of all fruits is " + sum);
System.out.println("fruit that achieved the largest profit is " + popular);
}
private static String getPopularFruitName(List<Fruit> fruits) {
int max = 0;
String name = null;
for (Fruit fruit : fruits) {
int checkVal = fruit.getPrice() * fruit.getAmount();
if(checkVal > max) {
max = checkVal;
name = fruit.getName();
}
}
return name;
}
private static int getSum(List<Fruit> fruits) {
int result = 0;
for (Fruit fruit : fruits) {
result += fruit.getPrice() * fruit.getAmount();
}
return result;
}
private static List<Fruit> parseInput(String[] words) {
List<Fruit> result = new ArrayList<Fruit>();
int element = 1;
final int name = 1;
final int price = 2;
final int amount = 3;
Fruit fruit = null;
for (String word : words) {
if (word.equals("stop") || word.isEmpty()) {
break;
}
if(element > amount)
element = name;
switch (element) {
case name:
fruit = new Fruit(word);
result.add(fruit);
break;
case price:
if (fruit != null) {
fruit.setPrice(Integer.valueOf(word));
}
break;
case amount:
if(fruit != null) {
fruit.setAmount(Integer.valueOf(word));
}
break;
}
element++;
}
return result;
}
static class Fruit {
String name;
int price = 0;
int amount = 0;
Fruit(String name) {
this.name = name;
}
String getName() {
return name;
}
int getPrice() {
return price;
}
void setPrice(int price) {
this.price = price;
}
int getAmount() {
return amount;
}
void setAmount(int amount) {
this.amount = amount;
}
#Override
public String toString() {
return name + ". $" + price +
", amount=" + amount;
}
}
}
Comments to code - it's proper way to parse all the inputted string and parse it to an object that stores all the data - name, price and amount. Store all parsed objects into array or a list and then calculate max and popular fruit while looping your parsed fruit array
I found some mistake. The most important was in the while condition. Check this out.
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner (System.in);
String fruitname = null;
String maxfruit = null;
int fruitSum = 0;
int totalSum = 0;
int max = 0;
System.out.print("Fruit name, " + "price in killogram, number of killogram sold: ");
while(!(fruitname = input.next()).equals("stop")){
fruitSum = input.nextInt() * input.nextInt();
totalSum += fruitSum;
if(fruitSum > max){
maxfruit = fruitname;
max = fruitSum;
}
}
System.out.println("the earned money of all fruits is " + totalSum);
System.out.println("fruit that achieved the largest profit is "+ maxfruit);
}
}
Oh it is reading it.
the problem is that it doesn't do what you want it to do.
the problems with the code I can see are this:
you are not storing the fruits quantities or prices anywhere, you need to store the values
in an array or something (maxFruit,MaxValue) to compare them later.
when you are reading the fruit values and a "stop" string is input the next step in your code is to wait for the price so it won't get out of the loop even if you input "stop", you need to restructure your scanner loop.
And if it is a beginner class it may be ok, but the code you are writing is not object oriented don't write the logic in the main.
You may want to learn to debug it is a very useful tool when you are learning to code, if you run this program in debug mode , you could see that the values are getting input and everything that is happening, Netbeans and Eclipse have very good debuggers and it would be worth to expend half an hour learning the basics of debugging It certainly helped me a lot when I was starting.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FruitSells {
public static void main(String... args) {
BufferedReader bufer = new BufferedReader(new InputStreamReader(System.in));
try {
String str;
String[] inarr;
int sumMoney = 0;
do {
str = (String) bufer.readLine();
inarr = str.split(" ");
for(int i = 1; i < inarr.length; i += 3) {
sumMoney += Integer.parseInt(inarr[i]) * Integer.parseInt(inarr[i + 1]);
}
System.out.println(sumMoney);
sumMoney = 0;
} while (!str.equals("stop"));
} catch(IOException ex) {
System.out.println("Problems with bufer.readLine()");
}
}
}
something like this you can modernize it.sorry for eng i can not speak))and write correctly of course))