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

maroon1

New Member
[FONT=verdana, arial, helvetica] Hi

[/FONT][FONT=verdana, arial, helvetica] 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

[/FONT][FONT=verdana, arial, helvetica]I will give this part of a program as 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);

}


[/FONT][FONT=verdana, arial, helvetica]The problem in this program 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
[/FONT]
 
Last edited:
string_value = null;?
anyway, that is not the problem. I never liked that api for input (forgot what it is called... Scanner maybe???) When I was in computer science it was hard to use. Instead of using .nextDouble(), just do .nextLine() and then convert that string from the method into a double. Example
double_value = (double)(input.nextLine());
EDIT: BTW if you are just doing a simple iteration, a for loop would be much more appropriate.
 
Last edited:
PHP:
import java.util.Scanner;

public class Assignment1

{
    
 public static void main (String args[])    
 
    
  {
    
    Scanner input = new Scanner (System.in);
     
     double value;
     double earning;
     String item;
     int X=1;
    
     
  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);
    
    System.out.println();
    
    
    }
    
    
    }
    
}
When I execute this program this appears to me
1ph0.jpg


I have typed ^^ the name of the item, and it is value and after I press enter it displays the earning.

But the problem is that I can't type the name of the item again.

Look here
2dt5.jpg


So what should I do if I want to write the name of the item again ?

Please help
 
Last edited:
Did you even try what i said?

no, because I didn't understand what you told me

What "converting string from the method into a double" has to do with what I want ??

Please if you know how to write the program for me correctly then write it.
 
The reason it wont work that way is because when you use the method nextDouble() scanner is still on the same line, its hard to explain and that is one of the reasons i hate this api so much. Anyway, one added line and it works.
PHP:
import java.util.Scanner;

public class Assignment1

{
    
public static void main (String args[])    {
    
    Scanner input = new Scanner (System.in);
     double value;
     double earning;
     String item;
     int X=1;

  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();
    input.nextLine();
    
    earning = value + 200 + (((9*value)/100));
    System.out.printf("You have earned $%.2f\n",earning);
    System.out.println();
  }
 }
    
}
EDIT: the spacing is all screwed up now, but basically just add input.nextLine() right after you get the double from the scanner.
 
Last edited:
Thank you very much

But I found another way to do it
You need to just to put the "Scanner input = new Scanner (System.in);" after the while statement.


PHP:
import java.util.Scanner;

public class Assignment1

{
    
public static void main (String args[])    {
    
    
     double value;
     double earning;
     String item;
     int X=1;

  while (X>0) {    
  
  
   Scanner input = new Scanner (System.in);
    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);
    System.out.println();
  }
}
 
Back
Top