Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » How can I reset the value of a String in Java programming ?
Closed Thread
Old 12-09-2006, 11:59 AM   #1 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Question How can I reset the value of a String in Java programming ?

Hi

I'm novice in programming

I want some to tell how can I reset the value of string when it is defined.

Sorry for my bad english, but I mean how can I remove the defintion from a string when it is defined

This is an example

while (X>0)
{

System.out.print("Enter the name of the Item:");
item=input.nextLine();

System.out.print("Enter the value of the item: ");
value = input.nextDouble();

earning = value + 200 + (((9*value)/100));

System.out.printf("You have earned %.2f\n",earning);

}



As you see here ^^ , this program has no limit but the problem is that I can input the name of the item for the first time only, after that I can't input the name.
I think it skips the line that says item=input.nextLine(); after the first loop


So, can someone tell me how can I input the name of the item without any limits. I want to write the name of the item in every loop.


Thanks in advance
maroon1 is offline  
Old 12-09-2006, 10:42 PM   #2 (permalink)
 
Newb Techie

Join Date: Dec 2006

Posts: 14

foolsdelyte

Send a message via AIM to foolsdelyte
Default

Change the line 'item = input.nextLine();' to 'item = input.next();'.
foolsdelyte is offline  
Old 12-10-2006, 08:33 AM   #3 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Default

I think that I have done the problem incorrectly


Can someone solve for me this problem please

Quote:
A large company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5,000 worth of merchandise in a week receives $200 plus %9 of $5,000, or a total of $650. You have been supplied with a list of the items sold by each salesperson. The values of these items are as follows:
Code:
 Item               Value
  1                  239.99
  2                  129.75
  3                  99.95
  4                  350.89
Develop a Java application that inputs one salesperson's items sold for the last week and calculates and displays that salesperson's earnings. There is no limit to the number of items that can be sold by a salesperson.

maroon1 is offline  
Old 12-10-2006, 02:18 PM   #4 (permalink)
 
Newb Techie

Join Date: Dec 2006

Posts: 14

foolsdelyte

Send a message via AIM to foolsdelyte
Default

Why are you inputting the item name? It doesn't seem relevant to the problem. Here's one way you could do it: Prompt the user for the number of items, n, sold by one salesperson and use a for loop, from 1 to n, to read in every item cost, each time incrementing the sum by the input value. So:

public static void main(String[] args) throws IOException {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter number of items sold: ");
int n = stdin.nextInt();
double sum = 0;
for(int i=0; i<n; i++){
System.out.println("Enter value of next item: ");
sum += stdin.nextDouble();
}
System.out.println("Salesperson earns $"+(200+(0.09*sum)));
}

Instead of using a loop, you could prompt the user to enter all of the values at once delimited by spaces. You would then split that input into tokens and manage from there.

If the problem is asking the user to input the item names, and not the item values, you might initialize an array with the item values in it (set the first index to 0) {0, 239.99, 129.75, etc.} and each time the user enters the item number n, increment sum by the contents of array index n.
foolsdelyte is offline  
Old 12-11-2006, 09:55 AM   #5 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Default

My friend, can you tell me how can I initialize an Array with item values in it, please ?
maroon1 is offline  
Old 12-11-2006, 09:59 AM   #6 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Default

So far, I have made this program, but can you modify and add Array list for me please


Code:
import java.util.Scanner;

public class Assignment1

{
    
public static void main (String args[])    {
    
    
     double value;
     double earning;
     String item;
     int X=1;
     double total=0;
     int Stop=2;
     int Y=1;
     double E=1;
      
     

   
    
  while (E==1) 
{

 Scanner input = new Scanner (System.in);
    if (total>0)
   
    {
    
       System.out.print("write '1' to stop adding more items, write '2' to continue adding: ");
       Stop=input.nextInt();
       if(Stop==1){
       
       E=2;
       }
       }
       
           
        if (Y !=Stop) 
{
 
 
  if (X==1) {    
  
    
        Scanner input2 = new Scanner (System.in);
    
    System.out.print("Enter the name of the Item: ");
    item=input2.nextLine();
  
    System.out.print("Enter the value of the item: ");
    value = input2.nextDouble();
    
   
    total+=value;
    
     
    System.out.println();
    }
     } 
     
      }
    earning = 200 + (((9*total)/100));
    System.out.printf("You have earned $%.2f\n",earning);
   
 

 
}
    
}

maroon1 is offline  
Old 12-11-2006, 11:32 AM   #7 (permalink)
 
Newb Techie

Join Date: Dec 2006

Posts: 14

foolsdelyte

Send a message via AIM to foolsdelyte
Default

I still don't understand why you are inputting the item name as a String, when the problem as you described it names items from 1 to 4. The code I've just written is a little less complicated than yours, but it does basically the same thing. I commented most of the lines so you know what's going on. If you're going to initialize an array with the item values, there's no need to prompt the user for those values, just the item's 'name' (an integer from 1 to 4) that will access the value at that index in the array. The output is user friendly, so it should make sense:

import java.util.*;
import java.io.*;
public class Assignment{
public static void main(String[] args)throws IOException {
//Array of values: index corresponds to item's price
//i.e. Item 1 = 239.99, Item 2 = 129.75, etc.
double[] itemvalues = new double[]{0, 239.99, 129.75, 99.95, 350.89};
Scanner stdin = new Scanner(System.in);
int inp = 0; //User will input item number (name)
int sum = 0; //Keeps track of the sum
while(true){ //Program will loop until user enters a # <1 or >4
System.out.println("Enter the name (1 to 4) of the item sold. When finished, press any other key: ");
inp = stdin.nextInt();
if(inp<1||inp>4) //Will break if inp != 1-4
break;
else{
sum += itemvalues[inp];
}
}
sum = 200 + (((9*sum)/100));
System.out.println("You have earned: "+sum);
}
}
foolsdelyte is offline  
Old 12-11-2006, 12:27 PM   #8 (permalink)
 
Newb Techie

Join Date: Dec 2006

Posts: 14

foolsdelyte

Send a message via AIM to foolsdelyte
Default

One change. When it prompts the user to enter 'any key' to exit the program, I mean any number key. A key other than a number will cause a mismatch error.
foolsdelyte is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On