ShinyWaxBullet ShinyWaxBullet - 21 days ago 6
Java Question

No idea what's happening - No errors (New to Java)

I'm ready to learn as long as you'll teach me. I'm just sort of fiddling around with Java as it's the best way for me to learn. If you find anything that you think I could improve in my code, I'd love to hear about it.

The overall goal of the project I've prepared for myself is to create a program with Java that stores information about people. Then transfer that information via JavaScript to a Tomcat server I'll set up running SQL.

Now that you understand my overall goal (I'm far from it), my problem is as follows:

I can get to the part of the code that asks me for an entry command and name etc. no problem. However, when I type in "read" and press enter, it seemingly skips over the else if statement I have set up and goes straight to the else section.

Edit: I updated the post to show what solved the problem.

Working Code:

import javax.print.DocFlavor.READER;

import java.util.ArrayList;
import java.util.Scanner;


public class Main {

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int total = 0;
int years = 0;
int invested =0;
ArrayList<String> names = new ArrayList<String>(0);
ArrayList<String> address = new ArrayList<String>(0);
ArrayList<String> ids = new ArrayList<String>(0);
Scanner sReader = new Scanner(System.in);

while(reader.nextLine().toLowerCase().split(" ")[0] != "exit")
{
System.out.print("Enter Instruction: ");
String n = reader.nextLine();
String[] uin = n.toLowerCase().split(" ");
if(uin[0].equals("add"))
{
try {
System.out.println(add(Integer.parseInt(uin[1]),Integer.parseInt(uin[2])));
} catch (NumberFormatException e) {
System.out.println("Numbers Only");
System.out.print(e.getMessage());
} catch (Exception e){
System.out.println("ERROR");
}
}


if(uin[0].equals("sub") || uin[0].equals("subtract"))
{
try {
System.out.print(Integer.parseInt(uin[1])-Integer.parseInt(uin[2]));
} catch (NumberFormatException e){
System.out.println("Numbers Only");
System.out.print(e.getMessage());
} catch (Exception e){
System.out.println("ERROR");
}

}

if(uin[0].equals("string"))
{
String string = "this is a string.";
System.out.println(string.indexOf("cat"));
}
if(uin[0].equals("a"))
{
total+=Integer.parseInt(uin[1]);
invested+=Integer.parseInt(uin[1]);
}
if(uin[0].equals("aa"))
{
total+=10000;
invested+=10000;
}
if(uin[0].equals("bb"))
{
total-=55000;
invested-=55000;
}
if(uin[0].equals("aq"))
{
for(int i = 0;i < Integer.parseInt(uin[1]);i++){
total+=10000;
invested+=10000;
total*=1.06;
years+=1;
}
years-=1;
}
if(uin[0].equals("exit"))
{
break;
}
if(uin[0].equals("entry")){
for(int i=0;i==0;i+=0){
System.out.print("Entry Command: ");
String input = sReader.nextLine();
if(input.equals("exit")){
i = 1;
}else if(input.equals("read")){
for(int y=0;y<names.size();y++){
System.out.println("User #"+y);
System.out.println(names.get(y).toString());
System.out.println(address.get(y).toString());
System.out.println(ids.get(y).toString());
}
}else{
System.out.print("Name: ");
input = sReader.nextLine();
names.add(input);
System.out.print("Address: ");
input = sReader.nextLine();
address.add(input);
System.out.print("ID: ");
input = sReader.nextLine();
ids.add(input);
}
}
}
total*=1.06;
years+=1;
double earn = Math.floor(total) * 0.06;
System.out.println("Bank: " + total);
System.out.println("Invested: " + invested);
System.out.println("Yearly Earnings: " + earn);
System.out.println("Years: " + years);
}
}
public static int add(int n1, int n2){
int out;
try{
out = (n1+n2);
return(out);
}catch (Exception e){
System.out.print("ERROR");
return(0);
}
}
}


Old Code:

import javax.print.DocFlavor.READER;

import java.util.ArrayList;
import java.util.Scanner;


public class Main {

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int total = 0;
int years = 0;
int invested =0;
ArrayList<String> names = new ArrayList<String>(0);
ArrayList<String> address = new ArrayList<String>(0);
ArrayList<String> ids = new ArrayList<String>(0);
Scanner sReader = new Scanner(System.in);

while(reader.nextLine().toLowerCase().split(" ")[0] != "exit")
{
System.out.print("Enter Instruction: ");
String n = reader.nextLine();
String[] uin = n.toLowerCase().split(" ");
if(uin[0].equals("add"))
{
try {
System.out.println(add(Integer.parseInt(uin[1]),Integer.parseInt(uin[2])));
} catch (NumberFormatException e) {
System.out.println("Numbers Only");
System.out.print(e.getMessage());
} catch (Exception e){
System.out.println("ERROR");
}
}


if(uin[0].equals("sub") || uin[0].equals("subtract"))
{
try {
System.out.print(Integer.parseInt(uin[1])-Integer.parseInt(uin[2]));
} catch (NumberFormatException e){
System.out.println("Numbers Only");
System.out.print(e.getMessage());
} catch (Exception e){
System.out.println("ERROR");
}

}

if(uin[0].equals("string"))
{
String string = "this is a string.";
System.out.println(string.indexOf("cat"));
}
if(uin[0].equals("a"))
{
total+=Integer.parseInt(uin[1]);
invested+=Integer.parseInt(uin[1]);
}
if(uin[0].equals("aa"))
{
total+=10000;
invested+=10000;
}
if(uin[0].equals("bb"))
{
total-=55000;
invested-=55000;
}
if(uin[0].equals("aq"))
{
for(int i = 0;i < Integer.parseInt(uin[1]);i++){
total+=10000;
invested+=10000;
total*=1.06;
years+=1;
}
years-=1;
}
if(uin[0].equals("exit"))
{
break;
}
if(uin[0].equals("entry")){
for(int i=0;i==0;i+=0){
System.out.print("Entry Command: ");
sReader = new Scanner(System.in);
System.out.println(sReader.nextLine());
if(sReader.nextLine().equals("exit")){
i = 1;
}else if(sReader.nextLine().equals("read")){
System.out.println(names.size());
System.out.println("yup");
for(int y=0;y==names.size()-1;y++){
System.out.println(names);
System.out.println(names.get(y));
System.out.println(address.get(y));
System.out.println(ids.get(y));
}
}else{
System.out.print("Name: ");
sReader = new Scanner(System.in);
names.add(sReader.nextLine());
System.out.print("Address: ");
sReader = new Scanner(System.in);
address.add(sReader.nextLine());
System.out.print("ID: ");
sReader = new Scanner(System.in);
ids.add(sReader.nextLine());
}
}
}
total*=1.06;
years+=1;
double earn = Math.floor(total) * 0.06;
System.out.println("Bank: " + total);
System.out.println("Invested: " + invested);
System.out.println("Yearly Earnings: " + earn);
System.out.println("Years: " + years);
}
}
public static int add(int n1, int n2){
int out;
try{
out = (n1+n2);
return(out);
}catch (Exception e){
System.out.print("ERROR");
return(0);
}
}
}

Answer

It's going wrong because you are doing System.out.println(sReader.nextLine()) - this automatically gets the last input (so "exit" in your example). But when you try to do if (sReader.nextLine().equals("exit")) it's looking for the next line again.

To fix, you could store the input in a variable:

String input = sReader.nextLine();
System.out.println(input);
if (input.equals("exit")) {
    // etc
}