473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to allow string to change buffer?

I want to make a sort of simple menu in this program, but I can't seem to make the string "command" change. I couldn't understand the buffer string concept, and char only applies to single letters, numbers, or symbols. Is there some way to keep entering words into "command" whether it is a char or a string? This is was easier in C++ because strings were not immutable. Could you please suggest a solution?

import java.util.Scanner;
import java.lang.StringBuffer;

public class Driver {



public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Coins CoinsdHsu = new Coins();
String command = "";

System.out.println("Enter commands, enter Menu to view options");

while(true){
System.out.println("Command: ");
command = in.nextString();

if(command == "Menu"){
CoinsdHsu.menu();
}else if(command == "Enter number of cents"){
CoinsdHsu.howMany();
}else if(command == "Calculate coin distribution"){
CoinsdHsu.calculate();
}else if(command == "Stop"){
break;
}else{
System.out.println("Enter a valid command");
}
}

}
}

class Coins {

private int numCents;

public Coins(){};

public void calculate() {

int numQuarter = numCents / 25;
int quarterRemain = numCents % 25;
int numDime = quarterRemain / 10;
int dimeRemain = quarterRemain % 10;
int numNickel = dimeRemain / 5;
int numPenny = dimeRemain % 5;

System.out.println(numCents + " cents =>");
System.out.println("Quarter(s)\t" + numQuarter);
System.out.println("Dime(s)\t\t" + numDime);
System.out.println("Nickel(s)\t" + numNickel);
System.out.println("Penny(s)\t" + numPenny);
System.out.println("");
}

public void howMany(){
int cents;

Scanner in = new Scanner(System.in);

System.out.print("Number of cents: ");
cents = in.nextInt();

numCents = cents;
}

public void menu(){
System.out.println("Enter number of cents\nCalculate coin distribution\nStop\n");
}

}
Sep 9 '09 #1
4 2363
Oh I know in.nextString doesn't work now, but I just need some kind of way to enter different words into strings.
Sep 9 '09 #2
r035198x
13,262 8TB
Don't use == to compare strings. Use the equals method instead.
Sep 9 '09 #3
Tassos Souris
152 100+
I suggest controlling the menu options with plain integers. Use a function that displays the menu to the user, get the user input and return a appropriate code.
The codes could be:
Expand|Select|Wrap|Line Numbers
  1. public enum Menu{
  2.         MENU_ENTER_CENTS,
  3.         MENU_CALC_COIN_DISTR,
  4.         MENU_STOP,
  5.         MENU_INVALID
  6. }
  7.  
The function would be something like that:
Expand|Select|Wrap|Line Numbers
  1. public Menu getMenuOption(){
  2.         // Get user's input
  3.        return ( Menu.(OPTION) );
  4. }
  5.  
The menu processing code now more elegantly can be written as:
Expand|Select|Wrap|Line Numbers
  1. Menu option;
  2. while ( ( option = getMenuOption() ) != Menu.MENU_STOP ){
  3.         switch( option ){
  4.         case MENU_ENTER_CENTS:
  5.                 // do
  6.                 break;
  7.         case MENU_CALC_COIN_DISTR:
  8.                 // do
  9.                break;
  10.         // ....
  11.         default:
  12.                // do
  13.                break;
  14.         }
  15. }
  16.  
Hope i helped,
Tassos Souris
Sep 9 '09 #4
JosAH
11,448 Expert 8TB
There's a litle article on this issue in the Insights section here.

kind regards,

Jos
Sep 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: wqhdebian | last post by:
As far as I know,when encrypt or decrypt ,a key must first be got,and the key is first generate by a tool or from SecurityRandom,that means I can not generate the same key with the same input.Does...
5
by: David Sobey | last post by:
Hi Sorry bout this basic prob. Got a file called file.obj. tryna read the first line from it as a string and print it to the screen. getting errors: #include "stdafx.h" #include <stdio.h>...
4
by: Christopher | last post by:
I am using std::string to parse a command given by the user, I don't understand why the following snippet is not working as expected. string buffer, // store commands from...
15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
17
by: jake1138 | last post by:
Here is a function I have to get a number at the end of a string. I'm posting this in case it proves helpful to someone. Comments are welcome. int getnum(char *str) { char buffer; char *buf...
43
by: Steven T. Hatton | last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string Is there no C library function that will take an int and convert it to its ascii representation? The example Bjarne shows in his faq...
9
by: Michael D. Ober | last post by:
OK, I can't figure out a way to optimize the following VB 2005 code using StringBuilders: Public Const RecSize as Integer = 105 Private buffer As String Public Sub New() init End Sub...
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
21
by: yogicoder | last post by:
following is the code to accept a string from user, but i get segmentation fault as soon i have finished entering the string i.e. as soon as i press the 'enter' key. #include <stdio.h>...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.